1 // SPDX-License-Identifier: Apache-2.0 2 3 //! Welcome to Ciborium! 4 //! 5 //! Ciborium contains CBOR serialization and deserialization implementations for serde. 6 //! 7 //! # Quick Start 8 //! 9 //! You're probably looking for [`de::from_reader()`](crate::de::from_reader) 10 //! and [`ser::into_writer()`](crate::ser::into_writer), which are 11 //! the main functions. Note that byte slices are also readers and writers and can be 12 //! passed to these functions just as streams can. 13 //! 14 //! For dynamic CBOR value creation/inspection, see [`value::Value`](crate::value::Value). 15 //! 16 //! # Design Decisions 17 //! 18 //! ## Always Serialize Numeric Values to the Smallest Size 19 //! 20 //! Although the CBOR specification has differing numeric widths, this is only 21 //! a form of compression on the wire and is not intended to directly 22 //! represent an "integer width" or "float width." Therefore, ciborium always 23 //! serializes numbers to the smallest possible lossless encoding. For example, 24 //! we serialize `1u128` as a single byte (`01`). Likewise, we will also freely 25 //! decode that single byte into a `u128`. 26 //! 27 //! While there is some minor performance cost for this, there are several 28 //! reasons for this choice. First, the specification seems to imply it by 29 //! using a separate bit for the sign. Second, the specification requires 30 //! that implementations handle leading zeroes; a liberal reading of which 31 //! implies a requirement for lossless coercion. Third, dynamic languages like 32 //! Python have no notion of "integer width," making this is a practical 33 //! choice for maximizing wire compatibility with those languages. 34 //! 35 //! This coercion is **always** lossless. For floats, this implies that we 36 //! only coerce to a smaller size if coercion back to the original size has 37 //! the same raw bits as the original. 38 //! 39 //! ## Compatibility with Other Implementations 40 //! 41 //! The ciborium project follows the [Robustness Principle](https://en.wikipedia.org/wiki/Robustness_principle). 42 //! Therefore, we aim to be liberal in what we accept. This implies that we 43 //! aim to be wire-compatible with other implementations in decoding, but 44 //! not necessarily encoding. 45 //! 46 //! One notable example of this is that `serde_cbor` uses fixed-width encoding 47 //! of numbers and doesn't losslessly coerce. This implies that `ciborium` will 48 //! successfully decode `serde_cbor` encodings, but the opposite may not be the 49 //! case. 50 //! 51 //! ## Representing Map as a Sequence of Values 52 //! 53 //! Other serde parsers have generally taken the route of using `BTreeMap` or 54 //! `HashMap` to implement their encoding's underlying `Map` type. This crate 55 //! chooses to represent the `Map` type using `Vec<(Value, Value)>` instead. 56 //! 57 //! This decision was made because this type preserves the order of the pairs 58 //! on the wire. Further, for those that need the properties of `BTreeMap` or 59 //! `HashMap`, you can simply `collect()` the values into the respective type. 60 //! This provides maximum flexibility. 61 //! 62 //! ## Low-level Library 63 //! 64 //! The ciborium crate has the beginnings of a low-level library in the 65 //! (private) `basic` module. We may extend this to be more robust and expose 66 //! it for application consumption once we have it in a good state. If you'd 67 //! like to collaborate with us on that, please contact us. Alternatively, 68 //! we might fork this code into a separate crate with no serde dependency. 69 //! 70 //! ## Internal Types 71 //! 72 //! The ciborium crate contains a number of internal types that implement 73 //! useful serde traits. While these are not currently exposed, we might 74 //! choose to expose them in the future if there is demand. Generally, this 75 //! crate takes a conservative approach to exposing APIs to avoid breakage. 76 //! 77 //! ## Packed Encoding? 78 //! 79 //! Packed encoding uses numerical offsets to represent structure field names 80 //! and enum variant names. This can save significant space on the wire. 81 //! 82 //! While the authors of this crate like packed encoding, it should generally 83 //! be avoided because it can be fragile as it exposes invariants of your Rust 84 //! code to remote actors. We might consider adding this in the future. If you 85 //! are interested in this, please contact us. 86 87 #![cfg_attr(not(feature = "std"), no_std)] 88 #![deny(missing_docs)] 89 #![deny(clippy::all)] 90 #![deny(clippy::cargo)] 91 #![allow(clippy::unit_arg)] 92 93 extern crate alloc; 94 95 pub mod de; 96 pub mod ser; 97 pub mod tag; 98 pub mod value; 99 100 /// Build a `Value` conveniently. 101 /// 102 /// The syntax should be intuitive if you are familiar with JSON. You can also 103 /// inline simple Rust expressions, including custom values that implement 104 /// `serde::Serialize`. Note that this macro returns `Result<Value, Error>`, 105 /// so you should handle the error appropriately. 106 /// 107 /// ``` 108 /// use ciborium::cbor; 109 /// 110 /// let value = cbor!({ 111 /// "code" => 415, 112 /// "message" => null, 113 /// "continue" => false, 114 /// "extra" => { "numbers" => [8.2341e+4, 0.251425] }, 115 /// }).unwrap(); 116 /// ``` 117 #[macro_export] 118 macro_rules! cbor { 119 (@map {$($key:expr => $val:expr),*} $(,)?) => {{ 120 $crate::value::Value::Map(vec![ 121 $( 122 (cbor!( $key )?, cbor!( $val )?) 123 ),* 124 ]) 125 }}; 126 127 (@map {$($key:expr => $val:expr),*} { $($nkey:tt)* } => $($next:tt)*) => { 128 cbor!( 129 @map 130 { $($key => $val),* } 131 cbor!({ $($nkey)* })? => 132 $($next)* 133 ) 134 }; 135 136 (@map {$($key:expr => $val:expr),*} [ $($nkey:tt)* ] => $($next:tt)*) => { 137 cbor!( 138 @map 139 { $($key => $val),* } 140 cbor!([ $($nkey)* ])? => 141 $($next)* 142 ) 143 }; 144 145 (@map {$($key:expr => $val:expr),*} $nkey:expr => { $($nval:tt)* }, $($next:tt)*) => { 146 cbor!( 147 @map 148 { $($key => $val,)* $nkey => cbor!({ $($nval)* })? } 149 $($next)* 150 ) 151 }; 152 153 (@map {$($key:expr => $val:expr),*} $nkey:expr => [ $($nval:tt)* ], $($next:tt)*) => { 154 cbor!( 155 @map 156 { $($key => $val,)* $nkey => cbor!([ $($nval)* ])? } 157 $($next)* 158 ) 159 }; 160 161 (@map {$($key:expr => $val:expr),*} $nkey:expr => $nval:expr, $($next:tt)*) => { 162 cbor!( 163 @map 164 { $($key => $val,)* $nkey => cbor!($nval)? } 165 $($next)* 166 ) 167 }; 168 169 (@seq [$($val:expr),*] $(,)?) => { 170 $crate::value::Value::Array( 171 vec![$( cbor!($val)? ),*] 172 ) 173 }; 174 175 (@seq [$($val:expr),*] { $($item:tt)* }, $($next:tt)*) => { 176 cbor!( 177 @seq 178 [ $($val,)* cbor!({ $($item)* })? ] 179 $($next)* 180 ) 181 }; 182 183 (@seq [$($val:expr),*] [ $($item:tt)* ], $($next:tt)*) => { 184 cbor!( 185 @seq 186 [ $($val,)* cbor!([ $($item)* ])? ] 187 $($next)* 188 ) 189 }; 190 191 (@seq [$($val:expr),*] $item:expr, $($next:tt)*) => { 192 cbor!( 193 @seq 194 [ $($val,)* $item ] 195 $($next)* 196 ) 197 }; 198 199 ({ $($next:tt)* }) => {(||{ 200 ::core::result::Result::<_, $crate::value::Error>::from(Ok(cbor!(@map {} $($next)* ,))) 201 })()}; 202 203 ([ $($next:tt)* ]) => {(||{ 204 ::core::result::Result::<_, $crate::value::Error>::from(Ok(cbor!(@seq [] $($next)* ,))) 205 })()}; 206 207 ($val:expr) => {{ 208 #[allow(unused_imports)] 209 use $crate::value::Value::Null as null; 210 $crate::value::Value::serialized(&$val) 211 }}; 212 } 213