From e1128fe55d91ca60086de45c911b4568d2eec9ee Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Wed, 31 Mar 2021 13:43:46 -0600 Subject: awolnation voice BAIL --- src/makefile/command_line.rs | 3 +-- src/makefile/conditional.rs | 14 ++++++++++---- src/makefile/functions.rs | 24 ++++++++++++------------ src/makefile/mod.rs | 12 +++++++----- 4 files changed, 30 insertions(+), 23 deletions(-) (limited to 'src/makefile') diff --git a/src/makefile/command_line.rs b/src/makefile/command_line.rs index 050488c..aaf964a 100644 --- a/src/makefile/command_line.rs +++ b/src/makefile/command_line.rs @@ -94,8 +94,7 @@ impl CommandLine { if errored { // apparently there was an error. do we care? if !ignore_error { - // TODO handle this error gracefully - panic!("error from command execution!"); + anyhow::bail!("error from command execution!"); } } diff --git a/src/makefile/conditional.rs b/src/makefile/conditional.rs index 6795051..0b4de01 100644 --- a/src/makefile/conditional.rs +++ b/src/makefile/conditional.rs @@ -1,3 +1,5 @@ +use anyhow::bail; + use super::token::TokenString; pub enum Line { @@ -47,12 +49,13 @@ pub enum StateAction { } impl StateAction { + #[allow(clippy::panic)] pub fn apply_to(self, stack: &mut Vec) { match self { Self::Push(state) => stack.push(state), Self::Replace(state) => match stack.last_mut() { Some(x) => *x = state, - None => panic!("applying Replace on an empty condition stack"), + None => panic!("internal error: applying Replace on an empty condition stack"), }, Self::Pop => { stack.pop(); @@ -154,7 +157,7 @@ impl Line { State::SkippingUntilEndIf } Some(State::SkippingUntilElseOrEndIf) => State::Executing, - None => panic!("got an Else but not in a conditional"), + None => bail!("got an Else but not in a conditional"), }), Self::ElseIf(inner_condition) => match current_state { Some(State::Executing) | Some(State::SkippingUntilEndIf) => { @@ -163,9 +166,12 @@ impl Line { Some(State::SkippingUntilElseOrEndIf) => { inner_condition.action(current_state, is_macro_defined, expand_macro)? } - None => panic!("got an ElseIf but not in a conditional"), + None => bail!("got an ElseIf but not in a conditional"), + }, + Self::EndIf => match current_state { + Some(_) => StateAction::Pop, + None => bail!("got an EndIf but not in a conditional"), }, - Self::EndIf => StateAction::Pop, }) } } diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs index 005f906..97f05b6 100644 --- a/src/makefile/functions.rs +++ b/src/makefile/functions.rs @@ -53,7 +53,7 @@ pub fn expand_call(name: &str, args: &[TokenString], macros: &MacroSet) -> anyho "shell" => todo!(), // fallback - _ => panic!("function not implemented: {}", name), + _ => anyhow::bail!("function not implemented: {}", name), } } @@ -310,23 +310,23 @@ mod test { use std::fs::write; use std::path::MAIN_SEPARATOR; - let tempdir = tempfile::tempdir().unwrap(); + let tempdir = tempfile::tempdir()?; - write(tempdir.path().join("foo.c"), "").unwrap(); - write(tempdir.path().join("bar.h"), "").unwrap(); - write(tempdir.path().join("baz.txt"), "").unwrap(); - write(tempdir.path().join("acab.c"), "ACAB").unwrap(); - write(tempdir.path().join("based.txt"), "☭").unwrap(); + write(tempdir.path().join("foo.c"), "")?; + write(tempdir.path().join("bar.h"), "")?; + write(tempdir.path().join("baz.txt"), "")?; + write(tempdir.path().join("acab.c"), "ACAB")?; + write(tempdir.path().join("based.txt"), "☭")?; - set_current_dir(tempdir.path()).unwrap(); - set_var("HOME", tempdir.path().to_str().unwrap()); + set_current_dir(tempdir.path())?; + set_var("HOME", tempdir.path()); let sort = |x: String| call("sort", &[TokenString::text(&x)], &MacroSet::new()); assert_eq!(sort(call!(wildcard "*.c"))?, "acab.c foo.c"); assert_eq!( sort(call!(wildcard "~/ba?.*"))?, format!( "{0}{1}bar.h {0}{1}baz.txt", - tempdir.path().to_str().unwrap(), + tempdir.path().display(), MAIN_SEPARATOR ) ); @@ -339,7 +339,7 @@ mod test { macros.set( "test".to_owned(), MacroSource::File, - "worked for $(item).".parse().unwrap(), + "worked for $(item).".parse()?, ); assert_eq!( call( @@ -362,7 +362,7 @@ mod test { macros.set( "reverse".to_owned(), MacroSource::File, - "$(2) $(1)".parse().unwrap(), + "$(2) $(1)".parse()?, ); assert_eq!( call( diff --git a/src/makefile/mod.rs b/src/makefile/mod.rs index 94a104a..1fb5935 100644 --- a/src/makefile/mod.rs +++ b/src/makefile/mod.rs @@ -7,11 +7,12 @@ use std::io::{BufRead, BufReader}; use std::path::Path; use std::rc::Rc; -use crate::args::Args; -use anyhow::Context; +use anyhow::{bail, Context}; use lazy_static::lazy_static; use regex::Regex; +use crate::args::Args; + mod command_line; mod conditional; mod functions; @@ -217,9 +218,10 @@ impl<'a> Makefile<'a> { LineType::Rule => self.read_rule(&line_tokens, line_number, &mut lines_iter)?, LineType::Macro => self.read_macro(&line_tokens, line_number)?, LineType::Unknown => { - panic!( + bail!( "error: line {}: unknown line {:?}", - line_number, line_tokens + line_number, + line_tokens ); } } @@ -511,7 +513,7 @@ impl<'a> Makefile<'a> { } let targets = self.targets.borrow(); - Ok(targets.get(name).context("Target not found!")?.clone()) + Ok(Rc::clone(targets.get(name).context("Target not found!")?)) } pub fn update_target(&self, name: &str) -> anyhow::Result<()> { -- cgit v1.2.3