cbor!() { /* proc-macro */ }Expand description
Create a CBOR slice out of diagnostic notation (EDN)
It requires that the EDN also complies with Rust’s tokenization rules. This is the case for many simple expressions:
use cbor_macro::cbor;
let _ = cbor!([1, 2, "three"]);
let _ = cbor!({-1: 4, "x" "y": 1(1234567890), []: 3.14, simple(0): 1});Not all EDN is valid by Rust’s tokenizer rules, including “complex” comments and some forms of string escaping:
let _ = cbor!([1 / Funny comment :-) /, 2]);let _ = cbor!("zero \u0000");Also, there may exist EDN values that, while being both valid EDN and tokenizable Rust, lose information in the course of tokenization, and are reconstructed wrong. No working examples were found so far; the most “promising” candidates are line breaks within a string, exotic escapes, and application oriented literals.
For such cases, the cbo! macro provides a solution.
The output of the macro is a cboritem::CborItem, which is a newtype around a slice. Thus,
when storing the output of cbor! in static memory (as it is often useful), define a const to
avoid the added indirection and static memory usage of the extra pointer:
fn aead_feed_ad(data: &[u8]) { /* ... */ }
use cboritem::CborItem;
const ALGS: CborItem<'static> = cbor!([10, null, -8, -27]);
aead_feed_ad(&ALGS);Note that as always with macros, any style of parentheses around the expression is discarded,
and not reported. Thus, while cbor![1] is legal, it is still just the number 1, not an
array containing 1.