#bencode #serialization #serde #deserialize #serde-derive #debugging

torrust-serde-bencode

A Serde backed Bencode encoding/decoding library for Rust

1 unstable release

0.2.3 Sep 25, 2023

#6 in #serializing

Download history 53/week @ 2024-01-13 75/week @ 2024-01-20 58/week @ 2024-02-03 37/week @ 2024-02-10 60/week @ 2024-02-17 90/week @ 2024-02-24 24/week @ 2024-03-02 100/week @ 2024-03-09 36/week @ 2024-03-16 41/week @ 2024-03-23 85/week @ 2024-03-30 42/week @ 2024-04-06 199/week @ 2024-04-13 59/week @ 2024-04-20 86/week @ 2024-04-27

388 downloads per month

MIT license

42KB
1K SLoC

Torrust Serde Bencode

A Serde backed Bencode encoding/decoding library for Rust.

Forked from: https://github.com/toby/serde-bencode due to inactivity in upstream repo.

Installation

Add the following to your Cargo.toml:

[dependencies]
torrust-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

~145–385KB