cyme 3.0.1

List system USB buses and devices. A modern cross-platform lsusb
Documentation
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
//! Colouring of cyme output
use colored::*;
use serde::{Deserialize, Deserializer, Serialize};
use std::fmt;

/// Colours [`crate::display::Block`] fields based on loose typing of field type
///
/// Considered using HashMap with Colouring Enum like IconTheme but this seemed to suit better, it is less flexible though...
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields)]
pub struct ColourTheme {
    /// Colour to use for name from descriptor
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub name: Option<Color>,
    /// Colour to use for serial from descriptor
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub serial: Option<Color>,
    /// Colour to use for manufacturer from descriptor
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub manufacturer: Option<Color>,
    /// Colour to use for driver from udev
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub driver: Option<Color>,
    /// Colour to use for general String data
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub string: Option<Color>,
    /// Colour to use for icons
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub icon: Option<Color>,
    /// Colour to use for location data
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub location: Option<Color>,
    /// Colour to use for path data
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub path: Option<Color>,
    /// Colour to use for general number values
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub number: Option<Color>,
    /// Colour to use for speed
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub speed: Option<Color>,
    /// Colour to use for Vendor ID
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub vid: Option<Color>,
    /// Colour to use for Product ID
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub pid: Option<Color>,
    /// Colour to use for generic BaseClass
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub class_code: Option<Color>,
    /// Colour to use for SubCodes
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub sub_code: Option<Color>,
    /// Colour to use for protocol
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub protocol: Option<Color>,
    /// Colour to use for info/enum type
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub attributes: Option<Color>,
    /// Colour to use for power information
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub power: Option<Color>,
    /// Tree colour
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub tree: Option<Color>,
    /// Colour at prepended before printing `Bus`
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub tree_bus_start: Option<Color>,
    /// Colour printed at end of tree before printing `Device`
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub tree_bus_terminator: Option<Color>,
    /// Colour printed at end of tree before printing configuration
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub tree_configuration_terminator: Option<Color>,
    /// Colour printed at end of tree before printing interface
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub tree_interface_terminator: Option<Color>,
    /// Colour for endpoint in before print
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub tree_endpoint_in: Option<Color>,
    /// Colour for endpoint out before print
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub tree_endpoint_out: Option<Color>,
    /// Colour used when muting a line, e.g. for hub devices with mute-hubs
    #[serde(
        default,
        serialize_with = "color_serializer",
        deserialize_with = "deserialize_option_color_from_string"
    )]
    pub muted: Option<Color>,
}

fn deserialize_option_color_from_string<'de, D>(deserializer: D) -> Result<Option<Color>, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum ColorOrNull<'a> {
        Str(&'a str),
        #[serde(deserialize_with = "deserialize_color")]
        FromStr(Color),
        Null,
    }

    match ColorOrNull::deserialize(deserializer)? {
        ColorOrNull::Str(s) => match s {
            "" => Ok(None),
            _ => Ok(Some(deserialize_color(
                serde::de::IntoDeserializer::into_deserializer(s),
            )?)),
        },
        ColorOrNull::FromStr(i) => Ok(Some(i)),
        ColorOrNull::Null => Ok(None),
    }
}

// Custom color deserialize, adapted from: https://github.com/Peltoche/lsd/blob/master/src/theme/color.rs
fn deserialize_color<'de, D>(deserializer: D) -> Result<Color, D::Error>
where
    D: serde::de::Deserializer<'de>,
{
    struct ColorVisitor;
    impl<'de> serde::de::Visitor<'de> for ColorVisitor {
        type Value = Color;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("colour string, '#rgb' or `3 u8 RGB array`")
        }

        fn visit_str<E>(self, value: &str) -> Result<Color, E>
        where
            E: serde::de::Error,
        {
            if let Some(stripped) = value.strip_prefix('$') {
                let num = u8::from_str_radix(stripped, 16).map_err(|_| {
                    serde::de::Error::custom(format!(
                        "Invalid ANSI color code: {}. Expected format is '$XX' where XX is a hex number between 00 and FF.",
                        value
                    ))
                })?;
                Ok(Color::AnsiColor(num))
            } else {
                Ok(Color::from(value))
            }
        }

        fn visit_seq<M>(self, mut seq: M) -> Result<Color, M::Error>
        where
            M: serde::de::SeqAccess<'de>,
        {
            let mut values = Vec::new();
            if let Some(size) = seq.size_hint() {
                if size != 3 {
                    return Err(serde::de::Error::invalid_length(
                        size,
                        &"a list of size 3(RGB)",
                    ));
                }
            }
            loop {
                match seq.next_element::<u8>() {
                    Ok(Some(x)) => {
                        values.push(x);
                    }
                    Ok(None) => break,
                    Err(e) => {
                        return Err(e);
                    }
                }
            }
            // recheck as size_hint sometimes not working
            if values.len() != 3 {
                return Err(serde::de::Error::invalid_length(
                    values.len(),
                    &"A u8 list of size 3: [R, G, B]",
                ));
            }
            Ok(Color::TrueColor {
                r: values[0],
                g: values[1],
                b: values[2],
            })
        }
    }

    deserializer.deserialize_any(ColorVisitor)
}

fn color_to_string(color: Color) -> String {
    match color {
        Color::Black => "black".into(),
        Color::Red => "red".into(),
        Color::Green => "green".into(),
        Color::Yellow => "yellow".into(),
        Color::Blue => "blue".into(),
        Color::Magenta => "magenta".into(),
        Color::Cyan => "cyan".into(),
        Color::White => "white".into(),
        Color::BrightBlack => "bright black".into(),
        Color::BrightRed => "bright red".into(),
        Color::BrightGreen => "bright green".into(),
        Color::BrightYellow => "bright yellow".into(),
        Color::BrightBlue => "bright blue".into(),
        Color::BrightMagenta => "bright magenta".into(),
        Color::BrightCyan => "bright cyan".into(),
        Color::BrightWhite => "bright white".into(),
        Color::AnsiColor(n) => format!("${:02X}", n),
        Color::TrueColor { r, g, b } => format!("#{:02X}{:02X}{:02X}", r, g, b),
    }
}

/// Have to make this because external crate does not impl Display
fn color_serializer<S>(color: &Option<Color>, s: S) -> Result<S::Ok, S::Error>
where
    S: serde::ser::Serializer,
{
    match color {
        Some(c) => s.serialize_str(&color_to_string(*c)),
        None => s.serialize_none(),
    }
}

impl Default for ColourTheme {
    fn default() -> Self {
        ColourTheme::new()
    }
}

impl ColourTheme {
    /// New theme with defaults
    pub fn new() -> Self {
        ColourTheme {
            name: Some(Color::BrightBlue),
            serial: Some(Color::Green),
            manufacturer: Some(Color::Blue),
            driver: Some(Color::BrightMagenta),
            string: Some(Color::Blue),
            icon: None,
            location: Some(Color::Magenta),
            path: Some(Color::BrightCyan),
            number: Some(Color::Cyan),
            speed: Some(Color::Magenta),
            vid: Some(Color::BrightYellow),
            pid: Some(Color::Yellow),
            class_code: Some(Color::BrightYellow),
            sub_code: Some(Color::Yellow),
            protocol: Some(Color::Yellow),
            attributes: Some(Color::Magenta),
            power: Some(Color::Red),
            tree: Some(Color::BrightBlack),
            tree_bus_start: Some(Color::BrightBlack),
            tree_bus_terminator: Some(Color::BrightBlack),
            tree_configuration_terminator: Some(Color::BrightBlack),
            tree_interface_terminator: Some(Color::BrightBlack),
            tree_endpoint_in: Some(Color::Yellow),
            tree_endpoint_out: Some(Color::Magenta),
            muted: Some(Color::BrightBlack),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_serialize_color_theme() {
        let ct: ColourTheme = ColourTheme::new();
        println!("{}", serde_json::to_string_pretty(&ct).unwrap());
    }

    #[test]
    fn test_deserialize_color_theme() {
        let ct: ColourTheme = serde_json::from_str(r#"{"name": "blue"}"#).unwrap();
        assert_eq!(ct.name, Some(Color::Blue));
    }

    #[test]
    fn test_serialize_deserialize_color_theme() {
        let ct: ColourTheme = ColourTheme::new();
        let ser = serde_json::to_string_pretty(&ct).unwrap();
        let ctrt: ColourTheme = serde_json::from_str(&ser).unwrap();
        assert_eq!(ct, ctrt);
    }

    #[test]
    fn test_deserialize_other_colors() {
        let ct: ColourTheme = serde_json::from_str(r##"{"name": "#FF5733"}"##).unwrap();
        assert_eq!(
            ct.name,
            Some(Color::TrueColor {
                r: 0xFF,
                g: 0x57,
                b: 0x33
            })
        );

        let ct2: ColourTheme = serde_json::from_str(r#"{"name": [255, 87, 51]}"#).unwrap();
        assert_eq!(
            ct2.name,
            Some(Color::TrueColor {
                r: 0xFF,
                g: 0x57,
                b: 0x33
            })
        );

        let ct3: ColourTheme = serde_json::from_str(r##"{"name": "$1A"}"##).unwrap();
        assert_eq!(ct3.name, Some(Color::AnsiColor(0x1A)));
    }
}