@@ -7,7 +7,10 @@ use comm_lib::aws::ddb::{
77 types:: { AttributeValue , PutRequest , ReturnConsumedCapacity , WriteRequest } ,
88} ;
99use comm_lib:: aws:: { AwsConfig , DynamoDBClient } ;
10- use comm_lib:: database:: { AttributeMap , DBItemAttributeError , DBItemError } ;
10+ use comm_lib:: database:: {
11+ AttributeExtractor , AttributeMap , DBItemAttributeError , DBItemError ,
12+ TryFromAttribute ,
13+ } ;
1114use constant_time_eq:: constant_time_eq;
1215use std:: collections:: { HashMap , HashSet } ;
1316use std:: str:: FromStr ;
@@ -723,7 +726,7 @@ impl DatabaseClient {
723726 item : Some ( mut item) ,
724727 ..
725728 } ) => {
726- let created = parse_date_time_attribute (
729+ let created = DateTime :: < Utc > :: try_from_attr (
727730 ACCESS_TOKEN_TABLE_CREATED_ATTRIBUTE ,
728731 item. remove ( ACCESS_TOKEN_TABLE_CREATED_ATTRIBUTE ) ,
729732 ) ?;
@@ -978,15 +981,13 @@ impl DatabaseClient {
978981 mut user : AttributeMap ,
979982 get_one_time_keys : bool ,
980983 ) -> Result < Option < Devices > , Error > {
981- let devices = parse_map_attribute (
982- USERS_TABLE_DEVICES_ATTRIBUTE ,
983- user. remove ( USERS_TABLE_DEVICES_ATTRIBUTE ) ,
984- ) ?;
984+ let devices: AttributeMap =
985+ user. take_attr ( USERS_TABLE_DEVICES_ATTRIBUTE ) ?;
985986
986987 let mut devices_response = HashMap :: with_capacity ( devices. len ( ) ) ;
987988 for ( device_id_key, device_info) in devices {
988989 let device_info_map =
989- parse_map_attribute ( & device_id_key, Some ( device_info) ) ?;
990+ AttributeMap :: try_from_attr ( & device_id_key, Some ( device_info) ) ?;
990991
991992 let mut device_info_string_map = HashMap :: new ( ) ;
992993 for ( attribute_name, attribute_value) in device_info_map {
@@ -1000,7 +1001,7 @@ impl DatabaseClient {
10001001 }
10011002
10021003 let attribute_value_str =
1003- parse_string_attribute ( & attribute_name, Some ( attribute_value) ) ?;
1004+ String :: try_from_attr ( & attribute_name, Some ( attribute_value) ) ?;
10041005 device_info_string_map. insert ( attribute_name, attribute_value_str) ;
10051006 }
10061007
@@ -1036,12 +1037,10 @@ impl DatabaseClient {
10361037 . get_user_from_user_info ( user_info. clone ( ) , auth_type)
10371038 . await
10381039 {
1039- Ok ( Some ( mut user) ) => parse_string_attribute (
1040- USERS_TABLE_PARTITION_KEY ,
1041- user. remove ( USERS_TABLE_PARTITION_KEY ) ,
1042- )
1043- . map ( Some )
1044- . map_err ( Error :: Attribute ) ,
1040+ Ok ( Some ( mut user) ) => user
1041+ . take_attr ( USERS_TABLE_PARTITION_KEY )
1042+ . map ( Some )
1043+ . map_err ( Error :: Attribute ) ,
10451044 Ok ( _) => Ok ( None ) ,
10461045 Err ( e) => Err ( e) ,
10471046 }
@@ -1056,10 +1055,7 @@ impl DatabaseClient {
10561055 . await
10571056 {
10581057 Ok ( Some ( mut user) ) => {
1059- let user_id = parse_string_attribute (
1060- USERS_TABLE_PARTITION_KEY ,
1061- user. remove ( USERS_TABLE_PARTITION_KEY ) ,
1062- ) ?;
1058+ let user_id = user. take_attr ( USERS_TABLE_PARTITION_KEY ) ?;
10631059 let password_file = parse_registration_data_attribute (
10641060 user. remove ( USERS_TABLE_REGISTRATION_ATTRIBUTE ) ,
10651061 ) ?;
@@ -1115,10 +1111,9 @@ impl DatabaseClient {
11151111 let mut result = Vec :: new ( ) ;
11161112 if let Some ( attributes) = scan_output. items {
11171113 for mut attribute in attributes {
1118- if let Ok ( username) = parse_string_attribute (
1119- USERS_TABLE_USERNAME_ATTRIBUTE ,
1120- attribute. remove ( USERS_TABLE_USERNAME_ATTRIBUTE ) ,
1121- ) {
1114+ if let Ok ( username) =
1115+ attribute. take_attr ( USERS_TABLE_USERNAME_ATTRIBUTE )
1116+ {
11221117 result. push ( username) ;
11231118 }
11241119 }
@@ -1178,19 +1173,14 @@ impl DatabaseClient {
11781173 return Ok ( None ) ;
11791174 } ;
11801175
1181- let nonce = parse_string_attribute (
1182- NONCE_TABLE_PARTITION_KEY ,
1183- item. remove ( & NONCE_TABLE_PARTITION_KEY . to_string ( ) ) ,
1184- ) ?;
1185-
1186- let created = parse_date_time_attribute (
1176+ let nonce = item. take_attr ( NONCE_TABLE_PARTITION_KEY ) ?;
1177+ let created = DateTime :: < Utc > :: try_from_attr (
11871178 NONCE_TABLE_CREATED_ATTRIBUTE ,
1188- item. remove ( & NONCE_TABLE_CREATED_ATTRIBUTE . to_string ( ) ) ,
1179+ item. remove ( NONCE_TABLE_CREATED_ATTRIBUTE ) ,
11891180 ) ?;
1190-
1191- let expiration_time = parse_date_time_attribute (
1181+ let expiration_time = DateTime :: < Utc > :: try_from_attr (
11921182 NONCE_TABLE_EXPIRATION_TIME_ATTRIBUTE ,
1193- item. remove ( & NONCE_TABLE_EXPIRATION_TIME_ATTRIBUTE . to_string ( ) ) ,
1183+ item. remove ( NONCE_TABLE_EXPIRATION_TIME_ATTRIBUTE ) ,
11941184 ) ?;
11951185
11961186 Ok ( Some ( NonceData {
@@ -1326,27 +1316,6 @@ fn create_composite_primary_key(
13261316 primary_key
13271317}
13281318
1329- fn parse_date_time_attribute (
1330- attribute_name : & str ,
1331- attribute : Option < AttributeValue > ,
1332- ) -> Result < DateTime < Utc > , DBItemError > {
1333- if let Some ( AttributeValue :: S ( created) ) = & attribute {
1334- created. parse ( ) . map_err ( |e| {
1335- DBItemError :: new (
1336- attribute_name. to_string ( ) ,
1337- attribute. into ( ) ,
1338- DBItemAttributeError :: InvalidTimestamp ( e) ,
1339- )
1340- } )
1341- } else {
1342- Err ( DBItemError :: new (
1343- attribute_name. to_string ( ) ,
1344- attribute. into ( ) ,
1345- DBItemAttributeError :: Missing ,
1346- ) )
1347- }
1348- }
1349-
13501319fn parse_auth_type_attribute (
13511320 attribute : Option < AttributeValue > ,
13521321) -> Result < AuthType , DBItemError > {
@@ -1425,6 +1394,7 @@ fn parse_registration_data_attribute(
14251394 }
14261395}
14271396
1397+ #[ deprecated( note = "Use `comm_lib` counterpart instead" ) ]
14281398#[ allow( dead_code) ]
14291399fn parse_map_attribute (
14301400 attribute_name : & str ,
@@ -1460,40 +1430,6 @@ fn parse_map_attribute(
14601430 }
14611431}
14621432
1463- fn parse_string_attribute (
1464- attribute_name : & str ,
1465- attribute_value : Option < AttributeValue > ,
1466- ) -> Result < String , DBItemError > {
1467- match attribute_value {
1468- Some ( AttributeValue :: S ( value) ) => Ok ( value) ,
1469- Some ( _) => {
1470- error ! (
1471- attribute = attribute_name,
1472- value = ?attribute_value,
1473- error_type = "IncorrectType" ,
1474- "Unexpected attribute type when parsing string attribute"
1475- ) ;
1476- Err ( DBItemError :: new (
1477- attribute_name. to_string ( ) ,
1478- attribute_value. into ( ) ,
1479- DBItemAttributeError :: IncorrectType ,
1480- ) )
1481- }
1482- None => {
1483- error ! (
1484- attribute = attribute_name,
1485- error_type = "Missing" ,
1486- "Attribute is missing"
1487- ) ;
1488- Err ( DBItemError :: new (
1489- attribute_name. to_string ( ) ,
1490- attribute_value. into ( ) ,
1491- DBItemAttributeError :: Missing ,
1492- ) )
1493- }
1494- }
1495- }
1496-
14971433fn create_device_info (
14981434 flattened_device_key_upload : FlattenedDeviceKeyUpload ,
14991435 social_proof : Option < String > ,
@@ -1583,7 +1519,7 @@ mod tests {
15831519 fn validate_keys ( ) {
15841520 // Taken from test user
15851521 let example_payload = r#"{\"notificationIdentityPublicKeys\":{\"curve25519\":\"DYmV8VdkjwG/VtC8C53morogNJhpTPT/4jzW0/cxzQo\",\"ed25519\":\"D0BV2Y7Qm36VUtjwyQTJJWYAycN7aMSJmhEsRJpW2mk\"},\"primaryIdentityPublicKeys\":{\"curve25519\":\"Y4ZIqzpE1nv83kKGfvFP6rifya0itRg2hifqYtsISnk\",\"ed25519\":\"cSlL+VLLJDgtKSPlIwoCZg0h0EmHlQoJC08uV/O+jvg\"}}"# ;
1586- let serialized_payload = KeyPayload :: from_str ( & example_payload) . unwrap ( ) ;
1522+ let serialized_payload = KeyPayload :: from_str ( example_payload) . unwrap ( ) ;
15871523
15881524 assert_eq ! (
15891525 serialized_payload
0 commit comments