Skip to main content

cbor_core/
lib.rs

1#![forbid(unsafe_code)]
2
3//! Deterministic CBOR encoder and decoder following the
4//! [CBOR::Core](https://www.ietf.org/archive/id/draft-rundgren-cbor-core-25.html)
5//! profile (draft-rundgren-cbor-core-25).
6//!
7//! This crate works with CBOR as an owned data structure rather than as a
8//! serialization layer. Values can be constructed, inspected, modified, and
9//! round-tripped through their canonical byte encoding.
10//!
11//! # Types
12//!
13//! [`Value`] is the central type and a good starting point. It represents
14//! any CBOR data item and provides constructors, accessors, encoding, and
15//! decoding.
16//!
17//! | Type | Role |
18//! |------|------|
19//! | [`Value`] | Any CBOR data item. Start here. |
20//! | [`SimpleValue`] | CBOR simple value (`null`, `true`, `false`, 0-255). |
21//! | [`DataType`] | Classification of a value for type-level dispatch. |
22//! | [`Error`] | All errors produced by this crate. |
23//!
24//! The following types are helpers that appear in `From`/`Into` bounds
25//! and are rarely used directly:
26//!
27//! | Type | Role |
28//! |------|------|
29//! | [`Array`] | Wrapper around `Vec<Value>` for flexible array construction. |
30//! | [`Map`] | Wrapper around `BTreeMap<Value, Value>` for flexible map construction. |
31//! | [`Integer`] | Integer representation including big integers (tags 2/3). |
32//! | [`Float`] | IEEE 754 float stored in shortest CBOR form (f16, f32, or f64). |
33//!
34//! # Quick start
35//!
36//! ```
37//! use cbor_core::{Value, array, map};
38//!
39//! // Build a value
40//! let value = map! {
41//!     1 => "hello",
42//!     2 => array![10, 20, 30],
43//! };
44//!
45//! // Encode to bytes and decode back
46//! let bytes = value.encode();
47//! let decoded = Value::decode(&bytes).unwrap();
48//! assert_eq!(value, decoded);
49//!
50//! // Access inner data
51//! let greeting = decoded.as_map().unwrap()[&Value::from(1)].as_str().unwrap();
52//! assert_eq!(greeting, "hello");
53//! ```
54//!
55//! # Encoding rules
56//!
57//! All encoding is deterministic: integers and floats use their shortest
58//! representation, and map keys are sorted in CBOR canonical order. The
59//! decoder rejects non-canonical input.
60//!
61//! NaN values, including signaling NaNs and custom payloads, are preserved
62//! through encode/decode round-trips. Conversion between float widths uses
63//! bit-level manipulation to avoid hardware NaN canonicalization.
64
65mod array;
66mod consts;
67mod data_type;
68mod error;
69mod float;
70mod integer;
71mod macros;
72mod map;
73mod simple_value;
74mod value;
75
76pub use array::Array;
77pub use data_type::DataType;
78pub use error::{Error, Result};
79pub use float::Float;
80pub use integer::Integer;
81pub use map::Map;
82pub use simple_value::SimpleValue;
83pub use value::Value;
84
85use consts::*;
86
87#[cfg(test)]
88mod tests;