Skip to content

Commit

Permalink
test for icon name match
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna-f1sh committed Jul 3, 2024
1 parent 41c7286 commit 3f0e954
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion doc/cyme_example_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"user": {
"classifier#02": "",
"classifier-sub-protocol#fe:01:01": "",
"name#.*sd\\scard\\sreader.*": "",
"name#.*^[sS][dD]\\s[cC]ard\\s[rR]eader.*": "",
"undefined-classifier": "",
"unknown-vendor": "",
"vid#05ac": "",
Expand Down
37 changes: 29 additions & 8 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,15 +379,17 @@ impl IconTheme {

/// Get icon for USBDevice `d` by checking `Self` using Name, Vendor ID and Product ID
pub fn get_device_icon(&self, d: &USBDevice) -> String {
if let (Some(vid), Some(pid)) = (d.vendor_id, d.product_id) {
// try name first since vidpid will return UnknownVendor default icon if not found
// does mean regex will be built/checked for every device
match self.get_name_icon(&d.name) {
s if !s.is_empty() => s,
_ => self.get_vidpid_icon(vid, pid),
// try name first since vidpid will return UnknownVendor default icon if not found
// does mean regex will be built/checked for every device
match self.get_name_icon(&d.name) {
s if !s.is_empty() => s,
_ => {
if let (Some(vid), Some(pid)) = (d.vendor_id, d.product_id) {
self.get_vidpid_icon(vid, pid)
} else {
String::new()
}
}
} else {
String::new()
}
}

Expand Down Expand Up @@ -629,4 +631,23 @@ mod tests {
Icon::Name(r".*^[sS][dD]\s[cC]ard\s[rR]eader.*".to_string())
);
}

#[test]
fn icon_match_name() {
let device = USBDevice {
name: "SD Card Reader".to_string(),
..Default::default()
};

let theme = IconTheme {
user: Some(HashMap::from([(
Icon::Name(r".*^[sS][dD]\s[cC]ard\s[rR]eader.*".to_string()),
"\u{ef61}".into(),
)])),
..Default::default()
};

let icon = theme.get_device_icon(&device);
assert_eq!(icon, "\u{ef61}");
}
}

0 comments on commit 3f0e954

Please sign in to comment.