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

torrust-serde-bencode

A Serde backed Bencode encoding/decoding library for Rust

1 unstable release

0.2.3 Sep 25, 2023

#8 in #serializing

Download history 49/week @ 2023-12-17 73/week @ 2024-01-07 53/week @ 2024-01-14 75/week @ 2024-01-21 58/week @ 2024-02-04 39/week @ 2024-02-11 59/week @ 2024-02-18 91/week @ 2024-02-25 24/week @ 2024-03-03 100/week @ 2024-03-10 37/week @ 2024-03-17 39/week @ 2024-03-24 86/week @ 2024-03-31

264 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

~140–385KB