Skip to content

Commit

Permalink
Add conversion for ObjectDomain
Browse files Browse the repository at this point in the history
  • Loading branch information
aveenismail committed Nov 6, 2023
1 parent 8a16de8 commit af1983c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
29 changes: 28 additions & 1 deletion src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -678,6 +680,31 @@ impl ObjectDomain {
}
}

impl TryFrom<u16> for ObjectDomain {
type Error = Error;
fn try_from(value: u16) -> Result<Self, Self::Error> {
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 {
Expand Down

0 comments on commit af1983c

Please sign in to comment.