[][src]Crate bt_bencode

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 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/");

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

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/");

Modules

value

Represents valid Bencode data.

Structs

Deserializer

A Bencode Deserializer for types which implement Deserialize.

Serializer

A Bencode Serializer for types which implement Serialize.

Enums

Error

All possible crate errors.

Value

Represents a valid Bencode value.

Functions

from_reader

Deserializes an instance of T from the bytes of an io::Read type.

from_slice

Deserializes an instance of T from a slice of bytes.

from_value

Deserializes an instance of T from a Value.

to_value

Serializes an instance of T into a Value.

to_vec

Serializes an instance of T into a new Vec as Bencode data.

to_writer

Serializes an instance of T into the writer W as Bencode data.

Type Definitions

Result

Alias for a Result with a bt_bencode::Error error type.