use argh::{FromArgValue, FromArgs};
#[derive(Debug, PartialEq)]
pub struct UsbDevice {
pub vendor_id: u16,
pub product_id: Option<u16>,
}
impl FromArgValue for UsbDevice {
fn from_arg_value(value: &str) -> Result<Self, String> {
if let Some((vid, pid)) = value.split_once(':') {
let vid = u16::from_str_radix(vid, 16)
.map_err(|_| "Vendor ID is not a valid hexadecimal number'.".to_string())?;
let pid = u16::from_str_radix(pid, 16)
.map_err(|_| "Product ID is not a valid hexadecimal number'.".to_string())?;
Ok(UsbDevice { vendor_id: vid, product_id: Some(pid) })
} else {
let vid = u16::from_str_radix(value, 16)
.map_err(|_| "Vendor id is not a valid hexadecimal number'.".to_string())?;
Ok(UsbDevice { vendor_id: vid, product_id: None })
}
}
}
#[derive(FromArgs, Debug, PartialEq)]
pub struct Args {
#[argh(switch, short = 't')]
pub tree: bool,
#[argh(switch, short = 'v')]
pub verbose: bool,
#[argh(option, short = 'c')]
pub configuration: Option<u8>,
#[argh(option, short = 'd')]
pub device: Option<UsbDevice>,
}