aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/descriptor.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/descriptor.rs b/src/descriptor.rs
index c0d3314..9068c15 100644
--- a/src/descriptor.rs
+++ b/src/descriptor.rs
@@ -1,5 +1,6 @@
+use core::convert::TryFrom;
+
#[derive(Clone, Copy, Debug)]
-#[repr(u8)]
pub enum DescriptorType {
Device = 1,
Configuration = 2,
@@ -11,6 +12,24 @@ pub enum DescriptorType {
InterfacePower = 8,
}
+impl TryFrom<u8> for DescriptorType {
+ type Error = &'static str;
+
+ fn try_from(v: u8) -> Result<Self, Self::Error> {
+ match v {
+ 1 => Ok(Self::Device),
+ 2 => Ok(Self::Configuration),
+ 3 => Ok(Self::String),
+ 4 => Ok(Self::Interface),
+ 5 => Ok(Self::Endpoint),
+ 6 => Ok(Self::DeviceQualifier),
+ 7 => Ok(Self::OtherSpeed),
+ 8 => Ok(Self::InterfacePower),
+ _ => Err("invalid descriptor"),
+ }
+ }
+}
+
#[derive(Copy, Clone, Debug)]
#[repr(C, packed)]
pub struct DeviceDescriptor {