Skip to main content

bencode_torrent_to_toml/
bencode_torrent_to_toml.rs

1//! Example demonstrating conversion of torrent files from bencode format to TOML format.
2//! Takes torrent files from the "files" directory and creates corresponding TOML files.
3
4use babbel_bencode::{FileDestination, FileSource, parse, to_toml};
5use std::path::Path;
6
7#[path = "common/utility.rs"]
8mod bencode_utility_lib;
9use bencode_utility_lib::get_torrent_file_list;
10
11/// Converts a single torrent file from bencode format to TOML format
12fn process_torrent_file(file_path: &str) -> Result<(), String> {
13    let mut source = FileSource::new(file_path).map_err(|e| e.to_string())?;
14    let node = parse(&mut source).map_err(|e| e.to_string())?;
15    let mut destination = FileDestination::new(
16        Path::new(file_path)
17            .with_extension("toml")
18            .to_string_lossy()
19            .as_ref(),
20    )
21    .map_err(|e| e.to_string())?;
22    to_toml(&node, &mut destination)?;
23    Ok(())
24}
25
26fn main() {
27    let torrent_files = get_torrent_file_list("files");
28    for file_path in torrent_files {
29        match process_torrent_file(&file_path) {
30            Ok(()) => println!("Successfully converted {}", file_path),
31            Err(e) => eprintln!("Failed to convert {}: {}", file_path, e),
32        }
33    }
34}