islec/
main.rs

1use clap::Parser;
2use cranelift_isle::compile;
3use cranelift_isle::error::Errors;
4use std::{
5    fs,
6    io::{self, Write},
7    path::PathBuf,
8};
9
10#[derive(Parser)]
11struct Opts {
12    /// The output file to write the generated Rust code to. `stdout` is used if
13    /// this is not given.
14    #[arg(short, long)]
15    output: Option<PathBuf>,
16
17    /// The input ISLE DSL source files.
18    #[arg(required = true)]
19    inputs: Vec<PathBuf>,
20}
21
22fn main() -> Result<(), Errors> {
23    let _ = env_logger::try_init();
24
25    let opts = Opts::parse();
26    let code = compile::from_files(opts.inputs, &Default::default())?;
27
28    let stdout = io::stdout();
29    let (mut output, output_name): (Box<dyn Write>, _) = match &opts.output {
30        Some(f) => {
31            let output =
32                Box::new(fs::File::create(f).map_err(|e| {
33                    Errors::from_io(e, format!("failed to create '{}'", f.display()))
34                })?);
35            (output, f.display().to_string())
36        }
37        None => {
38            let output = Box::new(stdout.lock());
39            (output, "<stdout>".to_string())
40        }
41    };
42
43    output
44        .write_all(code.as_bytes())
45        .map_err(|e| Errors::from_io(e, format!("failed to write to '{}'", output_name)))?;
46
47    Ok(())
48}