Crates.io | bencode_lib |
lib.rs | bencode_lib |
version | 0.1.2 |
created_at | 2025-08-24 18:07:59.511932+00 |
updated_at | 2025-09-04 20:18:13.740407+00 |
description | Bencode library implementation. |
homepage | |
repository | |
max_upload_size | |
id | 1808644 |
size | 80,888 |
A Rust library for parsing, constructing, and converting Bencode data. In addition to round‑tripping Bencode, it can render parsed data to JSON, YAML, and XML.
Bencode is a compact serialization format commonly used by BitTorrent. It supports four types: integers, byte strings, lists, and dictionaries.
Node
)Node
back to canonical BencodeNode
to JSON, YAML, or XMLAdd to your Cargo.toml:
toml
[dependencies]
bencode_lib = "0.1.0"
toml
[dependencies]
bencode_lib = { path = "library" }
Parse a .torrent
(or any bencode) file and write it as YAML:
rust
use bencode_lib::{FileSource, FileDestination, parse, to_yaml};
use std::path::Path;
fn main() -> Result<(), String> {
let input_path = "example.torrent";
let output_path = Path::new(input_path).with_extension("yaml");
// Read and parse bencode
let mut src = FileSource::new(input_path)?;
let node = parse(&mut src)?;
// Convert to YAML and write out
let mut dst = FileDestination::new(output_path.to_string_lossy().as_ref())?;
to_yaml(&node, &mut dst);
Ok(())
}
Round‑trip a Bencode buffer:
rust
use bencode_lib::{BufferSource, BufferDestination, parse, stringify};
fn main() -> Result<(), String> {
let raw = b"d3:foo3:bar4:spamli1ei2ei3eee".to_vec();
let mut src = BufferSource::new(raw);
// Parse from memory
let node = parse(&mut src)?;
// Serialize back to bencode into memory
let mut dst = BufferDestination::new();
stringify(&node, &mut dst);
// If your BufferDestination exposes the bytes, you can inspect them:
// let bytes = dst.into_bytes();
Ok(())
}
Construct a Node
and render as JSON:
rust
use bencode_lib::{Node, to_json, BufferDestination};
fn main() {
let node = Node::Dict(vec![
(b"info".to_vec(), Node::List(vec![
Node::Integer(42),
Node::String(b"hello".to_vec()),
])),
]);
let mut dst = BufferDestination::new();
to_json(&node, &mut dst);
let json = String::from_utf8(dst.into_bytes()).unwrap();
println!("{}", json);
}
Read and write whole files with helpers:
rust
use bencode_lib::{read_file, write_file, Node};
fn main() -> Result<(), String> {
let node = read_file("input.bencode")?;
// ... mutate or inspect `node` ...
write_file("output.bencode", &node)?;
Ok(())
}
Node::Integer(i64)
Node::String(Vec<u8>)
Node::List(Vec<Node>)
Node::Dict(Vec<(Vec<u8>, Node)>)
FileSource
, FileDestination
BufferSource
, BufferDestination
Node
— in‑memory representation of Bencodeparse(&mut Source) -> Result<Node, String>
stringify(&Node, &mut Destination)
to_json(&Node, &mut Destination)
to_yaml(&Node, &mut Destination)
to_xml(&Node, &mut Destination)
version() -> String
— library versionread_file(path) -> Result<Node, String>
write_file(path, &Node) -> Result<(), String>
Result<Node, String>
.Result<…, String>
.This project is licensed under the MIT License. See the LICENSE file for details.