lsusb/
args.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use argh::{FromArgValue, FromArgs};
6
7#[derive(Debug, PartialEq)]
8pub struct UsbDevice {
9    pub vendor_id: u16,
10    pub product_id: Option<u16>,
11}
12
13impl FromArgValue for UsbDevice {
14    fn from_arg_value(value: &str) -> Result<Self, String> {
15        if let Some((vid, pid)) = value.split_once(':') {
16            let vid = u16::from_str_radix(vid, 16)
17                .map_err(|_| "Vendor ID is not a valid hexadecimal number'.".to_string())?;
18
19            let pid = u16::from_str_radix(pid, 16)
20                .map_err(|_| "Product ID is not a valid hexadecimal number'.".to_string())?;
21
22            Ok(UsbDevice { vendor_id: vid, product_id: Some(pid) })
23        } else {
24            let vid = u16::from_str_radix(value, 16)
25                .map_err(|_| "Vendor id is not a valid hexadecimal number'.".to_string())?;
26
27            Ok(UsbDevice { vendor_id: vid, product_id: None })
28        }
29    }
30}
31
32#[derive(FromArgs, Debug, PartialEq)]
33/// Display USB information
34pub struct Args {
35    #[argh(switch, short = 't')]
36    /// prints USB device tree
37    pub tree: bool,
38    #[argh(switch, short = 'v')]
39    /// verbose output (prints descriptors)
40    pub verbose: bool,
41    #[argh(option, short = 'c')]
42    /// prints configuration descriptor for specified configuration (rather than
43    /// current configuration)
44    pub configuration: Option<u8>,
45    #[argh(option, short = 'd')]
46    /// shows only devices with the specified vendor and product ID numbers (in hexadecimal)
47    /// UsbDevice must be in format vendor[:product]
48    pub device: Option<UsbDevice>,
49}