diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/hid.rs | 32 | ||||
-rw-r--r-- | src/main.rs | 3 | ||||
-rw-r--r-- | src/util.rs | 43 |
3 files changed, 57 insertions, 21 deletions
@@ -11,30 +11,20 @@ use usb_device::Result; const INTERFACE_CLASS_HID: u8 = 0x03; -#[derive(Clone, Copy, Debug, PartialEq)] -#[repr(u8)] -pub enum Subclass { - None = 0x00, - BootInterface = 0x01, -} - -impl From<Subclass> for u8 { - fn from(subclass: Subclass) -> u8 { - subclass as u8 +enum_u8! { + #[derive(Clone, Copy, Debug, PartialEq)] + pub enum Subclass { + None = 0x00, + BootInterface = 0x01, } } -#[derive(Clone, Copy, Debug, PartialEq)] -#[repr(u8)] -pub enum Protocol { - None = 0x00, - Keyboard = 0x01, - Mouse = 0x02, -} - -impl From<Protocol> for u8 { - fn from(protocol: Protocol) -> u8 { - protocol as u8 +enum_u8! { + #[derive(Clone, Copy, Debug, PartialEq)] + pub enum Protocol { + None = 0x00, + Keyboard = 0x01, + Mouse = 0x02, } } diff --git a/src/main.rs b/src/main.rs index b0ecafd..3857967 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,9 @@ extern crate panic_halt; +#[macro_use] +mod util; + mod device; mod hid; diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..4fc4448 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,43 @@ +// Copyright 2019 Robin Krahl <robin.krahl@ireas.org> +// SPDX-License-Identifier: GPL-3.0-or-later + +use core::marker::Sized; + +macro_rules! enum_u8 { + ( + $(#[$outer:meta])* + pub enum $name:ident { + $($var:ident = $num:expr),+ + $(,)* + } + ) => { + $(#[$outer])* + #[repr(u8)] + pub enum $name { + $( + $var = $num, + )* + } + + impl crate::util::TryFrom<u8> for $name { + fn try_from(val: u8) -> ::core::result::Result<Self, ()> { + match val { + $( + $num => Ok($name::$var), + )* + _ => Err(()) + } + } + } + + impl From<$name> for u8 { + fn from(val: $name) -> u8 { + val as u8 + } + } + }; +} + +pub trait TryFrom<T>: Sized { + fn try_from(val: T) -> Result<Self, ()>; +} |