A Serde backed Bencode encoding/decoding library for Rust.
Add the following to your Cargo.toml
:
toml
[dependencies]
serde = "0.8.19"
serde_derive = "0.8.19"
serde_bencode = "0.1.0"
Serde works best with Rust nightly, it is highly recommended that you use it.
This is an abbreviated .torrent
parsing example from
main.rs. If you compile this crate as a binary, it will
print metadata for any Torrent sent to stdin.
More examples are available in test.rs.
```rust
extern crate serde_bencode; extern crate serde;
extern crate serde_derive;
use serde_bencode::decoder; use std::io::{self, Read}; use serde::bytes::ByteBuf;
struct Node(String, i64);
struct File {
path: Vec
struct Info {
name: String,
pieces: ByteBuf,
#[serde(rename="piece length")]
piecelength: i64,
#[serde(default)]
md5sum: Option
struct Torrent {
info: Info,
#[serde(default)]
announce: Option
fn main() {
let stdin = io::stdin();
let mut buffer = Vec::new();
let mut handle = stdin.lock();
match handle.readtoend(&mut buffer) {
Ok() => {
match decoder::frombytes::
}
} ```
There is a Bencode enum provided when any valid
Bencode value is needed in a single typed container. For example you can
use it to serialize/deserialize type Vec<Bencode>
:
rust
let list: Vec<Bencode> = vec!["one".into(), "two".into(), "three".into(), 4i64.into()];
let mut ser = Encoder::new();
list.serialize(&mut ser).unwrap();
let list_serialize: Vec<u8> = ser.into();
assert_eq!(String::from_utf8(list_serialize).unwrap(), "l3:one3:two5:threei4ee");
In the main.rs
example you'll notice that the torrent.info.pieces
is
a serde::bytes::ByteBuf
. This is a wrapper type provided by Serde that
allows Vec<u8>
to be decoded as a Bencode ByteString instead of a
List.