diff --git a/shared/grpc_clients/src/error.rs b/shared/grpc_clients/src/error.rs --- a/shared/grpc_clients/src/error.rs +++ b/shared/grpc_clients/src/error.rs @@ -10,4 +10,6 @@ InvalidUri(InvalidUri), #[display(...)] GrpcStatus(Status), + #[display(fmt = "Invalid Device Type")] + InvalidDeviceType, } diff --git a/shared/grpc_clients/src/identity/device.rs b/shared/grpc_clients/src/identity/device.rs new file mode 100644 --- /dev/null +++ b/shared/grpc_clients/src/identity/device.rs @@ -0,0 +1,33 @@ +use super::protos::client::DeviceType; +use std::fmt::{Display, Formatter, Result as FmtResult}; + +use crate::error::Error; + +impl TryFrom for DeviceType { + type Error = crate::error::Error; + + fn try_from(value: i32) -> Result { + match value { + 0 => Ok(DeviceType::Keyserver), + 1 => Ok(DeviceType::Web), + 2 => Ok(DeviceType::Ios), + 3 => Ok(DeviceType::Android), + 4 => Ok(DeviceType::Windows), + 5 => Ok(DeviceType::MacOs), + _ => Err(Error::InvalidDeviceType), + } + } +} + +impl Display for DeviceType { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + match self { + DeviceType::Keyserver => write!(f, "keyserver"), + DeviceType::Web => write!(f, "web"), + DeviceType::Ios => write!(f, "ios"), + DeviceType::Android => write!(f, "android"), + DeviceType::Windows => write!(f, "windows"), + DeviceType::MacOs => write!(f, "macos"), + } + } +}