-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathicon.rs
563 lines (519 loc) · 23 KB
/
icon.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Icons and themeing of cyme output
use serde::{Deserialize, Serialize};
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;
use crate::display::Encoding;
use crate::error::{Error, ErrorKind};
use crate::system_profiler::{USBBus, USBDevice};
use crate::usb::{ClassCode, Direction};
/// If only standard UTF-8 characters are used, this is the default icon for a device
// const UTF8_DEFAULT_DEVICE_ICON: &str = "\u{2023}"; // ‣
/// Serialize alphabetically for HashMaps so they don't change each generation
fn sort_alphabetically<T: Serialize, S: serde::Serializer>(
value: &T,
serializer: S,
) -> Result<S::Ok, S::Error> {
let value = serde_json::to_value(value).map_err(serde::ser::Error::custom)?;
value.serialize(serializer)
}
/// Icon type enum is used as key in `HashMaps`
#[derive(Debug, Clone, Hash, PartialEq, Eq, SerializeDisplay, DeserializeFromStr)]
pub enum Icon {
/// Vendor ID lookup
Vid(u16),
/// Vendor ID and Product ID exact match
VidPid((u16, u16)),
/// Use to mask on msb of product ID
VidPidMsb((u16, u8)),
/// Class classifier icon
Classifier(ClassCode),
/// Class classifier lookup with SubClass and Protocol
ClassifierSubProtocol((ClassCode, u8, u8)),
/// Icon for unknown vendors
UnknownVendor,
/// Icon for undefined classifier
UndefinedClassifier,
/// Icon to use when tree is being printed within an extending branch
TreeEdge,
/// Icon to use for non-last list item
TreeLine,
/// Icon to use at last item in list
TreeCorner,
/// Blanking icon for inset without edge
TreeBlank,
/// Icon at prepended before printing `USBBus`
TreeBusStart,
/// Icon printed at end of tree before printing `USBDevice`
TreeDeviceTerminator,
/// Icon printed at end of tree before printing configuration
TreeConfigurationTerminator,
/// Icon printed at end of tree before printing interface
TreeInterfaceTerminator,
/// Icon for endpoint direction
Endpoint(Direction),
}
impl FromStr for Icon {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let value_split: Vec<&str> = s.split('#').collect();
let enum_name = value_split[0];
// no value in string, match kebab-case
if value_split.len() == 1 {
match enum_name {
"unknown-vendor" => Ok(Icon::UnknownVendor),
"undefined-classifier" => Ok(Icon::UndefinedClassifier),
"tree-edge" => Ok(Icon::TreeEdge),
"tree-blank" => Ok(Icon::TreeBlank),
"tree-line" => Ok(Icon::TreeLine),
"tree-corner" => Ok(Icon::TreeCorner),
"tree-bus-start" => Ok(Icon::TreeBusStart),
"tree-device-terminator" => Ok(Icon::TreeDeviceTerminator),
"tree-configuration-terminator" => Ok(Icon::TreeConfigurationTerminator),
"tree-interface-terminator" => Ok(Icon::TreeInterfaceTerminator),
"endpoint_in" => Ok(Icon::Endpoint(Direction::In)),
"endpoint_out" => Ok(Icon::Endpoint(Direction::Out)),
_ => Err(Error::new(
ErrorKind::Parsing,
"Invalid Icon enum name or valued enum without value",
)),
}
// enum contains value
} else {
let (parse_ints, errors): (Vec<Result<u32, _>>, Vec<_>) = value_split[1]
.split(':')
.map(|vs| u32::from_str_radix(vs.trim_start_matches("0x"), 16))
.partition(Result::is_ok);
let numbers: Vec<u16> = parse_ints.into_iter().map(|v| v.unwrap() as u16).collect();
if !errors.is_empty() {
return Err(Error::new(
ErrorKind::Parsing,
"Invalid value in enum string after #",
));
}
match value_split[0] {
"vid" => match numbers.first() {
Some(i) => Ok(Icon::Vid(*i)),
None => Err(Error::new(ErrorKind::Parsing, "No value for enum after $")),
},
"vid-pid" => match numbers.get(0..2) {
Some(slice) => Ok(Icon::VidPid((slice[0], slice[1]))),
None => Err(Error::new(ErrorKind::Parsing, "No value for enum after $")),
},
"vid-pid-msb" => match numbers.get(0..2) {
Some(slice) => Ok(Icon::VidPidMsb((slice[0], slice[1] as u8))),
None => Err(Error::new(ErrorKind::Parsing, "No value for enum after $")),
},
"classifier" => match numbers.first() {
Some(i) => Ok(Icon::Classifier(ClassCode::from(*i as u8))),
None => Err(Error::new(ErrorKind::Parsing, "No value for enum after $")),
},
"classifier-sub-protocol" => match numbers.get(0..3) {
Some(slice) => Ok(Icon::ClassifierSubProtocol((
ClassCode::from(slice[0] as u8),
slice[1] as u8,
slice[2] as u8,
))),
None => Err(Error::new(ErrorKind::Parsing, "No value for enum after $")),
},
_ => Err(Error::new(
ErrorKind::Parsing,
"Invalid Icon enum value holder",
)),
}
}
}
}
impl fmt::Display for Icon {
/// Output is a Enum kebab case with # separating base16 : separated values, _ if String value
///
/// ```
/// use cyme::icon::*;
///
/// let icon: Icon = Icon::VidPid((0x1d50, 0x6018));
/// assert_eq!(format!("{}", icon), "vid-pid#1d50:6018");
///
/// let icon: Icon = Icon::UnknownVendor;
/// assert_eq!(format!("{}", icon), "unknown-vendor");
/// ```
///
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Icon::Vid(v) => write!(f, "vid#{:04x}", v),
Icon::VidPid((v, p)) => write!(f, "vid-pid#{:04x}:{:04x}", v, p),
Icon::VidPidMsb((v, p)) => write!(f, "vid-pid-msb#{:04x}:{:02x}", v, p),
Icon::Classifier(c) => write!(f, "classifier#{:02x}", u8::from(c.to_owned())),
Icon::ClassifierSubProtocol(c) => write!(
f,
"classifier-sub-protocol#{:02x}:{:02x}:{:02x}",
u8::from(c.0.to_owned()),
c.1,
c.2
),
Icon::Endpoint(Direction::In) => write!(f, "endpoint_in"),
Icon::Endpoint(Direction::Out) => write!(f, "endpoint_out"),
_ => {
let dbg_str = format!("{:?}", self);
write!(f, "{}", heck::AsKebabCase(dbg_str))
}
}
}
}
/// Allows user supplied icons to replace or add to [`DEFAULT_ICONS`] and [`DEFAULT_UTF8_TREE`]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct IconTheme {
/// Will merge with [`DEFAULT_ICONS`] for user supplied
#[serde(serialize_with = "sort_alphabetically")]
pub user: Option<HashMap<Icon, String>>,
/// Will merge with [`DEFAULT_UTF8_TREE`] for user supplied tree drawing
#[serde(serialize_with = "sort_alphabetically")]
pub tree: Option<HashMap<Icon, String>>,
}
/// Make default icons lazy_static and outside of IconTheme keeps them static but can be overridden user HashMap<Icon, String> at runtime
impl Default for IconTheme {
fn default() -> Self {
IconTheme {
user: None,
tree: None,
}
}
}
lazy_static! {
/// Default icons to draw tree can be overridden by user icons with IconTheme `tree`
static ref DEFAULT_UTF8_TREE: HashMap<Icon, &'static str> = {
HashMap::from([
(Icon::TreeEdge, "\u{251c}\u{2500}\u{2500}"), // "├──"
(Icon::TreeLine, "\u{2502} "), // "│ "
(Icon::TreeCorner, "\u{2514}\u{2500}\u{2500}"), // "└──"
(Icon::TreeBlank, " "), // should be same char width as above
(Icon::TreeBusStart, "\u{25CF}"), // "●"
(Icon::TreeDeviceTerminator, "\u{25CB}"), // "○"
(Icon::TreeConfigurationTerminator, "\u{2022}"), // "•"
(Icon::TreeInterfaceTerminator, "\u{25E6}"), // "◦"
(Icon::Endpoint(Direction::In), "\u{2192}"), // →
(Icon::Endpoint(Direction::Out), "\u{2190}"), // ←
// (Icon::Endpoint(Direction::In), ">".into()), // →
// (Icon::Endpoint(Direction::Out), "<".into()), // ←
])
};
/// Ascii chars used by lsusb compatible mode or no utf-8
static ref DEFAULT_ASCII_TREE: HashMap<Icon, &'static str> = {
HashMap::from([
(Icon::TreeEdge, "|__"), // same as corner
(Icon::TreeLine, "| "), // no outside line but inset so starts under parent device
(Icon::TreeCorner, "|__"),
(Icon::TreeBlank, " "), // inset like line
(Icon::TreeBusStart, "/: "),
(Icon::TreeDeviceTerminator, "O"), // null
(Icon::TreeConfigurationTerminator, "o"), // null
(Icon::TreeInterfaceTerminator, "."), // null
(Icon::Endpoint(Direction::In), ">"), //
(Icon::Endpoint(Direction::Out), "<"), //
])
};
/// Default icon lookup can be overridden by user icons with IconTheme `icons`
///
/// Should probably keep fairly short but I've added things I use like debuggers, mcus as examples
pub static ref DEFAULT_ICONS: HashMap<Icon, &'static str> = {
HashMap::from([
(Icon::UnknownVendor, "\u{f287}"), // usb plug default
(Icon::Vid(0x05ac), "\u{f179}"), // apple
(Icon::Vid(0x8086), "\u{f179}"), // apple bus
(Icon::Vid(0x045e), "\u{f871}"), // microsoft
(Icon::Vid(0x18d1), "\u{f1a0}"), // google
(Icon::Vid(0x1D6B), "\u{f17c}"), // linux foundation
(Icon::Vid(0x1d50), "\u{e771}"), // open source VID
(Icon::VidPid((0x1915, 0x520c)), "\u{f00a3}"), // specialized
(Icon::VidPid((0x1915, 0x520d)), "\u{f00a3}"), // specialized
(Icon::VidPid((0x0483, 0x572B)), "\u{f00a3}"), // specialized
(Icon::Vid(0x046d), "\u{f87c}"), // logitech
(Icon::Vid(0x091e), "\u{e2a6}"), // garmin
(Icon::VidPid((0x1d50, 0x6018)), "\u{f188}"), // black magic probe
(Icon::Vid(0x1366), "\u{f188}"), // segger
(Icon::Vid(0xf1a0), "\u{f188}"), // arm
(Icon::VidPidMsb((0x0483, 0x37)), "\u{f188}"), // st-link
(Icon::VidPid((0x0483, 0xdf11)), "\u{f019}"), // STM DFU
(Icon::VidPid((0x1d50, 0x6017)), "\u{f188}"), // black magic probe DFU
(Icon::ClassifierSubProtocol((ClassCode::ApplicationSpecificInterface, 0x01, 0x01)), "\u{f188}"), // DFU
(Icon::ClassifierSubProtocol((ClassCode::WirelessController, 0x01, 0x01)), "\u{f188}"), // bluetooth DFU
(Icon::Vid(0x2341), "\u{f2db}"), // arduino
(Icon::Vid(0x239A), "\u{f2db}"), // adafruit
(Icon::Vid(0x2e8a), "\u{f315}"), // raspberry pi foundation
(Icon::Vid(0x0483), "\u{f2db}"), // stm
(Icon::Vid(0x1915), "\u{f2db}"), // nordic
(Icon::Vid(0x1fc9), "\u{f2db}"), // nxp
(Icon::Vid(0x1050), "\u{f805}"), // yubikey
(Icon::VidPid((0x18D1, 0x2D05)), "\u{e70e}"), // android dev
(Icon::VidPid((0x18D1, 0xd00d)), "\u{e70e}"), // android
(Icon::VidPid((0x1d50, 0x606f)), "\u{f191d}"), // candlelight_fw gs_can
(Icon::VidPidMsb((0x043e, 0x9a)), "\u{f878}"), // lg monitor
(Icon::VidPid((0x0781, 0xf7c9)), "\u{f878}"), // sandisk external disk
(Icon::Classifier(ClassCode::Audio), "\u{f001}"), //
(Icon::Classifier(ClassCode::Image), "\u{f03e}"), //
(Icon::Classifier(ClassCode::Video), "\u{f03d}"), //
(Icon::Classifier(ClassCode::Printer), "\u{f02f}"), //
// (Icon::Classifier(ClassCode::MassStorage), "\u{fc05}".into()),
(Icon::Classifier(ClassCode::Hub), "\u{f126}"), //
(Icon::Classifier(ClassCode::ContentSecurity), "\u{f805}"), //
(Icon::Classifier(ClassCode::SmartCart), "\u{f805}"), //
(Icon::Classifier(ClassCode::PersonalHealthcare), "\u{f21e}"), //
(Icon::Classifier(ClassCode::AudioVideo), "\u{fd3f}"), // ﴿
(Icon::Classifier(ClassCode::Billboard), "\u{f05a}"), //
(Icon::Classifier(ClassCode::I3CDevice), "\u{f493}"), //
(Icon::Classifier(ClassCode::Diagnostic), "\u{f489}"), //
(Icon::Classifier(ClassCode::WirelessController), "\u{f1eb}"), //
(Icon::Classifier(ClassCode::Miscellaneous), "\u{f074}"), //
(Icon::Classifier(ClassCode::CDCCommunications), "\u{e795}"), // serial
(Icon::Classifier(ClassCode::CDCData), "\u{e795}"), // serial
(Icon::Classifier(ClassCode::HID), "\u{f80b}"), //
(Icon::UndefinedClassifier, "\u{2636}"), //☶
])
};
}
impl IconTheme {
/// New theme with defaults
pub fn new() -> Self {
Default::default()
}
/// Get tree building icon checks `Self` for user `tree` and tries to find `icon` there, otherwise uses [`DEFAULT_UTF8_TREE`]
///
/// Also checks if user icon is valid for encoding, if not will return default for that encoding
pub fn get_tree_icon(&self, icon: &Icon, encoding: &Encoding) -> String {
// unwrap on DEFAULT_UTF8_TREE is ok here since should panic if missing from static list
if let Some(user_tree) = self.tree.as_ref() {
user_tree
.get(icon)
.map(|s| match encoding.str_is_valid(s) {
true => s.to_owned(),
false => get_default_tree_icon(icon, encoding),
})
.unwrap_or(get_default_tree_icon(icon, encoding))
} else {
get_default_tree_icon(icon, encoding)
}
}
/// Drill through [`DEFAULT_ICONS`] first looking for `VidPid` -> `VidPidMsb` -> `Vid` -> `UnknownVendor` -> ""
pub fn get_default_vidpid_icon(vid: u16, pid: u16) -> String {
// try vid pid first
DEFAULT_ICONS
.get(&Icon::VidPid((vid, pid)))
.unwrap_or(
DEFAULT_ICONS
.get(&Icon::VidPidMsb((vid, (pid >> 8) as u8)))
.unwrap_or(
DEFAULT_ICONS
.get(&Icon::Vid(vid))
.unwrap_or(DEFAULT_ICONS.get(&Icon::UnknownVendor).unwrap_or(&"")),
),
)
.to_string()
}
/// Drill through `Self` `icons` if present first looking for `VidPid` -> `VidPidMsb` -> `Vid` -> `UnknownVendor` -> `get_default_vidpid_icon`
pub fn get_vidpid_icon(&self, vid: u16, pid: u16) -> String {
if let Some(user_icons) = self.user.as_ref() {
// try vid pid first
user_icons
.get(&Icon::VidPid((vid, pid)))
.unwrap_or(
user_icons
.get(&Icon::VidPidMsb((vid, (pid >> 8) as u8)))
.unwrap_or(
user_icons.get(&Icon::Vid(vid)).unwrap_or(
user_icons
.get(&Icon::UnknownVendor)
.unwrap_or(&IconTheme::get_default_vidpid_icon(vid, pid)),
),
),
)
.to_owned()
} else {
IconTheme::get_default_vidpid_icon(vid, pid)
}
}
/// Get icon for device from static default lookup
pub fn get_default_device_icon(d: &USBDevice) -> String {
if let (Some(vid), Some(pid)) = (d.vendor_id, d.product_id) {
IconTheme::get_default_vidpid_icon(vid, pid)
} else {
String::new()
}
}
/// Get icon for USBDevice `d` by checking `Self` using 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) {
self.get_vidpid_icon(vid, pid)
} else {
String::new()
}
}
/// Get icon for USBBus `d` by checking `Self` using PCI Vendor and PCI Device
pub fn get_bus_icon(&self, d: &USBBus) -> String {
if let (Some(vid), Some(pid)) = (d.pci_vendor, d.pci_device) {
self.get_vidpid_icon(vid, pid)
} else {
String::new()
}
}
/// Drill through `DEFAULT_ICONS` first looking for `ClassifierSubProtocol` -> `Classifier` -> `UndefinedClassifier` -> ""
pub fn get_default_classifier_icon(class: &ClassCode, sub: u8, protocol: u8) -> String {
// try vid pid first
DEFAULT_ICONS
.get(&Icon::ClassifierSubProtocol((
class.to_owned(),
sub,
protocol,
)))
.unwrap_or(
DEFAULT_ICONS
.get(&Icon::Classifier(class.to_owned()))
.unwrap_or(DEFAULT_ICONS.get(&Icon::UndefinedClassifier).unwrap_or(&"")),
)
.to_string()
}
/// Drill through `Self` icons first looking for `ClassifierSubProtocol` -> `Classifier` -> `UndefinedClassifier` -> get_default_classifier_icon
pub fn get_classifier_icon(&self, class: &ClassCode, sub: u8, protocol: u8) -> String {
if let Some(user_icons) = self.user.as_ref() {
user_icons
.get(&Icon::ClassifierSubProtocol((
class.to_owned(),
sub,
protocol,
)))
.unwrap_or(
user_icons
.get(&Icon::Classifier(class.to_owned()))
.unwrap_or(&IconTheme::get_default_classifier_icon(
class, sub, protocol,
)),
)
.to_owned()
} else {
IconTheme::get_default_classifier_icon(class, sub, protocol)
}
}
}
/// Gets tree icon from [`DEFAULT_UTF8_TREE`] or [`DEFAULT_ASCII_TREE`] (depanding on [`Encoding`]) as `String` with `unwrap` because should panic if missing from there
pub fn get_default_tree_icon(i: &Icon, encoding: &Encoding) -> String {
match encoding {
Encoding::Utf8 | Encoding::Glyphs => DEFAULT_UTF8_TREE.get(i).unwrap().to_string(),
Encoding::Ascii => DEFAULT_ASCII_TREE.get(i).unwrap().to_string(),
}
}
/// Gets tree icon from [`DEFAULT_ASCII_TREE`] as `String` with `unwrap` because should panic if missing from there
pub fn get_ascii_tree_icon(i: &Icon) -> String {
DEFAULT_ASCII_TREE.get(i).unwrap().to_string()
}
/// Returns clone of lazy_static defaults
pub fn defaults() -> HashMap<Icon, &'static str> {
DEFAULT_ICONS.clone()
}
/// Returns example list of icons with all [`Icon`] types
pub fn example() -> HashMap<Icon, String> {
HashMap::from([
(Icon::UnknownVendor, "\u{f287}".into()), // usb plug default
(Icon::Vid(0x05ac), "\u{f179}".into()), // apple
(Icon::VidPid((0x1d50, 0x6018)), "\u{f188}".into()), // black magic probe
(Icon::VidPidMsb((0x0483, 0x37)), "\u{f188}".into()), // st-link
(
Icon::ClassifierSubProtocol((ClassCode::ApplicationSpecificInterface, 0x01, 0x01)),
"\u{f188}".into(),
), // DFU
(Icon::Vid(0x2e8a), "\u{f315}".into()), // raspberry pi foundation
(
Icon::Classifier(ClassCode::CDCCommunications),
"\u{e795}".into(),
), // serial
(Icon::UndefinedClassifier, "\u{2636}".into()), //☶
])
}
/// Returns example theme with [`Icon`] types and default tree
pub fn example_theme() -> IconTheme {
let tree_strings: HashMap<Icon, String> = DEFAULT_UTF8_TREE
.iter()
.map(|(k, v)| (k.to_owned(), v.to_string()))
.collect();
IconTheme {
user: Some(example()),
tree: Some(tree_strings),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serialize_theme() {
let theme = IconTheme {
user: Some(HashMap::from([
(Icon::UnknownVendor, "\u{f287}".into()), // usb plug default
])),
..Default::default()
};
assert_eq!(
serde_json::to_string(&theme).unwrap(),
"{\"user\":{\"unknown-vendor\":\"\"},\"tree\":null}"
);
}
#[test]
fn test_deserialize_theme() {
let theme: IconTheme =
serde_json::from_str("{\"user\":{\"unknown-vendor\":\"\"},\"tree\":null}").unwrap();
let actual_theme = IconTheme {
user: Some(HashMap::from([
(Icon::UnknownVendor, "\u{f287}".into()), // usb plug default
])),
..Default::default()
};
assert_eq!(theme, actual_theme);
}
#[test]
fn test_serialize_defaults() {
serde_json::to_string(&defaults()).unwrap();
}
#[test]
fn test_serialize_example() {
println!("{}", serde_json::to_string_pretty(&example()).unwrap());
}
#[test]
fn test_deserialize_icon_tuples() {
let item: (Icon, &'static str) = (Icon::VidPid((0x1d50, 0x6018)), "\u{f188}");
let item_ser = serde_json::to_string(&item).unwrap();
assert_eq!(item_ser, r#"["vid-pid#1d50:6018",""]"#);
let item: (Icon, &'static str) = (Icon::Endpoint(Direction::In), ">");
let item_ser = serde_json::to_string(&item).unwrap();
assert_eq!(item_ser, r#"["endpoint_in",">"]"#);
let item: (Icon, &'static str) = (
Icon::ClassifierSubProtocol((ClassCode::HID, 0x01, 0x0a)),
"K",
);
let item_ser = serde_json::to_string(&item).unwrap();
assert_eq!(item_ser, r#"["classifier-sub-protocol#03:01:0a","K"]"#);
}
#[test]
fn icon_from_str() {
let str = "vid#1d50";
let icon = Icon::from_str(str);
assert_eq!(icon.unwrap(), Icon::Vid(7504));
let str = "vid-pid#1d50:6018";
let icon = Icon::from_str(str);
assert_eq!(icon.unwrap(), Icon::VidPid((7504, 24600)));
let str = "classifier#03";
let icon = Icon::from_str(str);
assert_eq!(icon.unwrap(), Icon::Classifier(ClassCode::HID));
let str = "classifier-sub-protocol#03:01:0a";
let icon = Icon::from_str(str);
assert_eq!(
icon.unwrap(),
Icon::ClassifierSubProtocol((ClassCode::HID, 1, 10))
);
let str = "endpoint_in";
let icon = Icon::from_str(str);
assert_eq!(icon.unwrap(), Icon::Endpoint(Direction::In));
let str = "unknown-vendor";
let icon = Icon::from_str(str);
assert_eq!(icon.unwrap(), Icon::UnknownVendor);
}
}