core: switch to anyhow

This commit adds `anyhow` as a dependency and switches over to it from
Box<dyn Error>.

It actually looks like I've kept all of my errors rather shallow, such
that we don't get a huge benefit from anyhow at present. But now that
anyhow is in use, I expect to use its "context" feature more going
forward.
This commit is contained in:
Andrew Gallant
2023-10-12 12:16:42 -04:00
parent 53679e4c43
commit 92c81b1225
6 changed files with 105 additions and 104 deletions

View File

@@ -53,11 +53,11 @@ pub fn args() -> Vec<OsString> {
/// for each line in addition to successfully parsed arguments.
fn parse<P: AsRef<Path>>(
path: P,
) -> crate::Result<(Vec<OsString>, Vec<Box<dyn std::error::Error>>)> {
) -> anyhow::Result<(Vec<OsString>, Vec<anyhow::Error>)> {
let path = path.as_ref();
match std::fs::File::open(&path) {
Ok(file) => parse_reader(file),
Err(err) => Err(From::from(format!("{}: {}", path.display(), err))),
Err(err) => anyhow::bail!("{}: {}", path.display(), err),
}
}
@@ -74,7 +74,7 @@ fn parse<P: AsRef<Path>>(
/// in addition to successfully parsed arguments.
fn parse_reader<R: std::io::Read>(
rdr: R,
) -> crate::Result<(Vec<OsString>, Vec<Box<dyn std::error::Error>>)> {
) -> anyhow::Result<(Vec<OsString>, Vec<anyhow::Error>)> {
let mut bufrdr = std::io::BufReader::new(rdr);
let (mut args, mut errs) = (vec![], vec![]);
let mut line_number = 0;
@@ -90,7 +90,7 @@ fn parse_reader<R: std::io::Read>(
args.push(osstr.to_os_string());
}
Err(err) => {
errs.push(format!("{}: {}", line_number, err).into());
errs.push(anyhow::anyhow!("{line_number}: {err}"));
}
}
Ok(true)