print/
print.rs

1//! Pretty-print a VDF file using the `{:#}` Display format.
2//!
3//! Usage: cargo run --example pretty_print <vdf_file>
4//!
5//! This example demonstrates the pretty-print Display implementation that
6//! outputs valid VDF text format with proper indentation and escaping.
7
8use std::env;
9use std::path::Path;
10use steam_vdf_parser::{binary, parse_binary, parse_text};
11
12fn main() {
13    let args: Vec<String> = env::args().collect();
14    if args.len() < 2 {
15        eprintln!("Usage: {} <vdf_file>", args[0]);
16        eprintln!();
17        eprintln!("Pretty-prints a VDF file to stdout in valid VDF text format.");
18        eprintln!("Supports both text (.vdf) and binary (appinfo.vdf, packageinfo.vdf, shortcuts.vdf) formats.");
19        std::process::exit(1);
20    }
21
22    let path = Path::new(&args[1]);
23    let data = std::fs::read(path).expect("Failed to read file");
24
25    // Try to detect format and parse accordingly
26    let result = if let Ok(text) = std::str::from_utf8(&data) {
27        // Looks like text, try text parser first
28        parse_text(text)
29            .map(|v| v.into_owned())
30            .or_else(|_| parse_binary(&data).map(|v| v.into_owned()))
31    } else {
32        // Binary data - detect format from filename
33        let filename = path
34            .file_name()
35            .and_then(|n| n.to_str())
36            .unwrap_or_default();
37
38        if filename.contains("packageinfo") {
39            binary::parse_packageinfo(&data).map(|v| v.into_owned())
40        } else if filename.contains("appinfo") {
41            binary::parse_appinfo(&data).map(|v| v.into_owned())
42        } else {
43            parse_binary(&data).map(|v| v.into_owned())
44        }
45    };
46
47    match result {
48        Ok(vdf) => {
49            // Use the pretty-print Display implementation
50            println!("{:#}", vdf);
51        }
52        Err(e) => {
53            eprintln!("Error parsing VDF: {:?}", e);
54            std::process::exit(1);
55        }
56    }
57}