Skip to main content

cbor

Macro cbor 

Source
macro_rules! cbor {
    (@array [$($elems:expr,)*]) => { ... };
    (@array [$($elems:expr),*]) => { ... };
    (@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => { ... };
    (@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => { ... };
    (@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => { ... };
    (@array [$($elems:expr,)*] $last:expr) => { ... };
    (@array [$($elems:expr),*] , $($rest:tt)*) => { ... };
    (@array [$($elems:expr),*] $unexpected:tt $($rest:tt)*) => { ... };
    (@map [$($pairs:expr,)*] () () ()) => { ... };
    (@map [$($pairs:expr,)*] [$($key:tt)+] ($value:expr) , $($rest:tt)*) => { ... };
    (@map [$($pairs:expr,)*] [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => { ... };
    (@map [$($pairs:expr,)*] [$($key:tt)+] ($value:expr)) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)+) (=> [$($array:tt)*] $($rest:tt)*) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)+) (=> {$($map:tt)*} $($rest:tt)*) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)+) (=> $value:expr , $($rest:tt)*) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)+) (: $value:expr) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)+) (=> $value:expr) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)+) (:) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)+) (=>) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)+) () $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] () (: $($rest:tt)*) ($sep:tt $($copy:tt)*)) => { ... };
    (@map [$($pairs:expr,)*] () (=> $($rest:tt)*) ($sep:tt $($copy:tt)*)) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => { ... };
    (@map [$($pairs:expr,)*] () (($key:expr) : $($rest:tt)*) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] () (($key:expr) => $($rest:tt)*) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)*) (: $($unexpected:tt)+) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)*) (=> $($unexpected:tt)+) $copy:tt) => { ... };
    (@map [$($pairs:expr,)*] ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => { ... };
    (@key {$($map:tt)*}) => { ... };
    (@key [$($array:tt)*]) => { ... };
    (@key $key:expr) => { ... };
    (@leaf $val:expr) => { ... };
    ({ $($map:tt)* }) => { ... };
    ([ $($array:tt)* ]) => { ... };
    ($val:expr) => { ... };
}
Expand description

Builds a Value from JSON-like syntax.

Maps use : between keys and values, exactly like serde_json::json!; any expression implementing serde::Serialize can be inlined, including nested cbor! maps and arrays. Going beyond JSON, map keys may be any CBOR value — integers included — and null is the CBOR null. The macro returns Result<Value, value::Error>.

use cbor2::cbor;

let value = cbor!({
    "code": 415,
    "message": null,
    "continue": false,
    "extra": { "numbers": [8.2341e+4, 0.251425] },
    1: "an integer key",
}).unwrap();

The ciborium-style => separator is accepted as well, and is handy when a key expression itself contains a colon (alternatively, parenthesize the key):

use cbor2::cbor;

const ALG: i8 = 1;

let value = cbor!({ ALG => -7, (i8::MAX) : 0 }).unwrap();