4 releases
3.0.0-alpha.11 | Sep 18, 2023 |
---|---|
3.0.0-alpha.10 | Sep 16, 2023 |
3.0.0-alpha.8 | Sep 15, 2023 |
#1349 in Encoding
49 downloads per month
Used in torrust-tracker
63KB
1.5K
SLoC
Bencode
This library allows for the creation and parsing of bencode encodings.
Bencode is the binary encoding used throughout bittorrent technologies from metainfo files to DHT messages. Bencode types include integers, byte arrays, lists, and dictionaries, of which the last two can hold any bencode type (they could be recursively constructed).
lib.rs
:
Library for parsing and converting bencoded data.
Examples
Decoding bencoded data:
extern crate bencode;
use std::default::Default;
use bencode::{BencodeRef, BRefAccess, BDecodeOpt};
fn main() {
let data = b"d12:lucky_numberi7ee"; // cspell:disable-line
let bencode = BencodeRef::decode(data, BDecodeOpt::default()).unwrap();
assert_eq!(7, bencode.dict().unwrap().lookup("lucky_number".as_bytes())
.unwrap().int().unwrap());
}
Encoding bencoded data:
#[macro_use]
extern crate bencode;
fn main() {
let message = (ben_map!{
"lucky_number" => ben_int!(7),
"lucky_string" => ben_bytes!("7")
}).encode();
let data = b"d12:lucky_numberi7e12:lucky_string1:7e"; // cspell:disable-line
assert_eq!(&data[..], &message[..]);
}
Dependencies
~2.5–3.5MB
~72K SLoC