Skip to content

Commit

Permalink
[identity] Replace usage of Device enum with DeviceType
Browse files Browse the repository at this point in the history
Summary:
We can replace the usage of Device in database.rs and client_service.rs with the code generated DeviceType enum.
This reduces code duplication as we have implemented Display and TryFrom traits in shared.

Test Plan: Wrote basic unit tests utilizing try_from in database.rs and cargo builds

Reviewers: varun, jon

Reviewed By: varun

Subscribers: ashoat, tomek

Differential Revision: https://phab.comm.dev/D9474
  • Loading branch information
wyilio committed Oct 18, 2023
1 parent 637d661 commit 02ca5ba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 44 deletions.
14 changes: 8 additions & 6 deletions services/identity/src/client_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ use crate::client_service::client_proto::{
VerifyUserAccessTokenResponse, WalletLoginRequest, WalletLoginResponse,
};
use crate::config::CONFIG;
use crate::database::{DatabaseClient, Device, KeyPayload};
use crate::database::{
DBDeviceTypeInt, DatabaseClient, DeviceType, KeyPayload,
};
use crate::error::Error as DBError;
use crate::grpc_utils::DeviceInfoWithAuth;
use crate::id::generate_uuid;
Expand Down Expand Up @@ -84,7 +86,7 @@ pub struct FlattenedDeviceKeyUpload {
pub notif_prekey: String,
pub notif_prekey_signature: String,
pub notif_one_time_keys: Vec<String>,
pub device_type: Device,
pub device_type: DeviceType,
}

#[derive(derive_more::Constructor)]
Expand Down Expand Up @@ -168,7 +170,7 @@ impl IdentityClientService for ClientService {
notif_prekey,
notif_prekey_signature,
notif_one_time_keys: one_time_notif_prekeys,
device_type: Device::try_from(device_type)
device_type: DeviceType::try_from(DBDeviceTypeInt(device_type))
.map_err(handle_db_error)?,
},
};
Expand Down Expand Up @@ -271,7 +273,7 @@ impl IdentityClientService for ClientService {
notif_prekey,
notif_prekey_signature,
notif_one_time_keys: one_time_notif_prekeys,
device_type: Device::try_from(device_type)
device_type: DeviceType::try_from(DBDeviceTypeInt(device_type))
.map_err(handle_db_error)?,
},
};
Expand Down Expand Up @@ -508,7 +510,7 @@ impl IdentityClientService for ClientService {
notif_prekey,
notif_prekey_signature,
notif_one_time_keys: one_time_notif_prekeys,
device_type: Device::try_from(device_type)
device_type: DeviceType::try_from(DBDeviceTypeInt(device_type))
.map_err(handle_db_error)?,
},
};
Expand Down Expand Up @@ -650,7 +652,7 @@ impl IdentityClientService for ClientService {
notif_prekey,
notif_prekey_signature,
notif_one_time_keys: one_time_notif_prekeys,
device_type: Device::try_from(device_type)
device_type: DeviceType::try_from(DBDeviceTypeInt(device_type))
.map_err(handle_db_error)?,
},
social_proof,
Expand Down
59 changes: 21 additions & 38 deletions services/identity/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use constant_time_eq::constant_time_eq;
use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::str::FromStr;
use std::sync::Arc;

Expand Down Expand Up @@ -48,6 +47,7 @@ use crate::error::{AttributeValueFromHashMap, FromAttributeValue};
use crate::id::generate_uuid;
use crate::nonce::NonceData;
use crate::token::{AccessTokenData, AuthType};
pub use grpc_clients::identity::DeviceType;

#[derive(Serialize, Deserialize)]
pub struct OlmKeys {
Expand All @@ -72,49 +72,22 @@ impl FromStr for KeyPayload {
}
}

#[derive(Clone, Copy)]
#[allow(non_camel_case_types)]
pub enum Device {
// Numeric values should match the protobuf definition
Keyserver = 0,
Web,
Ios,
Android,
Windows,
MacOS,
}
pub struct DBDeviceTypeInt(pub i32);

impl TryFrom<i32> for Device {
impl TryFrom<DBDeviceTypeInt> for DeviceType {
type Error = crate::error::Error;

fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
0 => Ok(Device::Keyserver),
1 => Ok(Device::Web),
2 => Ok(Device::Ios),
3 => Ok(Device::Android),
4 => Ok(Device::Windows),
5 => Ok(Device::MacOS),
_ => Err(Error::Attribute(DBItemError {
fn try_from(value: DBDeviceTypeInt) -> Result<Self, Self::Error> {
let device_result = DeviceType::try_from(value.0);

device_result.map_err(|_| {
Error::Attribute(DBItemError {
attribute_name: USERS_TABLE_DEVICES_MAP_DEVICE_TYPE_ATTRIBUTE_NAME
.to_string(),
attribute_value: Some(AttributeValue::N(value.to_string())),
attribute_value: Some(AttributeValue::N(value.0.to_string())),
attribute_error: DBItemAttributeError::InvalidValue,
})),
}
}
}

impl Display for Device {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match self {
Device::Keyserver => write!(f, "keyserver"),
Device::Web => write!(f, "web"),
Device::Ios => write!(f, "ios"),
Device::Android => write!(f, "android"),
Device::Windows => write!(f, "windows"),
Device::MacOS => write!(f, "macos"),
}
})
})
}
}

Expand Down Expand Up @@ -1586,4 +1559,14 @@ mod tests {
"DYmV8VdkjwG/VtC8C53morogNJhpTPT/4jzW0/cxzQo"
);
}

#[test]
fn test_int_to_device_type() {
let valid_result = DeviceType::try_from(3);
assert!(valid_result.is_ok());
assert_eq!(valid_result.unwrap(), DeviceType::Android);

let invalid_result = DeviceType::try_from(6);
assert!(invalid_result.is_err());
}
}

0 comments on commit 02ca5ba

Please sign in to comment.