diff --git a/src/backend/native.rs b/src/backend/native.rs index 5d388e0..aaa331c 100644 --- a/src/backend/native.rs +++ b/src/backend/native.rs @@ -9,6 +9,33 @@ pub struct UsbInterface { interface: nusb::Interface, } +#[derive(PartialEq, Clone, Default)] +pub struct DeviceFilter { + pub vendor_id: Option, + pub product_id: Option, + pub class: Option, + pub subclass: Option, + pub protocol: Option, +} + +impl DeviceFilter { + pub fn new( + vendor_id: Option, + product_id: Option, + class: Option, + subclass: Option, + protocol: Option, + ) -> Self { + Self { + vendor_id, + product_id, + class, + subclass, + protocol, + } + } +} + pub async fn get_device(vendor_id: u16, product_id: u16) -> Result { let devices = nusb::list_devices().unwrap(); @@ -36,35 +63,6 @@ pub async fn get_device(vendor_id: u16, product_id: u16) -> Result, - pub product_id: Option, - pub class: Option, - pub subclass: Option, - pub protocol: Option, - serial_number: Option, -} - -impl DeviceFilter { - pub fn serial_number(&self) -> &Option { - &self.serial_number - } -} - -impl Default for DeviceFilter { - fn default() -> Self { - Self { - vendor_id: None, - product_id: None, - class: None, - subclass: None, - protocol: None, - serial_number: None, - } - } -} - pub async fn get_device_filter( device_filter: Vec, ) -> Result { @@ -97,10 +95,6 @@ pub async fn get_device_filter( result = info.protocol.unwrap() == prelim_dev_inf.protocol(); } - if info.serial_number().is_some() && prelim_dev_inf.serial_number().is_some() { - result = info.serial_number().as_ref().unwrap() == &prelim_dev_inf.serial_number().unwrap().to_owned(); - } - result } ).is_some() { diff --git a/src/backend/wasm.rs b/src/backend/wasm.rs index 81a3e1e..8b4f7aa 100644 --- a/src/backend/wasm.rs +++ b/src/backend/wasm.rs @@ -22,31 +22,29 @@ pub struct UsbInterface { } #[wasm_bindgen] -#[derive(PartialEq, Clone)] +#[derive(PartialEq, Clone, Default)] pub struct DeviceFilter { pub vendor_id: Option, pub product_id: Option, pub class: Option, pub subclass: Option, pub protocol: Option, - serial_number: Option, } impl DeviceFilter { - pub fn serial_number(&self) -> &Option { - &self.serial_number - } -} - -impl Default for DeviceFilter { - fn default() -> Self { + pub fn new( + vendor_id: Option, + product_id: Option, + class: Option, + subclass: Option, + protocol: Option, + ) -> Self { Self { - vendor_id: None, - product_id: None, - class: None, - subclass: None, - protocol: None, - serial_number: None, + vendor_id, + product_id, + class, + subclass, + protocol, } } } @@ -140,10 +138,6 @@ pub async fn get_device_filter( result = info.protocol.unwrap() == device.device_protocol(); } - if info.serial_number().is_some() && device.serial_number().is_some() { - result = info.serial_number().as_ref().unwrap() == &device.serial_number().unwrap().to_owned(); - } - result } ).is_some() { @@ -194,14 +188,6 @@ pub async fn get_device_filter( ) .unwrap(); } - if let Some(serial) = filter.serial_number { - js_sys::Reflect::set( - &js_filter, - &JsValue::from_str("serialNumber"), - &JsValue::from(serial), - ) - .unwrap(); - } arr.push(&js_filter); } diff --git a/src/lib.rs b/src/lib.rs index c203afc..198c8f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,8 +48,8 @@ pub use crate::context::UsbDevice; /// An implementation of a USB interface pub use crate::context::UsbInterface; -/// A single Device ID and Product ID pair to find when looking -/// for new USB devices using [get_device_filter] +/// Information about a USB device for finding it while trying +/// to look for new USB devices using [get_device_filter] #[doc(inline)] pub use crate::context::DeviceFilter; @@ -62,11 +62,12 @@ pub use crate::context::get_device; /// ## Example /// ```no_run /// # tokio_test::block_on(async { -/// use cross_usb::{get_device_filter, FilterTuple}; +/// use cross_usb::{get_device_filter, DeviceFilter, device_filter}; +/// /// /// let filter = vec![ -/// FilterTuple(0x054c, 0x00c9), -/// FilterTuple(0x054c, 0x0186), +/// device_filter!{vendor_id: 0x054c, product_id: 0x00c9}, +/// device_filter!{vendor_id: 0x054c}, /// ]; /// /// let device = get_device_filter(filter).await.expect("Could not find device in list"); @@ -74,3 +75,32 @@ pub use crate::context::get_device; /// ``` #[doc(inline)] pub use crate::context::get_device_filter; + +/// Macro to create a device filter easily from data. +/// +/// The only valid keys are fields of the [DeviceFilter] struct. +/// You may use as many or as few of them as you need, the rest +/// of the values will be filled with [None]. +/// +/// ## Usage +/// ``` +/// use cross_usb::device_filter; +/// +/// // Example with all fields filled +/// device_filter!{ +/// vendor_id: 0x054c, +/// product_id: 0x0186, +/// class: 0xFF, +/// subclass: 0x02, +/// protocol: 0x15, +/// }; +/// ``` +#[macro_export] +macro_rules! device_filter { + ($($field:ident: $val:expr),+ $(,)?) => { + cross_usb::DeviceFilter { + $($field: Some($val),)* + ..cross_usb::DeviceFilter::default() + } + } +}