Add Usage enum for all pages in usb_hid_usage

This commit is contained in:
David Hoppenbrouwers
2022-09-09 21:32:01 +02:00
parent 5e4b2fdb1d
commit 1cef2ec5ef
2 changed files with 18 additions and 1 deletions

View File

@ -2,7 +2,7 @@ use core::num::NonZeroU16;
pub const PAGE: u16 = 0x09;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Usage {
NoButton,
Button(NonZeroU16),

View File

@ -48,6 +48,23 @@ macro_rules! page {
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Usage {
$($v($m::Usage),)*
}
impl TryFrom<(u16, u16)> for Usage {
type Error = UnknownUsage;
fn try_from((page, usage): (u16, u16)) -> Result<Self, Self::Error> {
Ok(match page {
$($m::PAGE => Self::$v($m::Usage::try_from(usage).map_err(|_| UnknownUsage)?),)*
_ => return Err(UnknownUsage),
})
}
}
};
}