diff options
author | Qrius <[email protected]> | 2024-09-26 00:11:05 +0200 |
---|---|---|
committer | Qrius <[email protected]> | 2024-09-26 00:11:05 +0200 |
commit | 509744ad69135447ba0f445b3f68207722ffab62 (patch) | |
tree | 447994ea1bb5e425eba5ebecad407527109ba18f /src | |
parent | 5cb56cf81dafba1011ccff8c24b6e4b150406f17 (diff) | |
download | skaldpress-509744ad69135447ba0f445b3f68207722ffab62.tar.gz skaldpress-509744ad69135447ba0f445b3f68207722ffab62.zip |
Add a macro for array_push
Diffstat (limited to 'src')
-rw-r--r-- | src/macro_processor/macro_processor.rs | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/src/macro_processor/macro_processor.rs b/src/macro_processor/macro_processor.rs index 866fbdd..ad230c0 100644 --- a/src/macro_processor/macro_processor.rs +++ b/src/macro_processor/macro_processor.rs @@ -286,6 +286,25 @@ fn smp_builtin_indent( lin.push_str(&smp.process_input(&l)?); out.push_str(&lin); } + Ok(out) +} + +/// Push any arguments to array macro +fn smp_builtin_array_push( + smp: &mut MacroProcessor, + macro_name: &str, + args: &mut [String], +) -> Result<String, SMPError> { + for arg in args.iter() { + if let Err(e) = smp.array_push(macro_name, MacroType::String(arg.to_string())) { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("Error executing array_push ({:?})", e), + )); + } + } Ok(String::new()) } @@ -428,6 +447,10 @@ impl MacroProcessor { MacroType::Function(smp_builtin_indent), ); self.define_macro(String::from("expr"), MacroType::Function(smp_builtin_expr)); + self.define_macro( + String::from("array_push"), + MacroType::Function(smp_builtin_array_push), + ); #[cfg(feature = "time")] self.define_macro( String::from("format_time"), @@ -456,8 +479,9 @@ impl MacroProcessor { self.macros.insert(name, macro_expansion); } - pub fn array_push(&mut self, name: String, element: MacroType) -> Result<(), SMPError> { - let Some(macro_body) = self.macros.get_mut(&name) else { + /// Push a MacroType into a array, this allows creating special macros that can iterate + pub fn array_push(&mut self, name: &str, element: MacroType) -> Result<(), SMPError> { + let Some(macro_body) = self.macros.get_mut(name) else { return Err(SMPError::UnknownError(4, None)); }; let MacroType::Array(array) = macro_body else { |