-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathnode_types_yaml.rs
More file actions
51 lines (45 loc) · 1.24 KB
/
node_types_yaml.rs
File metadata and controls
51 lines (45 loc) · 1.24 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
use clap::Parser;
use std::io::Read;
#[derive(Parser)]
#[clap(
name = "node-types-yaml",
about = "Convert between YAML and JSON node-types formats"
)]
struct Cli {
/// Input file (reads from stdin if not provided)
input: Option<String>,
/// Convert from JSON to YAML (default is YAML to JSON)
#[arg(long)]
from_json: bool,
}
fn main() {
let args = Cli::parse();
let input = match &args.input {
Some(path) => std::fs::read_to_string(path).unwrap_or_else(|e| {
eprintln!("Error reading {path}: {e}");
std::process::exit(1);
}),
None => {
let mut buf = String::new();
std::io::stdin()
.read_to_string(&mut buf)
.unwrap_or_else(|e| {
eprintln!("Error reading stdin: {e}");
std::process::exit(1);
});
buf
}
};
let result = if args.from_json {
yeast::node_types_yaml::convert_from_json(&input)
} else {
yeast::node_types_yaml::convert(&input)
};
match result {
Ok(output) => print!("{output}"),
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
}