From 093e58de2ffc8243e6ff929f50a2aaa6ef60848b Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Thu, 1 Apr 2021 17:44:50 -0600 Subject: slightly fancier errors --- src/makefile/command_line.rs | 6 ++++-- src/makefile/conditional.rs | 10 ++++----- src/makefile/functions.rs | 49 ++++++++++++++++++-------------------------- src/makefile/macro.rs | 12 +++++------ src/makefile/mod.rs | 36 +++++++++++++++++--------------- src/makefile/pattern.rs | 7 ++++--- src/makefile/target.rs | 6 ++++-- src/makefile/token.rs | 8 ++++---- 8 files changed, 67 insertions(+), 67 deletions(-) (limited to 'src/makefile') diff --git a/src/makefile/command_line.rs b/src/makefile/command_line.rs index aaf964a..8756bc9 100644 --- a/src/makefile/command_line.rs +++ b/src/makefile/command_line.rs @@ -3,6 +3,8 @@ use std::fmt; use std::io; use std::process::{Command, ExitStatus}; +use eyre::bail; + use crate::makefile::target::Target; use crate::makefile::token::{Token, TokenString}; use crate::makefile::Makefile; @@ -69,7 +71,7 @@ impl CommandLine { } } - pub fn execute(&self, file: &Makefile, target: &Target) -> anyhow::Result<()> { + pub fn execute(&self, file: &Makefile, target: &Target) -> eyre::Result<()> { let ignore_error = self.ignore_errors || file.args.ignore_errors || file.special_target_has_prereq(".IGNORE", &target.name); @@ -94,7 +96,7 @@ impl CommandLine { if errored { // apparently there was an error. do we care? if !ignore_error { - anyhow::bail!("error from command execution!"); + bail!("error from command execution!"); } } diff --git a/src/makefile/conditional.rs b/src/makefile/conditional.rs index 0b4de01..149638e 100644 --- a/src/makefile/conditional.rs +++ b/src/makefile/conditional.rs @@ -1,4 +1,4 @@ -use anyhow::bail; +use eyre::{bail, Result}; use super::token::TokenString; @@ -83,8 +83,8 @@ fn decode_condition_args(line_body: &str) -> Option<(TokenString, TokenString)> impl Line { pub fn from( line: &str, - expand_macro: impl Fn(&TokenString) -> anyhow::Result, - ) -> anyhow::Result> { + expand_macro: impl Fn(&TokenString) -> Result, + ) -> Result> { Ok(Some(if let Some(line) = line.strip_prefix("ifeq ") { match decode_condition_args(line) { Some((arg1, arg2)) => Self::IfEqual(arg1, arg2), @@ -117,8 +117,8 @@ impl Line { &self, current_state: Option<&State>, is_macro_defined: impl Fn(&str) -> bool, - expand_macro: impl Fn(&TokenString) -> anyhow::Result, - ) -> anyhow::Result { + expand_macro: impl Fn(&TokenString) -> Result, + ) -> Result { Ok(match self { Self::IfEqual(arg1, arg2) => { let arg1 = expand_macro(arg1)?; diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs index 97f05b6..a4d206f 100644 --- a/src/makefile/functions.rs +++ b/src/makefile/functions.rs @@ -1,8 +1,10 @@ +use eyre::{bail, Result}; + use super::pattern::r#match; use super::r#macro::{Set as MacroSet, Source as MacroSource}; use super::token::TokenString; -pub fn expand_call(name: &str, args: &[TokenString], macros: &MacroSet) -> anyhow::Result { +pub fn expand_call(name: &str, args: &[TokenString], macros: &MacroSet) -> Result { match name { "filter" => { assert_eq!(args.len(), 2); @@ -53,21 +55,15 @@ pub fn expand_call(name: &str, args: &[TokenString], macros: &MacroSet) -> anyho "shell" => todo!(), // fallback - _ => anyhow::bail!("function not implemented: {}", name), + _ => bail!("function not implemented: {}", name), } } // Text Functions mod text { - use super::r#match; - use super::MacroSet; - use super::TokenString; + use super::*; - pub fn filter( - macros: &MacroSet, - patterns: &TokenString, - text: &TokenString, - ) -> anyhow::Result { + pub fn filter(macros: &MacroSet, patterns: &TokenString, text: &TokenString) -> Result { let patterns = macros.expand(patterns)?; let patterns = patterns.split_whitespace().collect::>(); let text = macros.expand(text)?; @@ -88,7 +84,7 @@ mod text { macros: &MacroSet, patterns: &TokenString, text: &TokenString, - ) -> anyhow::Result { + ) -> Result { let patterns = macros.expand(patterns)?; let patterns = patterns.split_whitespace().collect::>(); let text = macros.expand(text)?; @@ -105,7 +101,7 @@ mod text { Ok(result_pieces.join(" ")) } - pub fn sort(macros: &MacroSet, words: &TokenString) -> anyhow::Result { + pub fn sort(macros: &MacroSet, words: &TokenString) -> Result { let words = macros.expand(words)?; let mut words = words.split_whitespace().collect::>(); words.sort_unstable(); @@ -120,12 +116,11 @@ mod file_name { use std::ffi::OsStr; use std::path::Path; - use anyhow::Context; + use eyre::WrapErr; - use super::MacroSet; - use super::TokenString; + use super::*; - pub fn notdir(macros: &MacroSet, words: &TokenString) -> anyhow::Result { + pub fn notdir(macros: &MacroSet, words: &TokenString) -> Result { let words = macros.expand(words)?; let words = words .split_whitespace() @@ -139,7 +134,7 @@ mod file_name { Ok(words.join(" ")) } - pub fn basename(macros: &MacroSet, words: &TokenString) -> anyhow::Result { + pub fn basename(macros: &MacroSet, words: &TokenString) -> Result { let words = macros.expand(words)?; let words = words .split_whitespace() @@ -157,7 +152,7 @@ mod file_name { macros: &MacroSet, prefix: &TokenString, targets: &TokenString, - ) -> anyhow::Result { + ) -> Result { let prefix = macros.expand(prefix)?; let targets = macros.expand(targets)?; let results = targets @@ -167,7 +162,7 @@ mod file_name { Ok(results.join(" ")) } - pub fn wildcard(macros: &MacroSet, pattern: &TokenString) -> anyhow::Result { + pub fn wildcard(macros: &MacroSet, pattern: &TokenString) -> Result { let pattern = macros.expand(pattern)?; let home_dir = env::var("HOME") .ok() @@ -190,16 +185,14 @@ mod file_name { // foreach mod foreach { - use super::MacroSet; - use super::MacroSource; - use super::TokenString; + use super::*; pub fn foreach( macros: &MacroSet, var: &TokenString, list: &TokenString, text: &TokenString, - ) -> anyhow::Result { + ) -> Result { let var = macros.expand(var)?; let list = macros.expand(list)?; let words = list.split_whitespace(); @@ -217,14 +210,12 @@ mod foreach { // call mod call { - use super::MacroSet; - use super::MacroSource; - use super::TokenString; + use super::*; pub fn call<'a>( macros: &MacroSet, args: impl Iterator, - ) -> anyhow::Result { + ) -> Result { let args = args .map(|arg| macros.expand(arg)) .collect::, _>>()?; @@ -244,9 +235,9 @@ mod test { use crate::makefile::r#macro::{Set as MacroSet, Source as MacroSource}; - type R = anyhow::Result<()>; + type R = Result<()>; - fn call(name: &str, args: &[TokenString], macros: &MacroSet) -> anyhow::Result { + fn call(name: &str, args: &[TokenString], macros: &MacroSet) -> Result { super::expand_call(name, args, macros) } diff --git a/src/makefile/macro.rs b/src/makefile/macro.rs index f18060f..e760282 100644 --- a/src/makefile/macro.rs +++ b/src/makefile/macro.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::env; use std::fmt; -use anyhow::Context; +use eyre::{bail, Result, WrapErr}; use regex::Regex; use super::functions; @@ -16,9 +16,9 @@ pub enum Source { Builtin, } -pub trait LookupInternal: for<'a> Fn(&'a str) -> anyhow::Result {} +pub trait LookupInternal: for<'a> Fn(&'a str) -> Result {} -impl Fn(&'a str) -> anyhow::Result> LookupInternal for F {} +impl Fn(&'a str) -> Result> LookupInternal for F {} #[derive(Clone)] pub struct Set<'parent, 'lookup> { @@ -51,13 +51,13 @@ impl<'parent, 'lookup> Set<'parent, 'lookup> { } } - fn lookup_internal(&self, name: &str) -> anyhow::Result { + fn lookup_internal(&self, name: &str) -> Result { if let Some(lookup) = self.lookup_internal { lookup(name) } else if let Some(parent) = self.parent { parent.lookup_internal(name) } else { - anyhow::bail!("no lookup possible") + bail!("no lookup possible") } } @@ -80,7 +80,7 @@ impl<'parent, 'lookup> Set<'parent, 'lookup> { self.data.remove(name) } - pub fn expand(&self, text: &TokenString) -> anyhow::Result { + pub fn expand(&self, text: &TokenString) -> Result { let mut result = String::new(); for token in text.tokens() { match token { diff --git a/src/makefile/mod.rs b/src/makefile/mod.rs index 1fb5935..af8ca81 100644 --- a/src/makefile/mod.rs +++ b/src/makefile/mod.rs @@ -7,7 +7,7 @@ use std::io::{BufRead, BufReader}; use std::path::Path; use std::rc::Rc; -use anyhow::{bail, Context}; +use eyre::{bail, eyre, Context, Result}; use lazy_static::lazy_static; use regex::Regex; @@ -133,7 +133,7 @@ impl<'a> Makefile<'a> { } } - pub fn and_read_file(&mut self, path: impl AsRef) -> anyhow::Result<()> { + pub fn and_read_file(&mut self, path: impl AsRef) -> Result<()> { let file = File::open(path); // TODO handle errors let file = file.context("couldn't open makefile!")?; @@ -141,7 +141,7 @@ impl<'a> Makefile<'a> { self.and_read(file_reader) } - pub fn and_read(&mut self, source: impl BufRead) -> anyhow::Result<()> { + pub fn and_read(&mut self, source: impl BufRead) -> Result<()> { let mut lines_iter = source .lines() .enumerate() @@ -235,12 +235,12 @@ impl<'a> Makefile<'a> { &mut self, line_tokens: &TokenString, line_number: usize, - lines_iter: &mut impl Iterator)>, - ) -> anyhow::Result<()> { + lines_iter: &mut impl Iterator)>, + ) -> Result<()> { let mut lines_iter = lines_iter.peekable(); let (targets, not_targets) = line_tokens .split_once(':') - .with_context(|| format!("read_rule couldn't find a ':' on line {}", line_number))?; + .ok_or_else(|| eyre!("read_rule couldn't find a ':' on line {}", line_number))?; let targets = self.expand_macros(&targets, None)?; let targets = targets.split_whitespace().collect::>(); let (prerequisites, mut commands) = match not_targets.split_once(';') { @@ -350,10 +350,10 @@ impl<'a> Makefile<'a> { Ok(()) } - fn read_macro(&mut self, line_tokens: &TokenString, line_number: usize) -> anyhow::Result<()> { + fn read_macro(&mut self, line_tokens: &TokenString, line_number: usize) -> Result<()> { let (name, mut value) = line_tokens .split_once('=') - .with_context(|| format!("read_rule couldn't find a ':' on line {}", line_number))?; + .ok_or_else(|| eyre!("read_rule couldn't find a ':' on line {}", line_number))?; let name = self.expand_macros(&name, None)?; // GNUisms are annoying, but popular let mut expand_value = false; @@ -418,7 +418,7 @@ impl<'a> Makefile<'a> { } } - pub fn get_target(&self, name: &str) -> anyhow::Result>> { + pub fn get_target(&self, name: &str) -> Result>> { // TODO implement .POSIX let follow_gnu = true; @@ -513,19 +513,23 @@ impl<'a> Makefile<'a> { } let targets = self.targets.borrow(); - Ok(Rc::clone(targets.get(name).context("Target not found!")?)) + Ok(Rc::clone( + targets + .get(name) + .ok_or_else(|| eyre!("Target not found!"))?, + )) } - pub fn update_target(&self, name: &str) -> anyhow::Result<()> { + pub fn update_target(&self, name: &str) -> Result<()> { self.get_target(name)?.borrow().update(self) } - fn expand_macros(&self, text: &TokenString, target: Option<&Target>) -> anyhow::Result { + fn expand_macros(&self, text: &TokenString, target: Option<&Target>) -> Result { let target = target.cloned(); let lookup_internal = move |name: &str| { let target = target .as_ref() - .context("internal macro but no current target!")?; + .ok_or_else(|| eyre!("internal macro but no current target!"))?; let macro_pieces = if name.starts_with('@') { // The $@ shall evaluate to the full target name of the // current target. @@ -564,7 +568,7 @@ impl<'a> Makefile<'a> { .map(|x| { Path::new(&x) .parent() - .context("no parent") + .ok_or_else(|| eyre!("no parent")) .map(|x| x.to_string_lossy().into()) }) .collect::>()? @@ -574,7 +578,7 @@ impl<'a> Makefile<'a> { .map(|x| { Path::new(&x) .file_name() - .context("no filename") + .ok_or_else(|| eyre!("no filename")) .map(|x| x.to_string_lossy().into()) }) .collect::>()? @@ -695,7 +699,7 @@ mod test { use super::*; - type R = anyhow::Result<()>; + type R = Result<()>; fn empty_makefile(args: &Args) -> Makefile { Makefile { diff --git a/src/makefile/pattern.rs b/src/makefile/pattern.rs index 6648851..b47c75d 100644 --- a/src/makefile/pattern.rs +++ b/src/makefile/pattern.rs @@ -1,6 +1,7 @@ +use eyre::Result; use regex::{Captures, Regex}; -fn compile_pattern(pattern: &str) -> anyhow::Result { +fn compile_pattern(pattern: &str) -> Result { let mut result = String::new(); for c in pattern.chars() { if c == '%' { @@ -24,7 +25,7 @@ fn compile_pattern(pattern: &str) -> anyhow::Result { Ok(Regex::new(&result)?) } -pub fn r#match<'a>(pattern: &str, text: &'a str) -> anyhow::Result>> { +pub fn r#match<'a>(pattern: &str, text: &'a str) -> Result>> { Ok(compile_pattern(pattern)?.captures(text)) } @@ -32,7 +33,7 @@ pub fn r#match<'a>(pattern: &str, text: &'a str) -> anyhow::Result; + type R = Result<()>; #[test] fn pattern_backslashes() -> R { diff --git a/src/makefile/target.rs b/src/makefile/target.rs index 64fdb7d..615c9e9 100644 --- a/src/makefile/target.rs +++ b/src/makefile/target.rs @@ -3,6 +3,8 @@ use std::fmt; use std::fs::metadata; use std::time::SystemTime; +use eyre::Result; + use crate::makefile::command_line::CommandLine; use super::Makefile; @@ -50,7 +52,7 @@ impl Target { exists && newer_than_all_dependencies } - pub fn update(&self, file: &Makefile) -> anyhow::Result<()> { + pub fn update(&self, file: &Makefile) -> Result<()> { for prereq in &self.prerequisites { file.update_target(prereq)?; } @@ -62,7 +64,7 @@ impl Target { Ok(()) } - fn execute_commands(&self, file: &Makefile) -> anyhow::Result<()> { + fn execute_commands(&self, file: &Makefile) -> Result<()> { for command in &self.commands { command.execute(file, self)?; } diff --git a/src/makefile/token.rs b/src/makefile/token.rs index 16403ff..076ac60 100644 --- a/src/makefile/token.rs +++ b/src/makefile/token.rs @@ -1,7 +1,7 @@ use std::fmt; use std::str::FromStr; -use anyhow::Context; +use eyre::WrapErr; use nom::{ branch::alt, bytes::complete::{tag, take_till1, take_while1}, @@ -252,16 +252,16 @@ fn full_text_tokens(input: &str) -> IResult<&str, TokenString> { all_consuming(tokens_but_not(vec![]))(input) } -pub fn tokenize(input: &str) -> anyhow::Result { +pub fn tokenize(input: &str) -> eyre::Result { let (_, result) = full_text_tokens(input) .finish() - .map_err(|err| anyhow::anyhow!(err.to_string())) + .map_err(|err| eyre::eyre!(err.to_string())) .with_context(|| format!("couldn't parse {:?}", input))?; Ok(result) } impl FromStr for TokenString { - type Err = anyhow::Error; + type Err = eyre::Error; fn from_str(s: &str) -> Result { tokenize(s) -- cgit v1.2.3