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

torrust-serde-bencode

A Serde backed Bencode encoding/decoding library for Rust

1 unstable release

0.2.3 Sep 25, 2023

#32 in #bencode

Download history 94/week @ 2024-07-21 16/week @ 2024-07-28 100/week @ 2024-08-04 139/week @ 2024-08-11 18/week @ 2024-08-18 114/week @ 2024-08-25 82/week @ 2024-09-01 73/week @ 2024-09-08 84/week @ 2024-09-15 105/week @ 2024-09-22 72/week @ 2024-09-29 172/week @ 2024-10-06 91/week @ 2024-10-13 164/week @ 2024-10-20 69/week @ 2024-10-27 438/week @ 2024-11-03

762 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

~135–370KB