Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/cranelift/isle/islec/src/main.rs
1692 views
1
use clap::Parser;
2
use cranelift_isle::compile;
3
use cranelift_isle::error::Errors;
4
use std::{
5
fs,
6
io::{self, Write},
7
path::PathBuf,
8
};
9
10
#[derive(Parser)]
11
struct 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
22
fn 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
}
49
50