diff options
author | Melody Horn <melody@boringcactus.com> | 2021-03-31 12:51:11 -0600 |
---|---|---|
committer | Melody Horn <melody@boringcactus.com> | 2021-03-31 12:51:11 -0600 |
commit | e1a0584936b3aa5ce971e875dec750d2ae937d2e (patch) | |
tree | f1db896b9623e6480181df3ad4792107d9d87ecf /src/makefile/functions.rs | |
parent | d10ad4b37f726180e3da562a1fbb6cbbd106ef58 (diff) | |
download | makers-e1a0584936b3aa5ce971e875dec750d2ae937d2e.tar.gz makers-e1a0584936b3aa5ce971e875dec750d2ae937d2e.zip |
massively upgrade error handling
Diffstat (limited to 'src/makefile/functions.rs')
-rw-r--r-- | src/makefile/functions.rs | 84 |
1 files changed, 48 insertions, 36 deletions
diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs index 61b9828..8087a94 100644 --- a/src/makefile/functions.rs +++ b/src/makefile/functions.rs @@ -2,7 +2,11 @@ use super::pattern::r#match; use super::r#macro::{MacroSet, MacroSource}; use super::token::TokenString; -pub(crate) fn expand_call(name: &str, args: &[TokenString], macros: &MacroSet) -> String { +pub(crate) fn expand_call( + name: &str, + args: &[TokenString], + macros: &MacroSet, +) -> anyhow::Result<String> { match name { "filter" => { assert_eq!(args.len(), 2); @@ -63,50 +67,54 @@ mod text { use super::MacroSet; use super::TokenString; - pub(crate) fn filter(macros: &MacroSet, patterns: &TokenString, text: &TokenString) -> String { - let patterns = macros.expand(patterns); + pub(crate) fn filter( + macros: &MacroSet, + patterns: &TokenString, + text: &TokenString, + ) -> anyhow::Result<String> { + let patterns = macros.expand(patterns)?; let patterns = patterns.split_whitespace().collect::<Vec<_>>(); - let text = macros.expand(text); + let text = macros.expand(text)?; let text = text.split_whitespace(); let mut result_pieces = vec![]; for word in text { if patterns .iter() - .any(|pattern| r#match(pattern, word).is_some()) + .any(|pattern| r#match(pattern, word).map_or(false, |x| x.is_some())) { result_pieces.push(word); } } - result_pieces.join(" ") + Ok(result_pieces.join(" ")) } pub(crate) fn filter_out( macros: &MacroSet, patterns: &TokenString, text: &TokenString, - ) -> String { - let patterns = macros.expand(patterns); + ) -> anyhow::Result<String> { + let patterns = macros.expand(patterns)?; let patterns = patterns.split_whitespace().collect::<Vec<_>>(); - let text = macros.expand(text); + let text = macros.expand(text)?; let text = text.split_whitespace(); let mut result_pieces = vec![]; for word in text { if patterns .iter() - .all(|pattern| r#match(pattern, word).is_none()) + .all(|pattern| r#match(pattern, word).map_or(false, |x| x.is_none())) { result_pieces.push(word); } } - result_pieces.join(" ") + Ok(result_pieces.join(" ")) } - pub(crate) fn sort(macros: &MacroSet, words: &TokenString) -> String { - let words = macros.expand(words); + pub(crate) fn sort(macros: &MacroSet, words: &TokenString) -> anyhow::Result<String> { + let words = macros.expand(words)?; let mut words = words.split_whitespace().collect::<Vec<_>>(); words.sort_unstable(); words.dedup(); - words.join(" ") + Ok(words.join(" ")) } } @@ -116,11 +124,13 @@ mod file_name { use std::ffi::OsStr; use std::path::Path; + use anyhow::Context; + use super::MacroSet; use super::TokenString; - pub(crate) fn notdir(macros: &MacroSet, words: &TokenString) -> String { - let words = macros.expand(words); + pub(crate) fn notdir(macros: &MacroSet, words: &TokenString) -> anyhow::Result<String> { + let words = macros.expand(words)?; let words = words .split_whitespace() .map(|word| { @@ -130,11 +140,11 @@ mod file_name { .unwrap_or("") }) .collect::<Vec<_>>(); - words.join(" ") + Ok(words.join(" ")) } - pub(crate) fn basename(macros: &MacroSet, words: &TokenString) -> String { - let words = macros.expand(words); + pub(crate) fn basename(macros: &MacroSet, words: &TokenString) -> anyhow::Result<String> { + let words = macros.expand(words)?; let words = words .split_whitespace() .map(|word| { @@ -144,25 +154,25 @@ mod file_name { .map_or_else(String::new, ToString::to_string) }) .collect::<Vec<_>>(); - words.join(" ") + Ok(words.join(" ")) } pub(crate) fn addprefix( macros: &MacroSet, prefix: &TokenString, targets: &TokenString, - ) -> String { - let prefix = macros.expand(prefix); - let targets = macros.expand(targets); + ) -> anyhow::Result<String> { + let prefix = macros.expand(prefix)?; + let targets = macros.expand(targets)?; let results = targets .split_whitespace() .map(|t| format!("{}{}", prefix, t)) .collect::<Vec<_>>(); - results.join(" ") + Ok(results.join(" ")) } - pub(crate) fn wildcard(macros: &MacroSet, pattern: &TokenString) -> String { - let pattern = macros.expand(pattern); + pub(crate) fn wildcard(macros: &MacroSet, pattern: &TokenString) -> anyhow::Result<String> { + let pattern = macros.expand(pattern)?; let home_dir = env::var("HOME") .ok() .or_else(|| dirs::home_dir().and_then(|p| p.to_str().map(String::from))); @@ -172,13 +182,13 @@ mod file_name { pattern }; let results = glob::glob(&pattern) - .expect("invalid glob pattern!") + .context("invalid glob pattern!")? .filter_map(|path| { path.ok() .map(|x| x.to_str().map(ToString::to_string).unwrap_or_default()) }) .collect::<Vec<_>>(); - results.join(" ") + Ok(results.join(" ")) } } @@ -193,9 +203,9 @@ mod foreach { var: &TokenString, list: &TokenString, text: &TokenString, - ) -> String { - let var = macros.expand(var); - let list = macros.expand(list); + ) -> anyhow::Result<String> { + let var = macros.expand(var)?; + let list = macros.expand(list)?; let words = list.split_whitespace(); let mut macros = macros.with_overlay(); @@ -204,8 +214,8 @@ mod foreach { macros.set(var.clone(), MacroSource::File, TokenString::text(word)); macros.expand(text) }) - .collect::<Vec<_>>(); - results.join(" ") + .collect::<Result<Vec<_>, _>>()?; + Ok(results.join(" ")) } } @@ -218,8 +228,10 @@ mod call { pub(crate) fn call<'a>( macros: &MacroSet, args: impl Iterator<Item = &'a TokenString>, - ) -> String { - let args = args.map(|arg| macros.expand(arg)).collect::<Vec<_>>(); + ) -> anyhow::Result<String> { + let args = args + .map(|arg| macros.expand(arg)) + .collect::<Result<Vec<_>, _>>()?; let function = args[0].clone(); let mut macros = macros.with_overlay(); @@ -236,7 +248,7 @@ mod test { use crate::makefile::r#macro::{MacroSet, MacroSource}; - fn call(name: &str, args: &[TokenString], macros: &MacroSet) -> String { + fn call(name: &str, args: &[TokenString], macros: &MacroSet) -> anyhow::Result<String> { super::expand_call(name, args, macros) } |