irontide-bencode 1.0.1

Serde-based bencode serialization for BitTorrent
Documentation

Serde-based bencode codec for BitTorrent.

Bencode is the serialization format used throughout BitTorrent for .torrent files, tracker responses, and DHT messages. It has four types:

  • Integers: i42e, i-1e, i0e
  • Byte strings: 4:spam (length-prefixed)
  • Lists: l<values>e
  • Dictionaries: d<key><value>...e (keys are byte strings, sorted)

Usage

use serde::{Serialize, Deserialize};
use irontide_bencode::{to_bytes, from_bytes};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Torrent {
    announce: String,
    #[serde(rename = "piece length")]
    piece_length: i64,
}

let torrent = Torrent {
    announce: "http://tracker.example.com/announce".into(),
    piece_length: 262144,
};

let encoded = to_bytes(&torrent).unwrap();
let decoded: Torrent = from_bytes(&encoded).unwrap();
assert_eq!(torrent, decoded);