#bencode

torrust-serde-bencode

A Serde backed Bencode encoding/decoding library for Rust

1 unstable release

0.2.3 Sep 25, 2023

#28 in #bencode

Download history 95/week @ 2024-07-02 114/week @ 2024-07-09 44/week @ 2024-07-16 88/week @ 2024-07-23 21/week @ 2024-07-30 106/week @ 2024-08-06 125/week @ 2024-08-13 22/week @ 2024-08-20 120/week @ 2024-08-27 81/week @ 2024-09-03 85/week @ 2024-09-10 71/week @ 2024-09-17 127/week @ 2024-09-24 92/week @ 2024-10-01 137/week @ 2024-10-08 119/week @ 2024-10-15

484 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