diff options
-rw-r--r-- | src/makefile/functions.rs | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs index 712b944..7bb0908 100644 --- a/src/makefile/functions.rs +++ b/src/makefile/functions.rs @@ -3,7 +3,7 @@ use std::env; use std::process::{Command, Stdio}; use std::rc::Rc; -use eyre::{bail, Result}; +use eyre::{bail, Result, WrapErr}; use super::pattern::r#match; use super::r#macro::{Set as MacroSet, Source as MacroSource}; @@ -40,6 +40,10 @@ pub fn expand_call( assert_eq!(args.len(), 1); text::sort(macros, &args[0]) } + "word" => { + assert_eq!(args.len(), 2); + text::word(macros, &args[0], &args[1]) + } "words" => { assert_eq!(args.len(), 1); text::words(macros, &args[0]) @@ -211,6 +215,13 @@ mod text { Ok(words.join(" ")) } + pub fn word(macros: &MacroSet, n: &TokenString, text: &TokenString) -> Result<String> { + let n = macros.expand(n)?; + let n: usize = n.parse().wrap_err("while calling `word`")?; + let text = macros.expand(text)?; + Ok(text.split_whitespace().nth(n + 1).unwrap_or("").to_owned()) + } + pub fn words(macros: &MacroSet, words: &TokenString) -> Result<String> { let words = macros.expand(words)?; Ok(words.split_whitespace().count().to_string()) |