diff --git a/libraries/cbor/src/macros.rs b/libraries/cbor/src/macros.rs index 29d76b1..d8dc860 100644 --- a/libraries/cbor/src/macros.rs +++ b/libraries/cbor/src/macros.rs @@ -44,9 +44,10 @@ use core::iter::Peekable; /// # fn main() { /// # let map = alloc::collections::BTreeMap::new(); /// read_cbor_map! { -/// map, -/// x @ 1, -/// y @ 2, +/// let { +/// 1 => x, +/// 2 => y, +/// } = map; /// }; /// # } /// ``` @@ -64,7 +65,7 @@ use core::iter::Peekable; /// ``` #[macro_export] macro_rules! read_cbor_map { - ( $map:expr, $( $variable:ident @ $key:expr, )+ ) => { + ( let { $( $key:expr => $variable:ident, )+ } = $map:expr; ) => { // A pre-requisite for this algorithm to work is that the keys to extract from the map are // sorted. #[cfg(test)] @@ -98,7 +99,6 @@ pub fn read_cbor_map_peek_value( match key.cmp(&needle) { Ordering::Less => { it.next(); - continue; } Ordering::Equal => { let value: Value = it.next().unwrap().1; @@ -634,9 +634,10 @@ mod test { }; read_cbor_map! { - extract_map(map), - x1 @ 1, - x2 @ 2, + let { + 1 => x1, + 2 => x2, + } = extract_map(map); }; assert_eq!(x1, Some(cbor_unsigned!(10))); @@ -652,9 +653,10 @@ mod test { }; read_cbor_map! { - extract_map(map), - _x2 @ 2, - _x1 @ 1, + let { + 2 => _x2, + 1 => _x1, + } = extract_map(map); }; } @@ -673,9 +675,10 @@ mod test { }; read_cbor_map! { - extract_map(map), - x3 @ 3, - x7 @ 7, + let { + 3 => x3, + 7 => x7, + } = extract_map(map); }; assert_eq!(x3, Some(cbor_unsigned!(30))); @@ -691,13 +694,14 @@ mod test { }; read_cbor_map! { - extract_map(map), - x0 @ 0, - x1 @ 1, - x2 @ 2, - x3 @ 3, - x4 @ 4, - x5 @ 5, + let { + 0 => x0, + 1 => x1, + 2 => x2, + 3 => x3, + 4 => x4, + 5 => x5, + } = extract_map(map); }; assert_eq!(x0, None); diff --git a/src/ctap/command.rs b/src/ctap/command.rs index 7307c40..653e6da 100644 --- a/src/ctap/command.rs +++ b/src/ctap/command.rs @@ -125,16 +125,17 @@ impl TryFrom for AuthenticatorMakeCredentialParameters { fn try_from(cbor_value: cbor::Value) -> Result { read_cbor_map! { - extract_map(cbor_value)?, - client_data_hash @ 1, - rp @ 2, - user @ 3, - cred_param_vec @ 4, - exclude_list @ 5, - extensions @ 6, - options @ 7, - pin_uv_auth_param @ 8, - pin_uv_auth_protocol @ 9, + let { + 1 => client_data_hash, + 2 => rp, + 3 => user, + 4 => cred_param_vec, + 5 => exclude_list, + 6 => extensions, + 7 => options, + 8 => pin_uv_auth_param, + 9 => pin_uv_auth_protocol, + } = extract_map(cbor_value)?; }; let client_data_hash = extract_byte_string(ok_or_missing(client_data_hash)?)?; @@ -207,14 +208,15 @@ impl TryFrom for AuthenticatorGetAssertionParameters { fn try_from(cbor_value: cbor::Value) -> Result { read_cbor_map! { - extract_map(cbor_value)?, - rp_id @ 1, - client_data_hash @ 2, - allow_list @ 3, - extensions @ 4, - options @ 5, - pin_uv_auth_param @ 6, - pin_uv_auth_protocol @ 7, + let { + 1 => rp_id, + 2 => client_data_hash, + 3 => allow_list, + 4 => extensions, + 5 => options, + 6 => pin_uv_auth_param, + 7 => pin_uv_auth_protocol, + } = extract_map(cbor_value)?; }; let rp_id = extract_text_string(ok_or_missing(rp_id)?)?; @@ -276,13 +278,14 @@ impl TryFrom for AuthenticatorClientPinParameters { fn try_from(cbor_value: cbor::Value) -> Result { read_cbor_map! { - extract_map(cbor_value)?, - pin_protocol @ 1, - sub_command @ 2, - key_agreement @ 3, - pin_auth @ 4, - new_pin_enc @ 5, - pin_hash_enc @ 6, + let { + 1 => pin_protocol, + 2 => sub_command, + 3 => key_agreement, + 4 => pin_auth, + 5 => new_pin_enc, + 6 => pin_hash_enc, + } = extract_map(cbor_value)?; }; let pin_protocol = extract_unsigned(ok_or_missing(pin_protocol)?)?; diff --git a/src/ctap/data_formats.rs b/src/ctap/data_formats.rs index 114e0d6..942abc7 100644 --- a/src/ctap/data_formats.rs +++ b/src/ctap/data_formats.rs @@ -32,10 +32,11 @@ impl TryFrom for PublicKeyCredentialRpEntity { fn try_from(cbor_value: cbor::Value) -> Result { read_cbor_map! { - extract_map(cbor_value)?, - rp_id @ "id", - rp_icon @ "icon", - rp_name @ "name", + let { + "id" => rp_id, + "icon" => rp_icon, + "name" => rp_name, + } = extract_map(cbor_value)?; }; let rp_id = extract_text_string(ok_or_missing(rp_id)?)?; @@ -64,11 +65,12 @@ impl TryFrom for PublicKeyCredentialUserEntity { fn try_from(cbor_value: cbor::Value) -> Result { read_cbor_map! { - extract_map(cbor_value)?, - user_id @ "id", - user_icon @ "icon", - user_name @ "name", - user_display_name @ "displayName", + let { + "id" => user_id, + "icon" => user_icon, + "name" => user_name, + "displayName" => user_display_name, + } = extract_map(cbor_value)?; }; let user_id = extract_byte_string(ok_or_missing(user_id)?)?; @@ -142,9 +144,10 @@ impl TryFrom for PublicKeyCredentialParameter { fn try_from(cbor_value: cbor::Value) -> Result { read_cbor_map! { - extract_map(cbor_value)?, - alg @ "alg", - cred_type @ "type", + let { + "alg" => alg, + "type" => cred_type, + } = extract_map(cbor_value)?; }; let cred_type = PublicKeyCredentialType::try_from(ok_or_missing(cred_type)?)?; @@ -211,10 +214,11 @@ impl TryFrom for PublicKeyCredentialDescriptor { fn try_from(cbor_value: cbor::Value) -> Result { read_cbor_map! { - extract_map(cbor_value)?, - key_id @ "id", - key_type @ "type", - transports @ "transports", + let { + "id" => key_id, + "type" => key_type, + "transports" => transports, + } = extract_map(cbor_value)?; }; let key_type = PublicKeyCredentialType::try_from(ok_or_missing(key_type)?)?; @@ -260,9 +264,10 @@ impl TryFrom for MakeCredentialExtensions { fn try_from(cbor_value: cbor::Value) -> Result { read_cbor_map! { - extract_map(cbor_value)?, - cred_protect @ "credProtect", - hmac_secret @ "hmac-secret", + let { + "credProtect" => cred_protect, + "hmac-secret" => hmac_secret, + } = extract_map(cbor_value)?; }; let hmac_secret = hmac_secret.map_or(Ok(false), extract_bool)?; @@ -286,8 +291,9 @@ impl TryFrom for GetAssertionExtensions { fn try_from(cbor_value: cbor::Value) -> Result { read_cbor_map! { - extract_map(cbor_value)?, - hmac_secret @ "hmac-secret", + let { + "hmac-secret" => hmac_secret, + } = extract_map(cbor_value)?; }; let hmac_secret = hmac_secret @@ -309,10 +315,11 @@ impl TryFrom for GetAssertionHmacSecretInput { fn try_from(cbor_value: cbor::Value) -> Result { read_cbor_map! { - extract_map(cbor_value)?, - cose_key @ 1, - salt_enc @ 2, - salt_auth @ 3, + let { + 1 => cose_key, + 2 => salt_enc, + 3 => salt_auth, + } = extract_map(cbor_value)?; }; let cose_key = extract_map(ok_or_missing(cose_key)?)?; @@ -338,10 +345,11 @@ impl TryFrom for MakeCredentialOptions { fn try_from(cbor_value: cbor::Value) -> Result { read_cbor_map! { - extract_map(cbor_value)?, - rk @ "rk", - up @ "up", - uv @ "uv", + let { + "rk" => rk, + "up" => up, + "uv" => uv, + } = extract_map(cbor_value)?; }; let rk = match rk { @@ -372,10 +380,11 @@ impl TryFrom for GetAssertionOptions { fn try_from(cbor_value: cbor::Value) -> Result { read_cbor_map! { - extract_map(cbor_value)?, - rk @ "rk", - up @ "up", - uv @ "uv", + let { + "rk" => rk, + "up" => up, + "uv" => uv, + } = extract_map(cbor_value)?; }; if let Some(options_entry) = rk { @@ -534,14 +543,15 @@ impl TryFrom for PublicKeyCredentialSource { CredProtectPolicy, CredRandom, CredentialId, OtherUi, PrivateKey, RpId, UserHandle, }; read_cbor_map! { - extract_map(cbor_value)?, - credential_id @ CredentialId, - private_key @ PrivateKey, - rp_id @ RpId, - user_handle @ UserHandle, - other_ui @ OtherUi, - cred_random @ CredRandom, - cred_protect_policy @ CredProtectPolicy, + let { + CredentialId => credential_id, + PrivateKey => private_key, + RpId => rp_id, + UserHandle => user_handle, + OtherUi => other_ui, + CredRandom => cred_random, + CredProtectPolicy => cred_protect_policy, + } = extract_map(cbor_value)?; }; let credential_id = extract_byte_string(ok_or_missing(credential_id)?)?; @@ -634,12 +644,13 @@ impl TryFrom for ecdh::PubKey { fn try_from(cose_key: CoseKey) -> Result { read_cbor_map! { - cose_key.0, - key_type @ 1, - algorithm @ 3, - curve @ -1, - x_bytes @ -2, - y_bytes @ -3, + let { + 1 => key_type, + 3 => algorithm, + -1 => curve, + -2 => x_bytes, + -3 => y_bytes, + } = cose_key.0; }; let key_type = extract_integer(ok_or_missing(key_type)?)?;