From 6f71e5513a9a452c0dfdf6808e0d2fb7a775162e Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Fri, 9 Aug 2019 12:44:43 -0400 Subject: Add TryFrom impl for DescriptorType. --- src/descriptor.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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 for DescriptorType { + type Error = &'static str; + + fn try_from(v: u8) -> Result { + 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 { -- cgit v1.2.3