1 unstable release
0.2.3 | Sep 25, 2023 |
---|
#28 in #bencode
484 downloads per month
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