Apply syntax suggestion to read_cbor_map! macro.

This commit is contained in:
Guillaume Endignoux
2020-06-15 16:55:07 +02:00
parent b0321f6b4f
commit 493efa9b25
3 changed files with 111 additions and 93 deletions

View File

@@ -44,9 +44,10 @@ use core::iter::Peekable;
/// # fn main() { /// # fn main() {
/// # let map = alloc::collections::BTreeMap::new(); /// # let map = alloc::collections::BTreeMap::new();
/// read_cbor_map! { /// read_cbor_map! {
/// map, /// let {
/// x @ 1, /// 1 => x,
/// y @ 2, /// 2 => y,
/// } = map;
/// }; /// };
/// # } /// # }
/// ``` /// ```
@@ -64,7 +65,7 @@ use core::iter::Peekable;
/// ``` /// ```
#[macro_export] #[macro_export]
macro_rules! read_cbor_map { 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 // A pre-requisite for this algorithm to work is that the keys to extract from the map are
// sorted. // sorted.
#[cfg(test)] #[cfg(test)]
@@ -98,7 +99,6 @@ pub fn read_cbor_map_peek_value(
match key.cmp(&needle) { match key.cmp(&needle) {
Ordering::Less => { Ordering::Less => {
it.next(); it.next();
continue;
} }
Ordering::Equal => { Ordering::Equal => {
let value: Value = it.next().unwrap().1; let value: Value = it.next().unwrap().1;
@@ -634,9 +634,10 @@ mod test {
}; };
read_cbor_map! { read_cbor_map! {
extract_map(map), let {
x1 @ 1, 1 => x1,
x2 @ 2, 2 => x2,
} = extract_map(map);
}; };
assert_eq!(x1, Some(cbor_unsigned!(10))); assert_eq!(x1, Some(cbor_unsigned!(10)));
@@ -652,9 +653,10 @@ mod test {
}; };
read_cbor_map! { read_cbor_map! {
extract_map(map), let {
_x2 @ 2, 2 => _x2,
_x1 @ 1, 1 => _x1,
} = extract_map(map);
}; };
} }
@@ -673,9 +675,10 @@ mod test {
}; };
read_cbor_map! { read_cbor_map! {
extract_map(map), let {
x3 @ 3, 3 => x3,
x7 @ 7, 7 => x7,
} = extract_map(map);
}; };
assert_eq!(x3, Some(cbor_unsigned!(30))); assert_eq!(x3, Some(cbor_unsigned!(30)));
@@ -691,13 +694,14 @@ mod test {
}; };
read_cbor_map! { read_cbor_map! {
extract_map(map), let {
x0 @ 0, 0 => x0,
x1 @ 1, 1 => x1,
x2 @ 2, 2 => x2,
x3 @ 3, 3 => x3,
x4 @ 4, 4 => x4,
x5 @ 5, 5 => x5,
} = extract_map(map);
}; };
assert_eq!(x0, None); assert_eq!(x0, None);

View File

@@ -125,16 +125,17 @@ impl TryFrom<cbor::Value> for AuthenticatorMakeCredentialParameters {
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> { fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
client_data_hash @ 1, 1 => client_data_hash,
rp @ 2, 2 => rp,
user @ 3, 3 => user,
cred_param_vec @ 4, 4 => cred_param_vec,
exclude_list @ 5, 5 => exclude_list,
extensions @ 6, 6 => extensions,
options @ 7, 7 => options,
pin_uv_auth_param @ 8, 8 => pin_uv_auth_param,
pin_uv_auth_protocol @ 9, 9 => pin_uv_auth_protocol,
} = extract_map(cbor_value)?;
}; };
let client_data_hash = extract_byte_string(ok_or_missing(client_data_hash)?)?; let client_data_hash = extract_byte_string(ok_or_missing(client_data_hash)?)?;
@@ -207,14 +208,15 @@ impl TryFrom<cbor::Value> for AuthenticatorGetAssertionParameters {
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> { fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
rp_id @ 1, 1 => rp_id,
client_data_hash @ 2, 2 => client_data_hash,
allow_list @ 3, 3 => allow_list,
extensions @ 4, 4 => extensions,
options @ 5, 5 => options,
pin_uv_auth_param @ 6, 6 => pin_uv_auth_param,
pin_uv_auth_protocol @ 7, 7 => pin_uv_auth_protocol,
} = extract_map(cbor_value)?;
}; };
let rp_id = extract_text_string(ok_or_missing(rp_id)?)?; let rp_id = extract_text_string(ok_or_missing(rp_id)?)?;
@@ -276,13 +278,14 @@ impl TryFrom<cbor::Value> for AuthenticatorClientPinParameters {
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> { fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
pin_protocol @ 1, 1 => pin_protocol,
sub_command @ 2, 2 => sub_command,
key_agreement @ 3, 3 => key_agreement,
pin_auth @ 4, 4 => pin_auth,
new_pin_enc @ 5, 5 => new_pin_enc,
pin_hash_enc @ 6, 6 => pin_hash_enc,
} = extract_map(cbor_value)?;
}; };
let pin_protocol = extract_unsigned(ok_or_missing(pin_protocol)?)?; let pin_protocol = extract_unsigned(ok_or_missing(pin_protocol)?)?;

View File

@@ -32,10 +32,11 @@ impl TryFrom<cbor::Value> for PublicKeyCredentialRpEntity {
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> { fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
rp_id @ "id", "id" => rp_id,
rp_icon @ "icon", "icon" => rp_icon,
rp_name @ "name", "name" => rp_name,
} = extract_map(cbor_value)?;
}; };
let rp_id = extract_text_string(ok_or_missing(rp_id)?)?; let rp_id = extract_text_string(ok_or_missing(rp_id)?)?;
@@ -64,11 +65,12 @@ impl TryFrom<cbor::Value> for PublicKeyCredentialUserEntity {
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> { fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
user_id @ "id", "id" => user_id,
user_icon @ "icon", "icon" => user_icon,
user_name @ "name", "name" => user_name,
user_display_name @ "displayName", "displayName" => user_display_name,
} = extract_map(cbor_value)?;
}; };
let user_id = extract_byte_string(ok_or_missing(user_id)?)?; let user_id = extract_byte_string(ok_or_missing(user_id)?)?;
@@ -142,9 +144,10 @@ impl TryFrom<cbor::Value> for PublicKeyCredentialParameter {
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> { fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
alg @ "alg", "alg" => alg,
cred_type @ "type", "type" => cred_type,
} = extract_map(cbor_value)?;
}; };
let cred_type = PublicKeyCredentialType::try_from(ok_or_missing(cred_type)?)?; let cred_type = PublicKeyCredentialType::try_from(ok_or_missing(cred_type)?)?;
@@ -211,10 +214,11 @@ impl TryFrom<cbor::Value> for PublicKeyCredentialDescriptor {
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> { fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
key_id @ "id", "id" => key_id,
key_type @ "type", "type" => key_type,
transports @ "transports", "transports" => transports,
} = extract_map(cbor_value)?;
}; };
let key_type = PublicKeyCredentialType::try_from(ok_or_missing(key_type)?)?; let key_type = PublicKeyCredentialType::try_from(ok_or_missing(key_type)?)?;
@@ -260,9 +264,10 @@ impl TryFrom<cbor::Value> for MakeCredentialExtensions {
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> { fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
cred_protect @ "credProtect", "credProtect" => cred_protect,
hmac_secret @ "hmac-secret", "hmac-secret" => hmac_secret,
} = extract_map(cbor_value)?;
}; };
let hmac_secret = hmac_secret.map_or(Ok(false), extract_bool)?; let hmac_secret = hmac_secret.map_or(Ok(false), extract_bool)?;
@@ -286,8 +291,9 @@ impl TryFrom<cbor::Value> for GetAssertionExtensions {
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> { fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
hmac_secret @ "hmac-secret", "hmac-secret" => hmac_secret,
} = extract_map(cbor_value)?;
}; };
let hmac_secret = hmac_secret let hmac_secret = hmac_secret
@@ -309,10 +315,11 @@ impl TryFrom<cbor::Value> for GetAssertionHmacSecretInput {
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> { fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
cose_key @ 1, 1 => cose_key,
salt_enc @ 2, 2 => salt_enc,
salt_auth @ 3, 3 => salt_auth,
} = extract_map(cbor_value)?;
}; };
let cose_key = extract_map(ok_or_missing(cose_key)?)?; let cose_key = extract_map(ok_or_missing(cose_key)?)?;
@@ -338,10 +345,11 @@ impl TryFrom<cbor::Value> for MakeCredentialOptions {
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> { fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
rk @ "rk", "rk" => rk,
up @ "up", "up" => up,
uv @ "uv", "uv" => uv,
} = extract_map(cbor_value)?;
}; };
let rk = match rk { let rk = match rk {
@@ -372,10 +380,11 @@ impl TryFrom<cbor::Value> for GetAssertionOptions {
fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> { fn try_from(cbor_value: cbor::Value) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
rk @ "rk", "rk" => rk,
up @ "up", "up" => up,
uv @ "uv", "uv" => uv,
} = extract_map(cbor_value)?;
}; };
if let Some(options_entry) = rk { if let Some(options_entry) = rk {
@@ -534,14 +543,15 @@ impl TryFrom<cbor::Value> for PublicKeyCredentialSource {
CredProtectPolicy, CredRandom, CredentialId, OtherUi, PrivateKey, RpId, UserHandle, CredProtectPolicy, CredRandom, CredentialId, OtherUi, PrivateKey, RpId, UserHandle,
}; };
read_cbor_map! { read_cbor_map! {
extract_map(cbor_value)?, let {
credential_id @ CredentialId, CredentialId => credential_id,
private_key @ PrivateKey, PrivateKey => private_key,
rp_id @ RpId, RpId => rp_id,
user_handle @ UserHandle, UserHandle => user_handle,
other_ui @ OtherUi, OtherUi => other_ui,
cred_random @ CredRandom, CredRandom => cred_random,
cred_protect_policy @ CredProtectPolicy, CredProtectPolicy => cred_protect_policy,
} = extract_map(cbor_value)?;
}; };
let credential_id = extract_byte_string(ok_or_missing(credential_id)?)?; let credential_id = extract_byte_string(ok_or_missing(credential_id)?)?;
@@ -634,12 +644,13 @@ impl TryFrom<CoseKey> for ecdh::PubKey {
fn try_from(cose_key: CoseKey) -> Result<Self, Ctap2StatusCode> { fn try_from(cose_key: CoseKey) -> Result<Self, Ctap2StatusCode> {
read_cbor_map! { read_cbor_map! {
cose_key.0, let {
key_type @ 1, 1 => key_type,
algorithm @ 3, 3 => algorithm,
curve @ -1, -1 => curve,
x_bytes @ -2, -2 => x_bytes,
y_bytes @ -3, -3 => y_bytes,
} = cose_key.0;
}; };
let key_type = extract_integer(ok_or_missing(key_type)?)?; let key_type = extract_integer(ok_or_missing(key_type)?)?;