Simplify syntax of the read_cbor_map! macro, to align it with cbor_map.

This commit is contained in:
Guillaume Endignoux
2020-06-09 15:26:55 +02:00
parent 2c4bf7d422
commit 2124511913
3 changed files with 70 additions and 69 deletions

View File

@@ -40,8 +40,8 @@
/// # let map = alloc::collections::BTreeMap::new(); /// # let map = alloc::collections::BTreeMap::new();
/// read_cbor_map! { /// read_cbor_map! {
/// map, /// map,
/// x @ cbor_unsigned!(1), /// x @ 1,
/// y @ cbor_unsigned!(2), /// y @ 2,
/// }; /// };
/// # } /// # }
/// ``` /// ```
@@ -65,7 +65,7 @@ macro_rules! read_cbor_map {
#[cfg(test)] #[cfg(test)]
test_ordered_keys!($( $key, )+); test_ordered_keys!($( $key, )+);
use $crate::values::{KeyType, Value}; use $crate::values::{IntoCborKey, KeyType, Value};
use ::core::cmp::Ordering; use ::core::cmp::Ordering;
use ::core::iter::Peekable; use ::core::iter::Peekable;
use ::alloc::collections::btree_map::IntoIter; use ::alloc::collections::btree_map::IntoIter;
@@ -73,7 +73,7 @@ macro_rules! read_cbor_map {
let mut it: Peekable<IntoIter<KeyType, Value>> = $map.into_iter().peekable(); let mut it: Peekable<IntoIter<KeyType, Value>> = $map.into_iter().peekable();
$( $(
let $variable: Option<Value> = { let $variable: Option<Value> = {
let needle: KeyType = $key; let needle: KeyType = $key.into_cbor_key();
loop { loop {
match it.peek() { match it.peek() {
None => break None, None => break None,
@@ -106,8 +106,9 @@ macro_rules! test_ordered_keys {
( $key1:expr, $key2:expr, $( $keys:expr, )* ) => { ( $key1:expr, $key2:expr, $( $keys:expr, )* ) => {
{ {
let k1: $crate::values::KeyType = $key1; use $crate::values::{IntoCborKey, KeyType};
let k2: $crate::values::KeyType = $key2; let k1: KeyType = $key1.into_cbor_key();
let k2: KeyType = $key2.into_cbor_key();
assert!( assert!(
k1 < k2, k1 < k2,
"{:?} < {:?} failed. The read_cbor_map! macro requires keys in sorted order.", "{:?} < {:?} failed. The read_cbor_map! macro requires keys in sorted order.",
@@ -621,8 +622,8 @@ mod test {
read_cbor_map! { read_cbor_map! {
extract_map(map), extract_map(map),
x1 @ cbor_unsigned!(1), x1 @ 1,
x2 @ cbor_unsigned!(2), x2 @ 2,
}; };
assert_eq!(x1, Some(cbor_unsigned!(10))); assert_eq!(x1, Some(cbor_unsigned!(10)));
@@ -639,8 +640,8 @@ mod test {
read_cbor_map! { read_cbor_map! {
extract_map(map), extract_map(map),
_x2 @ cbor_unsigned!(2), _x2 @ 2,
_x1 @ cbor_unsigned!(1), _x1 @ 1,
}; };
} }
@@ -660,8 +661,8 @@ mod test {
read_cbor_map! { read_cbor_map! {
extract_map(map), extract_map(map),
x3 @ cbor_unsigned!(3), x3 @ 3,
x7 @ cbor_unsigned!(7), x7 @ 7,
}; };
assert_eq!(x3, Some(cbor_unsigned!(30))); assert_eq!(x3, Some(cbor_unsigned!(30)));
@@ -678,12 +679,12 @@ mod test {
read_cbor_map! { read_cbor_map! {
extract_map(map), extract_map(map),
x0 @ cbor_unsigned!(0), x0 @ 0,
x1 @ cbor_unsigned!(1), x1 @ 1,
x2 @ cbor_unsigned!(2), x2 @ 2,
x3 @ cbor_unsigned!(3), x3 @ 3,
x4 @ cbor_unsigned!(4), x4 @ 4,
x5 @ cbor_unsigned!(5), x5 @ 5,
}; };
assert_eq!(x0, None); assert_eq!(x0, None);

View File

@@ -126,15 +126,15 @@ 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)?, extract_map(cbor_value)?,
client_data_hash @ cbor_unsigned!(1), client_data_hash @ 1,
rp @ cbor_unsigned!(2), rp @ 2,
user @ cbor_unsigned!(3), user @ 3,
cred_param_vec @ cbor_unsigned!(4), cred_param_vec @ 4,
exclude_list @ cbor_unsigned!(5), exclude_list @ 5,
extensions @ cbor_unsigned!(6), extensions @ 6,
options @ cbor_unsigned!(7), options @ 7,
pin_uv_auth_param @ cbor_unsigned!(8), pin_uv_auth_param @ 8,
pin_uv_auth_protocol @ cbor_unsigned!(9), pin_uv_auth_protocol @ 9,
}; };
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)?)?;
@@ -208,13 +208,13 @@ 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)?, extract_map(cbor_value)?,
rp_id @ cbor_unsigned!(1), rp_id @ 1,
client_data_hash @ cbor_unsigned!(2), client_data_hash @ 2,
allow_list @ cbor_unsigned!(3), allow_list @ 3,
extensions @ cbor_unsigned!(4), extensions @ 4,
options @ cbor_unsigned!(5), options @ 5,
pin_uv_auth_param @ cbor_unsigned!(6), pin_uv_auth_param @ 6,
pin_uv_auth_protocol @ cbor_unsigned!(7), pin_uv_auth_protocol @ 7,
}; };
let rp_id = extract_text_string(ok_or_missing(rp_id)?)?; let rp_id = extract_text_string(ok_or_missing(rp_id)?)?;
@@ -277,12 +277,12 @@ 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)?, extract_map(cbor_value)?,
pin_protocol @ cbor_unsigned!(1), pin_protocol @ 1,
sub_command @ cbor_unsigned!(2), sub_command @ 2,
key_agreement @ cbor_unsigned!(3), key_agreement @ 3,
pin_auth @ cbor_unsigned!(4), pin_auth @ 4,
new_pin_enc @ cbor_unsigned!(5), new_pin_enc @ 5,
pin_hash_enc @ cbor_unsigned!(6), pin_hash_enc @ 6,
}; };
let pin_protocol = extract_unsigned(ok_or_missing(pin_protocol)?)?; let pin_protocol = extract_unsigned(ok_or_missing(pin_protocol)?)?;

View File

@@ -33,9 +33,9 @@ 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)?, extract_map(cbor_value)?,
rp_id @ cbor_text!("id"), rp_id @ "id",
rp_icon @ cbor_text!("icon"), rp_icon @ "icon",
rp_name @ cbor_text!("name"), rp_name @ "name",
}; };
let rp_id = extract_text_string(ok_or_missing(rp_id)?)?; let rp_id = extract_text_string(ok_or_missing(rp_id)?)?;
@@ -65,10 +65,10 @@ 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)?, extract_map(cbor_value)?,
user_id @ cbor_text!("id"), user_id @ "id",
user_icon @ cbor_text!("icon"), user_icon @ "icon",
user_name @ cbor_text!("name"), user_name @ "name",
user_display_name @ cbor_text!("displayName"), user_display_name @ "displayName",
}; };
let user_id = extract_byte_string(ok_or_missing(user_id)?)?; let user_id = extract_byte_string(ok_or_missing(user_id)?)?;
@@ -143,8 +143,8 @@ 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)?, extract_map(cbor_value)?,
alg @ cbor_text!("alg"), alg @ "alg",
cred_type @ cbor_text!("type"), cred_type @ "type",
}; };
let cred_type = PublicKeyCredentialType::try_from(ok_or_missing(cred_type)?)?; let cred_type = PublicKeyCredentialType::try_from(ok_or_missing(cred_type)?)?;
@@ -212,9 +212,9 @@ 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)?, extract_map(cbor_value)?,
key_id @ cbor_text!("id"), key_id @ "id",
key_type @ cbor_text!("type"), key_type @ "type",
transports @ cbor_text!("transports"), transports @ "transports",
}; };
let key_type = PublicKeyCredentialType::try_from(ok_or_missing(key_type)?)?; let key_type = PublicKeyCredentialType::try_from(ok_or_missing(key_type)?)?;
@@ -261,8 +261,8 @@ 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)?, extract_map(cbor_value)?,
cred_protect @ cbor_text!("credProtect"), cred_protect @ "credProtect",
hmac_secret @ cbor_text!("hmac-secret"), hmac_secret @ "hmac-secret",
}; };
let hmac_secret = hmac_secret.map_or(Ok(false), extract_bool)?; let hmac_secret = hmac_secret.map_or(Ok(false), extract_bool)?;
@@ -287,7 +287,7 @@ 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)?, extract_map(cbor_value)?,
hmac_secret @ cbor_text!("hmac-secret"), hmac_secret @ "hmac-secret",
}; };
let hmac_secret = hmac_secret let hmac_secret = hmac_secret
@@ -310,9 +310,9 @@ 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)?, extract_map(cbor_value)?,
cose_key @ cbor_unsigned!(1), cose_key @ 1,
salt_enc @ cbor_unsigned!(2), salt_enc @ 2,
salt_auth @ cbor_unsigned!(3), salt_auth @ 3,
}; };
let cose_key = extract_map(ok_or_missing(cose_key)?)?; let cose_key = extract_map(ok_or_missing(cose_key)?)?;
@@ -339,9 +339,9 @@ 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)?, extract_map(cbor_value)?,
rk @ cbor_text!("rk"), rk @ "rk",
up @ cbor_text!("up"), up @ "up",
uv @ cbor_text!("uv"), uv @ "uv",
}; };
let rk = match rk { let rk = match rk {
@@ -373,9 +373,9 @@ 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)?, extract_map(cbor_value)?,
rk @ cbor_text!("rk"), rk @ "rk",
up @ cbor_text!("up"), up @ "up",
uv @ cbor_text!("uv"), uv @ "uv",
}; };
if let Some(options_entry) = rk { if let Some(options_entry) = rk {
@@ -630,11 +630,11 @@ 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, cose_key.0,
key_type @ cbor_int!(1), key_type @ 1,
algorithm @ cbor_int!(3), algorithm @ 3,
curve @ cbor_int!(-1), curve @ -1,
x_bytes @ cbor_int!(-2), x_bytes @ -2,
y_bytes @ cbor_int!(-3), y_bytes @ -3,
}; };
let key_type = extract_integer(ok_or_missing(key_type)?)?; let key_type = extract_integer(ok_or_missing(key_type)?)?;