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//! | [`Float`] | IEEE 754 float stored in shortest CBOR form (f16, f32, or f64). |
32//!
33//! # Quick start
34//!
35//! ```
36//! use cbor_core::{Value, array, map};
37//!
38//! // Build a value
39//! let value = map! {
40//!     1 => "hello",
41//!     2 => array![10, 20, 30],
42//! };
43//!
44//! // Encode to bytes and decode back
45//! let bytes = value.encode();
46//! let decoded = Value::decode(&bytes).unwrap();
47//! assert_eq!(value, decoded);
48//!
49//! // Access inner data
50//! let greeting = decoded[1].as_str().unwrap();
51//! assert_eq!(greeting, "hello");
52//! ```
53//!
54//! # Encoding rules
55//!
56//! All encoding is deterministic: integers and floats use their shortest
57//! representation, and map keys are sorted in CBOR canonical order. The
58//! decoder rejects non-canonical input.
59//!
60//! NaN values, including signaling NaNs and custom payloads, are preserved
61//! through encode/decode round-trips. Conversion between float widths uses
62//! bit-level manipulation to avoid hardware NaN canonicalization.
63
64mod array;
65mod consts;
66mod data_type;
67mod error;
68mod float;
69mod integer;
70mod macros;
71mod map;
72mod simple_value;
73mod value;
74
75pub use array::Array;
76pub use data_type::DataType;
77pub use error::{Error, Result};
78pub use float::Float;
79pub use map::Map;
80pub use simple_value::SimpleValue;
81pub use value::Value;
82
83use consts::*;
84use integer::*;
85
86#[cfg(test)]
87mod tests;