#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

#456 in Encoding

Download history 596/week @ 2022-08-12 818/week @ 2022-08-19 463/week @ 2022-08-26 361/week @ 2022-09-02 518/week @ 2022-09-09 529/week @ 2022-09-16 473/week @ 2022-09-23 434/week @ 2022-09-30 547/week @ 2022-10-07 432/week @ 2022-10-14 384/week @ 2022-10-21 461/week @ 2022-10-28 441/week @ 2022-11-04 500/week @ 2022-11-11 565/week @ 2022-11-18 344/week @ 2022-11-25

1,914 downloads per month
Used in 14 crates (10 directly)

MIT license

42KB
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

~155–275KB