convert/
convert.rs

1use std::env;
2use std::fs;
3use std::io::{self, Read};
4use std::process;
5use std::str::FromStr;
6
7fn main() {
8    if let Err(error) = run() {
9        eprintln!("error: {error}");
10        process::exit(1);
11    }
12}
13
14fn run() -> Result<(), Box<dyn std::error::Error>> {
15    let mut args = env::args().skip(1);
16    let target_raw = args.next().ok_or_else(|| {
17        "usage: cargo run --example convert <target-version> [path|-]".to_string()
18    })?;
19    let target = ustx::Version::from_str(&target_raw)?;
20
21    let input = if let Some(path) = args.next() {
22        if path == "-" {
23            read_stdin()?
24        } else {
25            fs::read_to_string(path)?
26        }
27    } else {
28        read_stdin()?
29    };
30
31    let mut project = ustx::Project::from_yaml_str(&input)?;
32    project.convert_to(target)?;
33    let output = project.to_yaml_string()?;
34    print!("{output}");
35    Ok(())
36}
37
38fn read_stdin() -> io::Result<String> {
39    let mut buffer = String::new();
40    io::stdin().read_to_string(&mut buffer)?;
41    Ok(buffer)
42}