Files
wasmtime/cranelift/src/souper_to_peepmatic.rs
Nick Fitzgerald 31cbbd1d20 clif-util: Use anyhow::Error for errors instead of String
Also does the same for `cranelift-filetests`.
2020-09-14 18:29:00 -07:00

32 lines
1.0 KiB
Rust

use anyhow::{Context, Result};
use std::io::{Read, Write};
use std::path::Path;
pub fn run(input: &Path, output: &Path) -> Result<()> {
let peepmatic_dsl = if input == Path::new("-") {
let stdin = std::io::stdin();
let mut stdin = stdin.lock();
let mut souper_dsl = vec![];
stdin
.read_to_end(&mut souper_dsl)
.context("failed to read from stdin")?;
let souper_dsl = String::from_utf8(souper_dsl).context("stdin is not UTF-8: {}")?;
peepmatic_souper::convert_str(&souper_dsl, Some(Path::new("stdin")))?
} else {
peepmatic_souper::convert_file(input)?
};
if output == Path::new("-") {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();
stdout
.write_all(peepmatic_dsl.as_bytes())
.context("error writing to stdout")?;
} else {
std::fs::write(output, peepmatic_dsl.as_bytes())
.with_context(|| format!("error writing to {}", output.display()))?;
}
Ok(())
}