#bencode #serialize #deserialize #serde

serde_bencode

A Serde backed Bencode encoding/decoding library for Rust

8 releases

0.2.3 Dec 1, 2020
0.2.2 Jul 17, 2020
0.2.1 Feb 10, 2020
0.2.0 May 31, 2017
0.1.1 Dec 21, 2016

#188 in Encoding

Download history 176/week @ 2021-02-26 141/week @ 2021-03-05 155/week @ 2021-03-12 147/week @ 2021-03-19 158/week @ 2021-03-26 187/week @ 2021-04-02 147/week @ 2021-04-09 292/week @ 2021-04-16 125/week @ 2021-04-23 151/week @ 2021-04-30 185/week @ 2021-05-07 136/week @ 2021-05-14 84/week @ 2021-05-21 92/week @ 2021-05-28 135/week @ 2021-06-04 147/week @ 2021-06-11

708 downloads per month
Used in 10 crates (7 directly)

MIT license

41KB
922 lines

serde-bencode

A Serde backed Bencode encoding/decoding library for Rust.

Installation

Add the following to your Cargo.toml:

[dependencies]
serde_bencode = "^0.2.3"
serde = "^1.0.0"
serde_derive = "^1.0.0"

Usage

This is an abbreviated .torrent parsing example from examples/parse_torrent.rs. If you compile this crate as a binary, it will print metadata for any Torrent sent to stdin.


lib.rs:

This crate is a Rust library for using the Serde serialization framework with bencode data.

Examples

use serde_derive::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
struct Product {
    name: String,
    price: u32,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let apple = Product {
        name: "Apple".to_string(),
        price: 130,
    };

    let serialized = serde_bencode::to_string(&apple)?;

    assert_eq!(serialized, "d4:name5:Apple5:pricei130ee".to_string());

    let deserialized: Product = serde_bencode::from_str(&serialized)?;

    assert_eq!(
        deserialized,
        Product {
            name: "Apple".to_string(),
            price: 130,
        }
    );

    Ok(())
}

Dependencies

~170–290KB