Skip to content

Commit

Permalink
Handle empty lines in hidutil output
Browse files Browse the repository at this point in the history
  • Loading branch information
rossmacarthur committed Feb 11, 2024
1 parent a59d12b commit d637bcc
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use anyhow::{anyhow, Context, Result};

use crate::cmd::CommandExt;
use crate::hex;
pub use crate::types::{Key, Map, Mappings};
pub use crate::types::Map;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Device {
Expand Down Expand Up @@ -44,6 +44,12 @@ fn parse_hidutil_output(mut output: &str) -> Result<Vec<Device>> {
output = &output[line.len() + 1..];

while !output.is_empty() {
// skip over any leading newlines
if output.starts_with('\n') {
output = &output[1..];
continue;
}

let mut line_end = 0;

// parse the line into a map of header -> value using the header
Expand Down Expand Up @@ -160,6 +166,8 @@ fn split_whitespace_indices(s: &str) -> impl Iterator<Item = (&str, usize)> + '_
mod tests {
use super::*;

use crate::types::{Key, Map};

#[test]
fn test_dump() {
let mappings = vec![Map(Key::Raw(0x7000000e), Key::Raw(0x7000000f))];
Expand Down Expand Up @@ -297,4 +305,30 @@ UserDevice 1
]
);
}

#[test]
fn test_parse_hidutil_output_empty_line() {
let output = r#"Services:
VendorID ProductID LocationID UsagePage Usage RegistryID Transport Class
Devices:
VendorID ProductID Product Built-In
0x0 0x0 BTM (null)
"#;

let devices = parse_hidutil_output(output).unwrap();
assert_eq!(
devices,
vec![Device {
vendor_id: 0,
product_id: 0,
name: "BTM".to_owned()
}]
);
}
}

0 comments on commit d637bcc

Please sign in to comment.