diff --git a/src/lib.rs b/src/lib.rs index 7e2098b..d56e90c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -88,7 +88,7 @@ impl Display for DeviceInfo { let mut info = String::new().to_owned(); info.push_str(format!("Version number:\t\t {}.{}.{}\n", self.major, self.minor, self.patch).as_str()); info.push_str(format!("Serial number:\t\t {}\n", self.serial).as_str()); - info.push_str(format!("Log used:\t\t {}/{}\n", self.log_used, self.log_total).as_str()); + info.push_str(format!("Log used:\t\t\t {}/{}\n", self.log_used, self.log_total).as_str()); let mut algo_str = String::new().to_owned(); self.algorithms.iter().for_each(|a| algo_str.push_str(format!("{},", ObjectAlgorithm::from(a)).as_str())); diff --git a/src/object.rs b/src/object.rs index 7ee6af4..2832a39 100644 --- a/src/object.rs +++ b/src/object.rs @@ -22,6 +22,7 @@ use lyh::{yh_algorithm, yh_capabilities, yh_object_descriptor, yh_object_type}; use error::Error; use std::collections::HashMap; +use std::convert::TryFrom; use std::str::FromStr; use std::fmt; use std::fmt::Display; @@ -152,10 +153,11 @@ pub enum ObjectCapability { DeleteOtpAeadKey, } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] /// Object domains pub enum ObjectDomain { /// Domain one + #[default] One, /// Domain two Two, @@ -678,6 +680,31 @@ impl ObjectDomain { } } +impl TryFrom for ObjectDomain { + type Error = Error; + fn try_from(value: u16) -> Result { + match value { + 1 => Ok(ObjectDomain::One), + 2 => Ok(ObjectDomain::Two), + 3 => Ok(ObjectDomain::Three), + 4 => Ok(ObjectDomain::Four), + 5 => Ok(ObjectDomain::Five), + 6 => Ok(ObjectDomain::Six), + 7 => Ok(ObjectDomain::Seven), + 8 => Ok(ObjectDomain::Eight), + 9 => Ok(ObjectDomain::Nine), + 10 => Ok(ObjectDomain::Ten), + 11 => Ok(ObjectDomain::Eleven), + 12 => Ok(ObjectDomain::Twelve), + 13 => Ok(ObjectDomain::Thirteen), + 14 => Ok(ObjectDomain::Fourteen), + 15 => Ok(ObjectDomain::Fifteen), + 16 => Ok(ObjectDomain::Sixteen), + _ => Err(Error::InvalidParameter("There are only domains 1 to 16".to_string())) + } + } +} + impl Display for ObjectDomain { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self {