Crate bt_bencode
source · [−]Expand description
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.
Documentation
Installation
[dependencies]
bt_bencode = "0.6.1"Examples
An example serializing a standard Rust collection type and then deserializing into a custom type:
use std::collections::BTreeMap;
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/");An example deserializing from a slice of bytes into a general Value
representation and then from the Value instance into a more strongly typed
data structure.
use serde_derive::{Serialize, Deserialize};
use bt_bencode::Value;
#[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().unwrap(), "query");
assert_eq!(
value.get("url").and_then(|url| url.as_str()).unwrap(),
"https://example.com/"
);
let info: Info = bt_bencode::from_value(value)?;
assert_eq!(info.t, "query");
assert_eq!(info.url, "https://example.com/");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.
Modules
Represents valid Bencode data.
Structs
A Bencode Deserializer for types which implement Deserialize.
A Bencode Serializer for types which implement Serialize.
Enums
Functions
Deserializes an instance of T from the bytes of an io::Read type.
Deserializes an instance of T from a slice of bytes.
Deserializes an instance of T from a Value.
Serializes an instance of T into the writer W as Bencode data.
Type Definitions
Alias for a Result with a bt_bencode::Error error type.