Encode credentials as a protocol buffer message

This permits to decode a credential of a different version without failing.
This commit is contained in:
Julien Cretin
2020-05-09 15:55:55 +02:00
parent a2eff7c632
commit f4b791ed91
5 changed files with 102 additions and 59 deletions

View File

@@ -41,18 +41,30 @@ macro_rules! cbor_map_options {
};
( $( $key:expr => $value:expr ),* ) => {
cbor_extend_map_options! (
::alloc::collections::BTreeMap::<_, $crate::values::Value>::new(),
$( $key => $value, )*
)
};
}
#[macro_export]
macro_rules! cbor_extend_map_options {
// Add trailing comma if missing.
( $initial:expr, $( $key:expr => $value:expr ),+ ) => {
cbor_extend_map_options! ( $initial, $($key => $value,)+ )
};
( $initial:expr, $( $key:expr => $value:expr, )* ) => {
{
// The import is unused if the list is empty.
#[allow(unused_imports)]
use $crate::values::{IntoCborKey, IntoCborValueOption};
let mut _map = ::alloc::collections::BTreeMap::<_, $crate::values::Value>::new();
let mut _map = $initial;
$(
{
let opt: Option<$crate::values::Value> = $value.into_cbor_value_option();
if let Some(val) = opt {
if let Some(val) = $value.into_cbor_value_option() {
_map.insert($key.into_cbor_key(), val);
}
}
)*
$crate::values::Value::Map(_map)
}

View File

@@ -36,7 +36,7 @@ impl<'a> Writer<'a> {
return false;
}
match value {
Value::KeyValue(KeyType::Unsigned(unsigned)) => self.start_item(0, unsigned as u64),
Value::KeyValue(KeyType::Unsigned(unsigned)) => self.start_item(0, unsigned),
Value::KeyValue(KeyType::Negative(negative)) => {
self.start_item(1, -(negative + 1) as u64)
}