Skip to main content

json_to_toml/
json_to_toml.rs

1use babbel_json::{FileDestination, FileSource, parse, to_toml};
2use std::fs;
3use std::path::Path;
4
5#[path = "common/utility.rs"]
6mod json_utility_lib;
7use json_utility_lib::get_json_file_list;
8
9/// Processes a single JSON file by converting it to TOML format.
10fn process_json_file(file_path: &str) -> Result<(), String> {
11    let mut source = FileSource::new(file_path).map_err(|e| e.to_string())?;
12    let node = parse(&mut source).map_err(|e| e.to_string())?;
13    let mut destination = FileDestination::new(
14        Path::new(file_path)
15            .with_extension("toml")
16            .to_string_lossy()
17            .as_ref(),
18    )
19    .map_err(|e| e.to_string())?;
20
21    to_toml(&node, &mut destination)?;
22    Ok(())
23}
24
25fn main() {
26    let json_files = get_json_file_list("files");
27    for file_path in json_files {
28        match process_json_file(&file_path) {
29            Ok(()) => println!("Successfully converted {}", file_path),
30            Err(e) => {
31                eprintln!("Failed to convert {}: {}", file_path, e);
32                let toml_file = file_path.replace(".json", ".toml");
33                let _ = fs::remove_file(toml_file);
34            }
35        }
36    }
37}