clif-util: Use anyhow::Error for errors instead of String

Also does the same for `cranelift-filetests`.
This commit is contained in:
Nick Fitzgerald
2020-09-14 16:23:30 -07:00
parent 9fea412333
commit 31cbbd1d20
43 changed files with 415 additions and 443 deletions

View File

@@ -1,20 +1,19 @@
use anyhow::{Context, Result};
use std::io::{Read, Write};
use std::path::Path;
pub fn run(input: &Path, output: &Path) -> Result<(), String> {
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)
.map_err(|e| format!("failed to read from stdin: {}", e))?;
let souper_dsl =
String::from_utf8(souper_dsl).map_err(|e| format!("stdin is not UTF-8: {}", e))?;
peepmatic_souper::convert_str(&souper_dsl, Some(Path::new("stdin")))
.map_err(|e| e.to_string())?
.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).map_err(|e| e.to_string())?
peepmatic_souper::convert_file(input)?
};
if output == Path::new("-") {
@@ -22,10 +21,10 @@ pub fn run(input: &Path, output: &Path) -> Result<(), String> {
let mut stdout = stdout.lock();
stdout
.write_all(peepmatic_dsl.as_bytes())
.map_err(|e| format!("error writing to stdout: {}", e))?;
.context("error writing to stdout")?;
} else {
std::fs::write(output, peepmatic_dsl.as_bytes())
.map_err(|e| format!("error writing to {}: {}", output.display(), e))?;
.with_context(|| format!("error writing to {}", output.display()))?;
}
Ok(())