Repository: google/OpenSK
File: libraries/cbor/src/macros.rs (Rust)

Code before:
            use $crate::values::{IntoCborKey, IntoCborValue};
            let mut _map = ::alloc::collections::BTreeMap::new();
            $(
                _map.insert($key.into_cbor_key(), $value.into_cbor_value());
            )*
            $crate::values::Value::Map(_map)
        }
    };
}

#[macro_export]
macro_rules! cbor_map_options {
    // trailing comma case
    ( $( $key:expr => $value:expr, )+ ) => {
        cbor_map_options! ( $($key => $value),+ )
    };

    ( $( $key:expr => $value:expr ),* ) => {
        {
            let mut _map = ::alloc::collections::BTreeMap::<_, $crate::values::Value>::new();
            extend_cbor_map_options! ( &mut _map, $( $key => $value, )* );
            $crate::values::Value::Map(_map)
        }
    };
}

#[macro_export]
macro_rules! extend_cbor_map_options {
    // Add trailing comma if missing.
    ( &mut $initial:expr, $( $key:expr => $value:expr ),+ ) => {
        extend_cbor_map_options! ( &mut $initial, $($key => $value,)+ )
    };

    ( &mut $initial:expr, $( $key:expr => $value:expr, )* ) => {
        {
            // The import is unused if the list is empty.
            #[allow(unused_imports)]
            use $crate::values::{IntoCborKey, IntoCborValueOption};
            $(
                if let Some(val) = $value.into_cbor_value_option() {
                    $initial.insert($key.into_cbor_key(), val);
                }
            )*
        }
    };
}

#[macro_export]
macro_rules! cbor_map_btree {
    ( $tree:expr ) => {
        $crate::values::Value::Map($tree)

Review comment (suggestion):
IIRC it was necessary to write the type here so that "into" knows into what type to convert it. In any case, the explicit type doesn't hurt readability of the macro's code.

Code after:
            use $crate::values::{IntoCborKey, IntoCborValue};
            let mut _map = ::alloc::collections::BTreeMap::new();
            $(
                _map.insert($key.into_cbor_key(), $value.into_cbor_value());
            )*
            $crate::values::Value::Map(_map)
        }
    };
}

#[macro_export]
macro_rules! cbor_map_options {
    // trailing comma case
    ( $( $key:expr => $value:expr, )+ ) => {
        cbor_map_options! ( $($key => $value),+ )
    };

    ( $( $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 opt: Option<$crate::values::Value> = $value.into_cbor_value_option();
                if let Some(val) = opt {
                    _map.insert($key.into_cbor_key(), val);
                }
            }
            )*
            $crate::values::Value::Map(_map)
        }
    };
}

#[macro_export]
macro_rules! cbor_map_btree {
    ( $tree:expr ) => {
        $crate::values::Value::Map($tree)
    };
}

#[macro_export]
macro_rules! cbor_array {
    // trailing comma case
    ( $( $value:expr, )+ ) => {
        cbor_array! ( $($value),+ )
    };

    ( $( $value:expr ),* ) => {