Module codec

Module codec 

Source
Expand description

MessagePack encoding/decoding for tree nodes

Blobs are stored raw (not wrapped) for efficiency. Tree nodes are MessagePack-encoded.

Determinism: Unlike CBOR, MessagePack doesn’t have a built-in canonical encoding. We ensure deterministic output by:

  1. Using fixed struct field order (Rust declaration order via serde)
  2. Converting HashMap metadata to BTreeMap before encoding (sorted keys)

Format uses short keys for compact encoding:

  • t: type (1 = File, 2 = Dir) - node type
  • l: links array
  • h: hash (in link)
  • t: type (in link, 0 = Blob, 1 = File, 2 = Dir)
  • n: name (in link, optional)
  • s: size (in link)
  • m: metadata (optional)

Enums§

CodecError
Error type for codec operations

Functions§

decode_tree_node
Decode MessagePack to a tree node
encode_and_hash
Encode a tree node and compute its hash
encode_tree_node
Encode a tree node to MessagePack
get_node_type
Get the type of data (Blob, File, or Dir) Returns LinkType::Blob for raw blobs that aren’t tree nodes
is_directory_node
Check if data is a directory tree node (node_type == Dir) Note: Prefer try_decode_tree_node() to avoid double decoding
is_tree_node
Check if data is a MessagePack-encoded tree node (vs raw blob) Tree nodes decode successfully with type = File or Dir Note: Prefer try_decode_tree_node() to avoid double decoding
try_decode_tree_node
Try to decode data as a tree node Returns Some(TreeNode) if valid tree node, None otherwise This is preferred over is_tree_node() to avoid double decoding