core:cbor
CBOR serialization and deserialization (RFC 8949, STD 94).
Implements the core:serde Serializer and Deserializer traits for the
Concise Binary Object Representation. The data model is a strict superset of
JSON's, so any type that serializes to core:json serializes to CBOR with no
source changes — the derived Serialize/Deserialize impls drive both.
The API is bytes-only (CBOR is binary).
Encoding uses preferred (shortest) serialization (RFC 8949 §4.1). Decoding is
variation-tolerant (§4.1): it accepts any well-formed encoding, preferred or
not, definite or indefinite length. to_bytes_canonical emits the Core
Deterministic Encoding profile (§4.2.1) for COSE-style signing, with the
documented float caveat (Wado has no f16, so the canonical float ladder
stops at binary32).
The spec is vendored verbatim at wado-compiler/ref/rfc8949.txt; comments
cite section numbers from it.
Synopsis
struct Point {
x: i32,
y: i32,
}
impl Serialize for Point;
impl Deserialize for Point;
let encoded = to_bytes(&Point { x: 1, y: 2 });
assert encoded matches { Ok(b) && from_bytes::<Point>(b) matches { Ok(p) && p.x == 1 && p.y == 2 } };
Functions
pub fn to_bytes<T: Serialize>(value: &T) -> Result<ByteList, SerializeError>
Serialize a value to CBOR using preferred (shortest) serialization.
Not guaranteed to be deterministic across equal values whose maps were built
in different orders; use to_bytes_canonical when byte-exact reproducibility
matters (e.g. COSE/CWT signing).
pub fn to_bytes_canonical<T: Serialize>(value: &T) -> Result<ByteList, SerializeError>
Serialize a value to CBOR using the Core Deterministic Encoding profile (RFC 8949 §4.2.1): preferred integer/length/tag forms, definite-length containers, and map keys sorted by the bytewise order of their encoded form. Equal values produce byte-identical output regardless of map insertion order — use this for COSE/CWT signing or content addressing.
Float caveat: Wado has no f16, so the canonical float ladder stops at
binary32. Output is byte-identical to a reference deterministic encoder for
integers, lengths, and map order, but may differ on float-bearing values.
pub fn from_bytes<T: Deserialize, B: AsByteSlice>(input: B, strict: bool = true) -> Result<T, DeserializeError>
Deserialize a value from CBOR — the primary entry point.
Accepts any byte source via AsByteSlice (ByteList, ByteArray,
ByteSlice, or a String's bytes), scanned directly with no copy. Decoding
is variation-tolerant (RFC 8949 §4.1): any well-formed encoding is accepted.
strict (default true) rejects items the target cannot represent; set
strict = false only when deserializing into core:value's Value, where
unrepresentable items fall to Value::Unknown instead of erroring.
Structs
pub struct CborSerializer
Fields are private.
impl Serializer for CborSerializer
fn serialize_i32(&mut self, v: i32) -> Result<(), SerializeError>
fn serialize_i64(&mut self, v: i64) -> Result<(), SerializeError>
fn serialize_u32(&mut self, v: u32) -> Result<(), SerializeError>
fn serialize_u64(&mut self, v: u64) -> Result<(), SerializeError>
fn serialize_i128(&mut self, v: i128) -> Result<(), SerializeError>
fn serialize_u128(&mut self, v: u128) -> Result<(), SerializeError>
fn serialize_f32(&mut self, v: f32) -> Result<(), SerializeError>
fn serialize_f64(&mut self, v: f64) -> Result<(), SerializeError>
fn serialize_bool(&mut self, v: bool) -> Result<(), SerializeError>
fn serialize_char(&mut self, v: char) -> Result<(), SerializeError>
fn serialize_string(&mut self, v: &String) -> Result<(), SerializeError>
fn serialize_tag(&mut self, tag: u64) -> Result<(), SerializeError>
fn serialize_bytes(&mut self, v: ByteSlice) -> Result<(), SerializeError>
fn serialize_null(&mut self) -> Result<(), SerializeError>
fn begin_seq(&mut self, len: i32) -> Result<CborSeqSerializer, SerializeError>
fn begin_map(&mut self, len: i32) -> Result<CborMapSerializer, SerializeError>
fn begin_struct(&mut self, name: &String, fields: i32) -> Result<CborStructSerializer, SerializeError>
fn serialize_unit_variant(&mut self, type_name: &String, variant_name: &String, disc: i32) -> Result<(), SerializeError>
fn begin_variant(&mut self, type_name: &String, variant_name: &String, disc: i32) -> Result<CborVariantSerializer, SerializeError>
pub struct CanonicalCborSerializer
Fields are private.
impl Serializer for CanonicalCborSerializer
fn serialize_i32(&mut self, v: i32) -> Result<(), SerializeError>
fn serialize_i64(&mut self, v: i64) -> Result<(), SerializeError>
fn serialize_u32(&mut self, v: u32) -> Result<(), SerializeError>
fn serialize_u64(&mut self, v: u64) -> Result<(), SerializeError>
fn serialize_i128(&mut self, v: i128) -> Result<(), SerializeError>
fn serialize_u128(&mut self, v: u128) -> Result<(), SerializeError>
fn serialize_f32(&mut self, v: f32) -> Result<(), SerializeError>
fn serialize_f64(&mut self, v: f64) -> Result<(), SerializeError>
fn serialize_bool(&mut self, v: bool) -> Result<(), SerializeError>
fn serialize_char(&mut self, v: char) -> Result<(), SerializeError>
fn serialize_string(&mut self, v: &String) -> Result<(), SerializeError>
fn serialize_tag(&mut self, tag: u64) -> Result<(), SerializeError>
fn serialize_bytes(&mut self, v: ByteSlice) -> Result<(), SerializeError>
fn serialize_null(&mut self) -> Result<(), SerializeError>
fn begin_seq(&mut self, len: i32) -> Result<CanonicalCborSeqSerializer, SerializeError>
fn begin_map(&mut self, len: i32) -> Result<CanonicalCborMapSerializer, SerializeError>
fn begin_struct(&mut self, name: &String, fields: i32) -> Result<CanonicalCborStructSerializer, SerializeError>
fn serialize_unit_variant(&mut self, type_name: &String, variant_name: &String, disc: i32) -> Result<(), SerializeError>
fn begin_variant(&mut self, type_name: &String, variant_name: &String, disc: i32) -> Result<CanonicalCborVariantSerializer, SerializeError>
pub struct CborDeserializer
Fields are private.
impl Deserializer for CborDeserializer
fn deserialize_i32(&mut self) -> Result<i32, DeserializeError>
fn deserialize_i64(&mut self) -> Result<i64, DeserializeError>
fn deserialize_u32(&mut self) -> Result<u32, DeserializeError>
fn deserialize_u64(&mut self) -> Result<u64, DeserializeError>
fn deserialize_i128(&mut self) -> Result<i128, DeserializeError>
fn deserialize_u128(&mut self) -> Result<u128, DeserializeError>
fn deserialize_f32(&mut self) -> Result<f32, DeserializeError>
fn deserialize_f64(&mut self) -> Result<f64, DeserializeError>
fn deserialize_bool(&mut self) -> Result<bool, DeserializeError>
fn deserialize_char(&mut self) -> Result<char, DeserializeError>
fn deserialize_string(&mut self) -> Result<String, DeserializeError>
fn deserialize_bytes(&mut self) -> Result<ByteList, DeserializeError>
fn is_null(&mut self) -> Result<bool, DeserializeError>
fn begin_container(&mut self, want_major: u8, expected: &String) -> Result<[i64, bool], DeserializeError>
Open a definite/indefinite container of the expected major type,
transparently unwrapping any self-described prefix and entering the
depth guard. Returns [remaining, indef] for the access to carry
(remaining is -1 for an indefinite container).
expected is the type-mismatch diagnostic, passed by reference (a
module-global constant) so the common success path allocates nothing;
the message is only copied on the cold error path.
