Skip to main content

bencode_full/
bencode_full.rs

1//! Full-featured bencode example with all format conversions
2//!
3//! This example demonstrates the library with all default features enabled,
4//! including all format conversions (JSON, TOML, XML, YAML).
5
6use babbel_bencode::{
7    BufferDestination, parse_bytes, stringify_to_bytes, to_json, to_toml, to_xml, to_yaml,
8};
9
10fn main() {
11    // Parse a bencode dictionary
12    let dict = b"d3:agei25e4:name4:Johne";
13
14    if let Ok(node) = parse_bytes(dict) {
15        // Convert back to bencode
16        if let Ok(output) = stringify_to_bytes(&node) {
17            assert_eq!(output, dict);
18            println!("Bencode: {}", String::from_utf8_lossy(&output));
19        }
20
21        // All format conversions available
22        let mut buffer = BufferDestination::new();
23        if to_json(&node, &mut buffer).is_ok() {
24            println!("JSON: {}", String::from_utf8_lossy(&buffer.buffer));
25        }
26
27        let mut buffer = BufferDestination::new();
28        if to_toml(&node, &mut buffer).is_ok() {
29            println!("TOML: {}", String::from_utf8_lossy(&buffer.buffer));
30        }
31
32        let mut buffer = BufferDestination::new();
33        if to_xml(&node, &mut buffer).is_ok() {
34            println!("XML: {}", String::from_utf8_lossy(&buffer.buffer));
35        }
36
37        let mut buffer = BufferDestination::new();
38        if to_yaml(&node, &mut buffer).is_ok() {
39            println!("YAML: {}", String::from_utf8_lossy(&buffer.buffer));
40        }
41    }
42}