-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
66 lines (56 loc) · 1.71 KB
/
Copy pathmain.rs
File metadata and controls
66 lines (56 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use anyhow::{Context, Result};
use clap::Parser;
use hk_parser::{parse_hk, resolve_interpolations};
use std::fs;
use std::path::PathBuf;
/// Hacker Lang configuration parser CLI
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Input .hk file
#[arg(short, long)]
input: PathBuf,
/// Validate only (don't resolve interpolations)
#[arg(short, long)]
validate: bool,
/// Resolve interpolations and output the result
#[arg(short, long)]
resolve: bool,
/// Output file (if not provided, print to stdout)
#[arg(short, long)]
output: Option<PathBuf>,
/// Pretty print errors with colors
#[arg(long, default_value_t = true)]
color: bool,
}
fn main() -> Result<()> {
let args = Args::parse();
// Read the file
let contents = fs::read_to_string(&args.input)
.with_context(|| format!("Failed to read input file: {}", args.input.display()))?;
// Parse
let parse_result = parse_hk(&contents);
match parse_result {
Ok(mut config) => {
if args.resolve {
// Resolve interpolations
if let Err(e) = resolve_interpolations(&mut config) {
e.pretty_print(&contents);
std::process::exit(1);
}
}
// Output
if let Some(output_path) = args.output {
hk_parser::write_hk_file(output_path, &config)?;
} else {
// Print to stdout
println!("{}", hk_parser::serialize_hk(&config));
}
}
Err(e) => {
e.pretty_print(&contents);
std::process::exit(1);
}
}
Ok(())
}