Skip to main content

json_pretty_print/
json_pretty_print.rs

1use babbel_json::misc::print;
2use babbel_json::{FileDestination, FileSource, parse};
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 reading, parsing, and pretty-printing it
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("json_pp")
16            .to_string_lossy()
17            .as_ref(),
18    )
19    .map_err(|e| e.to_string())?;
20
21    print(&node, &mut destination, 4);
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) => eprintln!("Failed to convert {}: {}", file_path, e),
31        }
32    }
33}