1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Canonical serialization via IPLD dag-cbor.
//!
//! # dag-cbor *is* the canonical form
//!
//! This crate does not implement its own canonicalization rules. It relies on
//! the IPLD dag-cbor codec, whose encoding is **deterministic by
//! construction**:
//!
//! - **Strict map key ordering.** Map keys are emitted in a fixed, defined
//! order, so two semantically-equal maps always produce identical bytes
//! regardless of insertion order.
//! - **Definite-length encoding.** Arrays and maps carry an explicit length;
//! indefinite-length ("streaming") items are forbidden.
//! - **Tag 42 for links.** A [`Cid`](ipld_core::cid::Cid) is encoded as a CBOR
//! tag-42 byte string, the IPLD convention for a content link.
//! - **Smallest-form integers and no duplicate keys.**
//!
//! Because the codec enforces these rules, determinism is a property of the
//! *encoder*, not of caller discipline. Callers do not need to sort fields,
//! pick a field order, or avoid maps: `to_canonical_dagcbor` will always
//! produce the same bytes for the same value.
//!
//! This determinism is what makes content addressing sound: the same value
//! hashes to the same [`ContentId`](crate::ContentId), always.
use DeserializeOwned;
use Serialize;
use crateContentError;
/// Encode a value to its canonical dag-cbor byte representation.
///
/// The returned bytes are deterministic: equal values always encode to equal
/// bytes (see the [module docs](self) for why). These bytes are the input to
/// [`ContentId::from_canonical_bytes`](crate::ContentId::from_canonical_bytes).
///
/// # Errors
///
/// Returns [`ContentError::EncodingError`] if the value cannot be represented
/// as dag-cbor (for example, a float that dag-cbor forbids, or an allocation
/// failure).
/// Decode a value from canonical dag-cbor bytes.
///
/// # Errors
///
/// Returns [`ContentError::DecodingError`] if the bytes are not valid canonical
/// dag-cbor for the target type.