Remove identifier from first 10 entries of serialize_map_optional!
This commit is contained in:
Родитель
a05a04c704
Коммит
aace29d1e7
|
@ -27,9 +27,9 @@ impl Serialize for SetMinPINLength {
|
|||
{
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v1: &0x01 => self.new_min_pin_length,
|
||||
v2: &0x02 => &self.min_pin_length_rpids,
|
||||
v3: &0x03 => self.force_change_pin,
|
||||
&0x01 => self.new_min_pin_length,
|
||||
&0x02 => &self.min_pin_length_rpids,
|
||||
&0x03 => self.force_change_pin,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -84,10 +84,10 @@ impl Serialize for AuthenticatorConfig {
|
|||
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v1: &0x01 => Some(entry01),
|
||||
v2: &0x02 => entry02,
|
||||
v3: &0x03 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()),
|
||||
v4: &0x04 => &self.pin_uv_auth_param,
|
||||
&0x01 => Some(entry01),
|
||||
&0x02 => entry02,
|
||||
&0x03 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()),
|
||||
&0x04 => &self.pin_uv_auth_param,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -97,9 +97,9 @@ impl Serialize for BioEnrollmentParams {
|
|||
{
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v1: &0x01 => self.template_id.as_deref().map(ByteBuf::from),
|
||||
v2: &0x02 => &self.template_friendly_name,
|
||||
v3: &0x03 => self.timeout_milliseconds,
|
||||
&0x01 => self.template_id.as_deref().map(ByteBuf::from),
|
||||
&0x02 => &self.template_friendly_name,
|
||||
&0x03 => self.timeout_milliseconds,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -174,11 +174,11 @@ impl Serialize for BioEnrollment {
|
|||
let (id, params) = self.subcommand.to_id_and_param();
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v1: &0x01 => Some(&self.modality), // Per spec currently always Fingerprint
|
||||
v2: &0x02 => Some(&id),
|
||||
v3: &0x03 => params.has_some().then_some(¶ms),
|
||||
v4: &0x04 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()),
|
||||
v5: &0x05 => &self.pin_uv_auth_param,
|
||||
&0x01 => Some(&self.modality), // Per spec currently always Fingerprint
|
||||
&0x02 => Some(&id),
|
||||
&0x03 => params.has_some().then_some(¶ms),
|
||||
&0x04 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()),
|
||||
&0x05 => &self.pin_uv_auth_param,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -88,14 +88,14 @@ impl Serialize for ClientPIN {
|
|||
let command: u8 = self.subcommand as u8;
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v1: &1 => self.pin_protocol.as_ref().map(|p| p.id()),
|
||||
v2: &2 => Some(&command),
|
||||
v3: &3 => &self.key_agreement,
|
||||
v4: &4 => self.pin_auth.as_ref().map(|pin_auth| Bytes::new(pin_auth.as_ref())),
|
||||
v5: &5 => self.new_pin_enc.as_ref().map(|new_pin_enc| Bytes::new(new_pin_enc.as_ref())),
|
||||
v6: &6 => self.pin_hash_enc.as_ref().map(|pin_hash_enc| Bytes::new(pin_hash_enc.as_ref())),
|
||||
v9: &9 => self.permissions,
|
||||
va: &0x0A => &self.rp_id,
|
||||
&1 => self.pin_protocol.as_ref().map(|p| p.id()),
|
||||
&2 => Some(&command),
|
||||
&3 => &self.key_agreement,
|
||||
&4 => self.pin_auth.as_ref().map(|pin_auth| Bytes::new(pin_auth.as_ref())),
|
||||
&5 => self.new_pin_enc.as_ref().map(|new_pin_enc| Bytes::new(new_pin_enc.as_ref())),
|
||||
&6 => self.pin_hash_enc.as_ref().map(|pin_hash_enc| Bytes::new(pin_hash_enc.as_ref())),
|
||||
&9 => self.permissions,
|
||||
&0x0A => &self.rp_id,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,9 +37,9 @@ impl Serialize for CredManagementParams {
|
|||
{
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v1: &0x01 => self.rp_id_hash.as_ref().map(|r| ByteBuf::from(r.as_ref())),
|
||||
v2: &0x02 => &self.credential_id,
|
||||
v3: &0x03 => &self.user,
|
||||
&0x01 => self.rp_id_hash.as_ref().map(|r| ByteBuf::from(r.as_ref())),
|
||||
&0x02 => &self.credential_id,
|
||||
&0x03 => &self.user,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -104,10 +104,10 @@ impl Serialize for CredentialManagement {
|
|||
let (id, params) = self.subcommand.to_id_and_param();
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v1: &0x01 => Some(&id),
|
||||
v2: &0x02 => params.has_some().then_some(¶ms),
|
||||
v3: &0x03 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()),
|
||||
v4: &0x04 => &self.pin_uv_auth_param,
|
||||
&0x01 => Some(&id),
|
||||
&0x02 => params.has_some().then_some(¶ms),
|
||||
&0x03 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()),
|
||||
&0x04 => &self.pin_uv_auth_param,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -273,13 +273,13 @@ impl Serialize for GetAssertion {
|
|||
{
|
||||
serialize_map_optional! {
|
||||
serializer,
|
||||
v1: &1 => Some(&self.rp.id),
|
||||
v2: &2 => Some(&self.client_data_hash),
|
||||
v3: &3 => (!&self.allow_list.is_empty()).then_some(&self.allow_list),
|
||||
v4: &4 => self.extensions.has_content().then_some(&self.extensions),
|
||||
v5: &5 => self.options.has_some().then_some(&self.options),
|
||||
v6: &6 => &self.pin_uv_auth_param,
|
||||
v7: &7 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()),
|
||||
&1 => Some(&self.rp.id),
|
||||
&2 => Some(&self.client_data_hash),
|
||||
&3 => (!&self.allow_list.is_empty()).then_some(&self.allow_list),
|
||||
&4 => self.extensions.has_content().then_some(&self.extensions),
|
||||
&5 => self.options.has_some().then_some(&self.options),
|
||||
&6 => &self.pin_uv_auth_param,
|
||||
&7 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -418,16 +418,16 @@ impl Serialize for MakeCredentials {
|
|||
debug!("Serialize MakeCredentials");
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v1: &0x01 => Some(&self.client_data_hash),
|
||||
v2: &0x02 => Some(&self.rp),
|
||||
v3: &0x03 => Some(&self.user),
|
||||
v4: &0x04 => Some(&self.pub_cred_params),
|
||||
v5: &0x05 => (!self.exclude_list.is_empty()).then_some(&self.exclude_list),
|
||||
v6: &0x06 => self.extensions.has_content().then_some(&self.extensions),
|
||||
v7: &0x07 => self.options.has_some().then_some(&self.options),
|
||||
v8: &0x08 => &self.pin_uv_auth_param,
|
||||
v9: &0x09 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()),
|
||||
va: &0x0a => &self.enterprise_attestation,
|
||||
&0x01 => Some(&self.client_data_hash),
|
||||
&0x02 => Some(&self.rp),
|
||||
&0x03 => Some(&self.user),
|
||||
&0x04 => Some(&self.pub_cred_params),
|
||||
&0x05 => (!self.exclude_list.is_empty()).then_some(&self.exclude_list),
|
||||
&0x06 => self.extensions.has_content().then_some(&self.extensions),
|
||||
&0x07 => self.options.has_some().then_some(&self.options),
|
||||
&0x08 => &self.pin_uv_auth_param,
|
||||
&0x09 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()),
|
||||
&0x0a => &self.enterprise_attestation,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
311
src/util.rs
311
src/util.rs
|
@ -84,20 +84,90 @@ pub fn decode_hex(s: &str) -> Vec<u8> {
|
|||
/// Arguments:
|
||||
/// - An expression of type [serde::Serializer]. This expression will be bound
|
||||
/// to a local variable and thus evaluated only once.
|
||||
/// - 0 or more entries of the form `$ident: $key => $value,`, where `$ident` is
|
||||
/// an arbitrary identifier, `$key` is any expression and `$value` is an
|
||||
/// expression of type [Option<T>]. The entry will be included in the map if
|
||||
/// and only if the `$value` is [Some].
|
||||
///
|
||||
/// The `$ident` is needed in order to bind each `$value` as a local variable,
|
||||
/// in order to evaluate each expression only once. Recommended use is to
|
||||
/// simply set `$ident` to `v1`, `v2`, ..., or possibly some descriptive
|
||||
/// label.
|
||||
/// - 0 to 10 (inclusive) more entries of the form `$key => $value,`, where
|
||||
/// `$key` is any expression and `$value` is an expression of type
|
||||
/// [Option<T>]. The entry will be included in the map if and only if the
|
||||
/// `$value` is [Some]. Each key and value expression is evaluated only once.
|
||||
/// - The 11th entry and forward instead needs to take the form `$ident: $key =>
|
||||
/// $value,`, where `$ident` is an arbitrary identifier. These `$ident`s are
|
||||
/// needed in order to bind each `$value` as a local variable, in order to
|
||||
/// evaluate each expression only once. Recommended use is to simply set
|
||||
/// `$ident` to `v1`, `v2`, ..., or possibly some descriptive label.
|
||||
macro_rules! serialize_map_optional {
|
||||
(
|
||||
$serializer:expr,
|
||||
$( $value_ident:ident : $key:expr => $value:expr , )*
|
||||
) => {
|
||||
($s:expr, $k1:expr => $v1:expr $(,)?) => {
|
||||
serialize_map_optional!(
|
||||
@internal $s,
|
||||
v1: $k1 => $v1,
|
||||
)
|
||||
};
|
||||
($s:expr, $k1:expr => $v1:expr, $k2:expr => $v2:expr $(,)?) => {
|
||||
serialize_map_optional!(
|
||||
@internal $s,
|
||||
v1: $k1 => $v1, v2: $k2 => $v2,
|
||||
)
|
||||
};
|
||||
($s:expr, $k1:expr => $v1:expr, $k2:expr => $v2:expr, $k3:expr => $v3:expr $(,)?) => {
|
||||
serialize_map_optional!(
|
||||
@internal $s,
|
||||
v1: $k1 => $v1, v2: $k2 => $v2, v3: $k3 => $v3,
|
||||
)
|
||||
};
|
||||
($s:expr, $k1:expr => $v1:expr, $k2:expr => $v2:expr, $k3:expr => $v3:expr, $k4:expr => $v4:expr $(,)?) => {
|
||||
serialize_map_optional!(
|
||||
@internal $s,
|
||||
v1: $k1 => $v1, v2: $k2 => $v2, v3: $k3 => $v3, v4: $k4 => $v4,
|
||||
)
|
||||
};
|
||||
($s:expr, $k1:expr => $v1:expr, $k2:expr => $v2:expr, $k3:expr => $v3:expr, $k4:expr => $v4:expr, $k5:expr => $v5:expr $(,)?) => {
|
||||
serialize_map_optional!(
|
||||
@internal $s,
|
||||
v1: $k1 => $v1, v2: $k2 => $v2, v3: $k3 => $v3, v4: $k4 => $v4, v5: $k5 => $v5,
|
||||
)
|
||||
};
|
||||
($s:expr, $k1:expr => $v1:expr, $k2:expr => $v2:expr, $k3:expr => $v3:expr, $k4:expr => $v4:expr, $k5:expr => $v5:expr,
|
||||
$k6:expr => $v6:expr $(,)?) => {
|
||||
serialize_map_optional!(
|
||||
@internal $s,
|
||||
v1: $k1 => $v1, v2: $k2 => $v2, v3: $k3 => $v3, v4: $k4 => $v4, v5: $k5 => $v5,
|
||||
v6: $k6 => $v6,
|
||||
)
|
||||
};
|
||||
($s:expr, $k1:expr => $v1:expr, $k2:expr => $v2:expr, $k3:expr => $v3:expr, $k4:expr => $v4:expr, $k5:expr => $v5:expr,
|
||||
$k6:expr => $v6:expr, $k7:expr => $v7:expr $(,)?) => {
|
||||
serialize_map_optional!(
|
||||
@internal $s,
|
||||
v1: $k1 => $v1, v2: $k2 => $v2, v3: $k3 => $v3, v4: $k4 => $v4, v5: $k5 => $v5,
|
||||
v6: $k6 => $v6, v7: $k7 => $v7,
|
||||
)
|
||||
};
|
||||
($s:expr, $k1:expr => $v1:expr, $k2:expr => $v2:expr, $k3:expr => $v3:expr, $k4:expr => $v4:expr, $k5:expr => $v5:expr,
|
||||
$k6:expr => $v6:expr, $k7:expr => $v7:expr, $k8:expr => $v8:expr $(,)?) => {
|
||||
serialize_map_optional!(
|
||||
@internal $s,
|
||||
v1: $k1 => $v1, v2: $k2 => $v2, v3: $k3 => $v3, v4: $k4 => $v4, v5: $k5 => $v5,
|
||||
v6: $k6 => $v6, v7: $k7 => $v7, v8: $k8 => $v8,
|
||||
)
|
||||
};
|
||||
($s:expr, $k1:expr => $v1:expr, $k2:expr => $v2:expr, $k3:expr => $v3:expr, $k4:expr => $v4:expr, $k5:expr => $v5:expr,
|
||||
$k6:expr => $v6:expr, $k7:expr => $v7:expr, $k8:expr => $v8:expr, $k9:expr => $v9:expr $(,)?) => {
|
||||
serialize_map_optional!(
|
||||
@internal $s,
|
||||
v1: $k1 => $v1, v2: $k2 => $v2, v3: $k3 => $v3, v4: $k4 => $v4, v5: $k5 => $v5,
|
||||
v6: $k6 => $v6, v7: $k7 => $v7, v8: $k8 => $v8, v9: $k9 => $v9,
|
||||
)
|
||||
};
|
||||
($s:expr, $k1:expr => $v1:expr, $k2:expr => $v2:expr, $k3:expr => $v3:expr, $k4:expr => $v4:expr, $k5:expr => $v5:expr,
|
||||
$k6:expr => $v6:expr, $k7:expr => $v7:expr, $k8:expr => $v8:expr, $k9:expr => $v9:expr, $ka:expr => $va:expr,
|
||||
$( $value_ident:ident : $key:expr => $value:expr , )*) => {
|
||||
serialize_map_optional!(
|
||||
@internal $s,
|
||||
v1: $k1 => $v1, v2: $k2 => $v2, v3: $k3 => $v3, v4: $k4 => $v4, v5: $k5 => $v5,
|
||||
v6: $k6 => $v6, v7: $k7 => $v7, v8: $k8 => $v8, v9: $k9 => $v9, va: $ka => $va,
|
||||
$( $value_ident : $key => $value , )*
|
||||
)
|
||||
};
|
||||
|
||||
(@internal $serializer:expr, $( $value_ident:ident : $key:expr => $value:expr , )*) => {
|
||||
{
|
||||
let serializer = $serializer;
|
||||
|
||||
|
@ -126,7 +196,8 @@ macro_rules! serialize_map_optional {
|
|||
/// - An expression of type [serde::Serializer]. This expression will be bound
|
||||
/// to a local variable and thus evaluated only once.
|
||||
/// - 0 or more entries of the form `$key => $value,`, where `$key` and `$value`
|
||||
/// are both expressions. Each expression is evaluated only once.
|
||||
/// are both expressions. Each key and value expression is evaluated only
|
||||
/// once.
|
||||
macro_rules! serialize_map {
|
||||
(@count_entry $value:expr) => { () };
|
||||
(
|
||||
|
@ -179,7 +250,7 @@ mod tests {
|
|||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v0: &0x00 => Some("a"),
|
||||
&0x00 => Some("a"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -193,8 +264,8 @@ mod tests {
|
|||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v0: &0x00 => Some("a"),
|
||||
v1: &"a" => Some(0x01),
|
||||
&0x00 => Some("a"),
|
||||
&"a" => Some(0x01),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -211,9 +282,9 @@ mod tests {
|
|||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v0: &0x00 => Some("a"),
|
||||
v1: &"a" => Some(0x01),
|
||||
v2: &["b"] => Some(["c"]),
|
||||
&0x00 => Some("a"),
|
||||
&"a" => Some(0x01),
|
||||
&["b"] => Some(["c"]),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -230,10 +301,10 @@ mod tests {
|
|||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v0: &0x00 => Some("a"),
|
||||
v1: &"a" => Some(0x01),
|
||||
v2: &["b"] => Some(["c"]),
|
||||
v3: &["c", "d"] => Some(["d", "e"]),
|
||||
&0x00 => Some("a"),
|
||||
&"a" => Some(0x01),
|
||||
&["b"] => Some(["c"]),
|
||||
&["c", "d"] => Some(["d", "e"]),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -250,11 +321,11 @@ mod tests {
|
|||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v0: &0x00 => Some("a"),
|
||||
v1: &"a" => Some(0x01),
|
||||
v2: &["b"] => Some(["c"]),
|
||||
v3: &["c", "d"] => Some(["d", "e"]),
|
||||
v4: &-0x04 => Some("e"),
|
||||
&0x00 => Some("a"),
|
||||
&"a" => Some(0x01),
|
||||
&["b"] => Some(["c"]),
|
||||
&["c", "d"] => Some(["d", "e"]),
|
||||
&-0x04 => Some("e"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -271,12 +342,12 @@ mod tests {
|
|||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v0: &0x00 => Some("a"),
|
||||
v1: &"a" => Some(0x01),
|
||||
v2: &["b"] => Some(["c"]),
|
||||
v3: &["c", "d"] => Some(["d", "e"]),
|
||||
v4: &-0x04 => Some("e"),
|
||||
v5: &0xff => Some("f"),
|
||||
&0x00 => Some("a"),
|
||||
&"a" => Some(0x01),
|
||||
&["b"] => Some(["c"]),
|
||||
&["c", "d"] => Some(["d", "e"]),
|
||||
&-0x04 => Some("e"),
|
||||
&0xff => Some("f"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -293,13 +364,13 @@ mod tests {
|
|||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v0: &0x00 => Some("a"),
|
||||
v1: &"a" => Some(0x01),
|
||||
v2: &["b"] => Some(["c"]),
|
||||
v3: &["c", "d"] => Some(["d", "e"]),
|
||||
v4: &-0x04 => Some("e"),
|
||||
v5: &0xff => Some("f"),
|
||||
v6: &0xffff => Some("g"),
|
||||
&0x00 => Some("a"),
|
||||
&"a" => Some(0x01),
|
||||
&["b"] => Some(["c"]),
|
||||
&["c", "d"] => Some(["d", "e"]),
|
||||
&-0x04 => Some("e"),
|
||||
&0xff => Some("f"),
|
||||
&0xffff => Some("g"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -318,14 +389,14 @@ mod tests {
|
|||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v0: &0x00 => Some("a"),
|
||||
v1: &"a" => Some(0x01),
|
||||
v2: &["b"] => Some(["c"]),
|
||||
v3: &["c", "d"] => Some(["d", "e"]),
|
||||
v4: &-0x04 => Some("e"),
|
||||
v5: &0xff => Some("f"),
|
||||
v6: &0xffff => Some("g"),
|
||||
v7: &0xffffff => Some("h"),
|
||||
&0x00 => Some("a"),
|
||||
&"a" => Some(0x01),
|
||||
&["b"] => Some(["c"]),
|
||||
&["c", "d"] => Some(["d", "e"]),
|
||||
&-0x04 => Some("e"),
|
||||
&0xff => Some("f"),
|
||||
&0xffff => Some("g"),
|
||||
&0xffffff => Some("h"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -342,15 +413,15 @@ mod tests {
|
|||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v0: &0x00 => Some("a"),
|
||||
v1: &"a" => Some(0x01),
|
||||
v2: &["b"] => Some(["c"]),
|
||||
v3: &["c", "d"] => Some(["d", "e"]),
|
||||
v4: &-0x04 => Some("e"),
|
||||
v5: &0xff => Some("f"),
|
||||
v6: &0xffff => Some("g"),
|
||||
v7: &0xffffff => Some("h"),
|
||||
v8: &0xffffffffu32 => Some("i"),
|
||||
&0x00 => Some("a"),
|
||||
&"a" => Some(0x01),
|
||||
&["b"] => Some(["c"]),
|
||||
&["c", "d"] => Some(["d", "e"]),
|
||||
&-0x04 => Some("e"),
|
||||
&0xff => Some("f"),
|
||||
&0xffff => Some("g"),
|
||||
&0xffffff => Some("h"),
|
||||
&0xffffffffu32 => Some("i"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -367,16 +438,16 @@ mod tests {
|
|||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v0: &0x00 => Some("a"),
|
||||
v1: &"a" => Some(0x01),
|
||||
v2: &["b"] => Some(["c"]),
|
||||
v3: &["c", "d"] => Some(["d", "e"]),
|
||||
v4: &-0x04 => Some("e"),
|
||||
v5: &0xff => Some("f"),
|
||||
v6: &0xffff => Some("g"),
|
||||
v7: &0xffffff => Some("h"),
|
||||
v8: &0xffffffffu32 => Some("i"),
|
||||
v9: &0xffffffffffffffffu64 => Some("i"),
|
||||
&0x00 => Some("a"),
|
||||
&"a" => Some(0x01),
|
||||
&["b"] => Some(["c"]),
|
||||
&["c", "d"] => Some(["d", "e"]),
|
||||
&-0x04 => Some("e"),
|
||||
&0xff => Some("f"),
|
||||
&0xffff => Some("g"),
|
||||
&0xffffff => Some("h"),
|
||||
&0xffffffffu32 => Some("i"),
|
||||
&0xffffffffffffffffu64 => Some("i"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -393,17 +464,17 @@ mod tests {
|
|||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v0: &0x00 => Some("a"),
|
||||
v1: &"a" => Some(0x01),
|
||||
v2: &["b"] => Some(["c"]),
|
||||
v3: &["c", "d"] => Some(["d", "e"]),
|
||||
v4: &-0x04 => Some("e"),
|
||||
v5: &0xff => Some("f"),
|
||||
v6: &0xffff => Some("g"),
|
||||
v7: &0xffffff => Some("h"),
|
||||
v8: &0xffffffffu32 => Some("i"),
|
||||
v9: &0xffffffffffffffffu64 => Some("i"),
|
||||
va: &0x0a => Some(-1337),
|
||||
&0x00 => Some("a"),
|
||||
&"a" => Some(0x01),
|
||||
&["b"] => Some(["c"]),
|
||||
&["c", "d"] => Some(["d", "e"]),
|
||||
&-0x04 => Some("e"),
|
||||
&0xff => Some("f"),
|
||||
&0xffff => Some("g"),
|
||||
&0xffffff => Some("h"),
|
||||
&0xffffffffu32 => Some("i"),
|
||||
&0xffffffffffffffffu64 => Some("i"),
|
||||
v1: &0x0a => Some(-1337),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -420,18 +491,18 @@ mod tests {
|
|||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
v0: &0x00 => Some("a"),
|
||||
v1: &"a" => Some(0x01),
|
||||
v2: &["b"] => Some(["c"]),
|
||||
v3: &["c", "d"] => Some(["d", "e"]),
|
||||
v4: &-0x04 => Some("e"),
|
||||
v5: &0xff => Some("f"),
|
||||
v6: &0xffff => Some("g"),
|
||||
v7: &0xffffff => Some("h"),
|
||||
v8: &0xffffffffu32 => Some("i"),
|
||||
v9: &0xffffffffffffffffu64 => Some("i"),
|
||||
va: &0x0a => Some(-1337),
|
||||
vb: &0x0b => Some(0xffffffffffffffffu64),
|
||||
&0x00 => Some("a"),
|
||||
&"a" => Some(0x01),
|
||||
&["b"] => Some(["c"]),
|
||||
&["c", "d"] => Some(["d", "e"]),
|
||||
&-0x04 => Some("e"),
|
||||
&0xff => Some("f"),
|
||||
&0xffff => Some("g"),
|
||||
&0xffffff => Some("h"),
|
||||
&0xffffffffu32 => Some("i"),
|
||||
&0xffffffffffffffffu64 => Some("i"),
|
||||
v1: &0x0a => Some(-1337),
|
||||
v2: &0x0b => Some(0xffffffffffffffffu64),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -440,5 +511,61 @@ mod tests {
|
|||
decode_hex("ac0061616161018161628161638261636164826164616523616518ff616619ffff61671a00ffffff61681affffffff61691bffffffffffffffff61690a3905380b1bffffffffffffffff")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize_map_optional_12_first_v1_absent() {
|
||||
struct Foo;
|
||||
impl Serialize for Foo {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
&0x00 => None::<i32>,
|
||||
&"a" => Some(0x01),
|
||||
&["b"] => Some(["c"]),
|
||||
&["c", "d"] => Some(["d", "e"]),
|
||||
&-0x04 => Some("e"),
|
||||
&0xff => Some("f"),
|
||||
&0xffff => Some("g"),
|
||||
&0xffffff => Some("h"),
|
||||
&0xffffffffu32 => Some("i"),
|
||||
&0xffffffffffffffffu64 => Some("i"),
|
||||
v1: &0x0a => Some(-1337),
|
||||
v2: &0x0b => Some(0xffffffffffffffffu64),
|
||||
)
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
serde_cbor::to_vec(&Foo).unwrap(),
|
||||
decode_hex("ab6161018161628161638261636164826164616523616518ff616619ffff61671a00ffffff61681affffffff61691bffffffffffffffff61690a3905380b1bffffffffffffffff")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize_map_optional_12_second_v1_absent() {
|
||||
struct Foo;
|
||||
impl Serialize for Foo {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_map_optional!(
|
||||
serializer,
|
||||
&0x00 => Some("a"),
|
||||
&"a" => Some(0x01),
|
||||
&["b"] => Some(["c"]),
|
||||
&["c", "d"] => Some(["d", "e"]),
|
||||
&-0x04 => Some("e"),
|
||||
&0xff => Some("f"),
|
||||
&0xffff => Some("g"),
|
||||
&0xffffff => Some("h"),
|
||||
&0xffffffffu32 => Some("i"),
|
||||
&0xffffffffffffffffu64 => Some("i"),
|
||||
v1: &0x0a => None::<i32>,
|
||||
v2: &0x0b => Some(0xffffffffffffffffu64),
|
||||
)
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
serde_cbor::to_vec(&Foo).unwrap(),
|
||||
decode_hex("ab0061616161018161628161638261636164826164616523616518ff616619ffff61671a00ffffff61681affffffff61691bffffffffffffffff61690b1bffffffffffffffff")
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче