aboutsummaryrefslogtreecommitdiffstats
path: root/src/descriptor.rs
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2019-08-09 12:44:43 -0400
committerBrian Cully <bjc@kublai.com>2019-08-09 12:44:43 -0400
commit6f71e5513a9a452c0dfdf6808e0d2fb7a775162e (patch)
treea529dbcc67e00773aa0c40d7e16938d422d170e3 /src/descriptor.rs
parentb255613f3792ec6dc1c57876e59d95c33538b607 (diff)
downloadusb-host-6f71e5513a9a452c0dfdf6808e0d2fb7a775162e.tar.gz
usb-host-6f71e5513a9a452c0dfdf6808e0d2fb7a775162e.zip
Add TryFrom impl for DescriptorType.
Diffstat (limited to 'src/descriptor.rs')
-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 {