Skip to main content

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!(
19            "Supports both text (.vdf) and binary (appinfo.vdf, packageinfo.vdf, shortcuts.vdf) formats."
20        );
21        std::process::exit(1);
22    }
23
24    let path = Path::new(&args[1]);
25    let data = std::fs::read(path).expect("Failed to read file");
26
27    // Try to detect format and parse accordingly
28    let result = if let Ok(text) = std::str::from_utf8(&data) {
29        // Looks like text, try text parser first
30        parse_text(text)
31            .map(|v| v.into_owned())
32            .or_else(|_| parse_binary(&data).map(|v| v.into_owned()))
33    } else {
34        // Binary data - detect format from filename
35        let filename = path
36            .file_name()
37            .and_then(|n| n.to_str())
38            .unwrap_or_default();
39
40        if filename.contains("packageinfo") {
41            binary::parse_packageinfo(&data).map(|v| v.into_owned())
42        } else if filename.contains("appinfo") {
43            binary::parse_appinfo(&data).map(|v| v.into_owned())
44        } else {
45            parse_binary(&data).map(|v| v.into_owned())
46        }
47    };
48
49    match result {
50        Ok(vdf) => {
51            // Use the pretty-print Display implementation
52            println!("{:#}", vdf);
53        }
54        Err(e) => {
55            eprintln!("Error parsing VDF: {:?}", e);
56            std::process::exit(1);
57        }
58    }
59}