#bencode

bt_bencode

A Bencode encoding/decoding implementation

4 releases (breaking)

0.4.0 May 6, 2021
0.3.0 Oct 11, 2020
0.2.0 Feb 20, 2020
0.1.0 Feb 18, 2020
Download history 27/week @ 2021-02-26 18/week @ 2021-03-05 18/week @ 2021-03-12 24/week @ 2021-03-19 21/week @ 2021-03-26 10/week @ 2021-04-02 23/week @ 2021-04-09 58/week @ 2021-04-16 56/week @ 2021-04-23 44/week @ 2021-04-30 40/week @ 2021-05-07 28/week @ 2021-05-14 18/week @ 2021-05-21 19/week @ 2021-05-28 17/week @ 2021-06-04 23/week @ 2021-06-11

129 downloads per month

MIT/Apache

110KB
3.5K SLoC

BtBencode

BtBencode is a library which can help with Bencode encoding/decoding. Bencode is primarily used in BitTorrent related applications.

It uses the Serde library to serialize and deserialize Bencode data.

Installation

[dependencies]
bt_bencode = "0.4.0"

License

Licensed under either of Apache License, Version 2.0 or MIT License at your option.

Contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

BtBencode is a library which can help with Bencode encoding/decoding. Bencode is primarily used in BitTorrent related applications.

It uses the Serde library to serialize and deserialize Bencode data.

Examples

An example serializing from a standard Rust collection type into a custom type:

# use bt_bencode::Error;
# use std::collections::BTreeMap;
#
# fn main() -> Result<(), Error> {
use serde_bytes::ByteBuf;
use serde_derive::Deserialize;

let mut dict: BTreeMap<String, String> = BTreeMap::new();
dict.insert(String::from("url"), String::from("https://example.com/"));

let serialized_bytes = bt_bencode::to_vec(&dict)?;

#[derive(Deserialize)]
struct Info {
    url: String,
}

let info: Info = bt_bencode::from_slice(&serialized_bytes)?;
assert_eq!(info.url, "https://example.com/");
#   Ok(())
# }

An example deserializing from an unknown slice of bytes, and then into a custom type.

# use bt_bencode::Error;
# use std::collections::BTreeMap;
#
# fn main() -> Result<(), Error> {
use bt_bencode::Value;
use serde_bytes::ByteBuf;
use serde_derive::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Info {
    t: String,
    url: String,
}

let serialized_bytes = bt_bencode::to_vec(&Info {
    t: String::from("query"),
    url: String::from("https://example.com/"),
})?;

let value: Value = bt_bencode::from_slice(&serialized_bytes)?;
assert_eq!(value["t"].as_str().ok_or(Error::UnsupportedType)?, "query");

let info: Info = bt_bencode::from_value(value)?;
assert_eq!(info.url, "https://example.com/");
#   Ok(())
# }

Dependencies

~180–305KB