pub enum Bencode {
Int(i64),
Bytes(Vec<u8>),
List(Vec<Bencode>),
Dict(BTreeMap<Vec<u8>, Bencode>),
}Expand description
A bencoded value — the four shapes the grammar admits.
Bytes is the only string type: bencode has no text/binary
distinction on the wire, so the AST keeps raw bytes and offers
Bencode::as_str for the UTF-8 view when a field is known textual.
Dict is a BTreeMap so iteration — and therefore
Bencode::to_bytes — is always in the sorted-key canonical order
the protocol requires.
Variants§
Int(i64)
i<decimal>e — a 64-bit signed integer.
Bytes(Vec<u8>)
<len>:<bytes> — a length-prefixed byte string.
List(Vec<Bencode>)
l<elements>e — an ordered list.
Dict(BTreeMap<Vec<u8>, Bencode>)
d<key><value>…e — keys are byte strings, sorted + unique.
Implementations§
Source§impl Bencode
impl Bencode
Sourcepub fn to_bytes(&self) -> Vec<u8> ⓘ
pub fn to_bytes(&self) -> Vec<u8> ⓘ
Encode to canonical bencode bytes through a typed builder — never
a format!() of the wire syntax (the ★★ TYPED EMISSION rule).
Dict keys emit in sorted order (BTreeMap iteration), the
canonicalization BitTorrent info-hashing relies on.
Sourcepub fn as_int(&self) -> Option<i64>
pub fn as_int(&self) -> Option<i64>
The integer, if this is an Bencode::Int.
Sourcepub fn as_bytes(&self) -> Option<&[u8]>
pub fn as_bytes(&self) -> Option<&[u8]>
The raw bytes, if this is a Bencode::Bytes.
Sourcepub fn as_list(&self) -> Option<&[Bencode]>
pub fn as_list(&self) -> Option<&[Bencode]>
The elements, if this is a Bencode::List.