Skip to main content

Crate bencode

Crate bencode 

Source
Expand description

Bencode — the BitTorrent encoding (BEP-3) as a typed AST.

Bencode is the wire format under every BitTorrent surface: .torrent metainfo, tracker announces, the DHT’s KRPC, the extension protocol. This crate is the keystone of the pleme-io ENXAME suite (theory/ENXAME.md): nothing else parses without it.

It is built in the pleme-io idiom — the TYPED-SPEC + INTERPRETER TRIPLET (the same shape as mado’s CsiCommand, engawa’s IR, sui-spec’s domains):

  • Typed borderBencode is the parse-don’t-validate AST. Bytes become one of four typed shapes; an ill-formed stream is a typed Error, never a panic.
  • Parserparse / parse_prefix: bytes → Bencode.
  • EmitterBencode::to_bytes: AST → bytes through a typed builder, never format!() of the wire syntax (the ★★ TYPED EMISSION rule). Dict keys serialize in sorted order — the canonical contract BitTorrent hashing depends on — guaranteed by the std::collections::BTreeMap backing Bencode::Dict.

The round-trip law parse(x.to_bytes()) == x and the canonical law parse(b).map(to_bytes) == b for canonical input are both pinned by property tests.

Enums§

Bencode
A bencoded value — the four shapes the grammar admits.
Error
A typed parse failure. Every malformed-stream condition is one of these — the AST is never built from invalid bytes.

Functions§

parse
Parse exactly one bencoded value from input, requiring that the value consumes the entire buffer.
parse_prefix
Parse one bencoded value from the front of input, returning the value and the unconsumed tail. Used when bencoded values are framed inside a larger stream (the peer-wire extension handshake, KRPC).