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 | 9970b48411b9816fbfa5b6b100a91baed0d44849 (patch) | |
tree | ce6826c246a9441c4d9be5d1658bdfbc8e76e8fd /src/macro_processor | |
parent | 16f0edd24e5b436e0e5894379248a31a6ec1c029 (diff) | |
download | skaldpress-9970b48411b9816fbfa5b6b100a91baed0d44849.tar.gz skaldpress-9970b48411b9816fbfa5b6b100a91baed0d44849.zip |
Add array_size
Diffstat (limited to 'src/macro_processor')
-rw-r--r-- | src/macro_processor/macro_processor.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/macro_processor/macro_processor.rs b/src/macro_processor/macro_processor.rs index ba28b15..612d2c5 100644 --- a/src/macro_processor/macro_processor.rs +++ b/src/macro_processor/macro_processor.rs @@ -412,6 +412,32 @@ fn smp_builtin_array_each( Ok(out) } +fn smp_builtin_array_size( + smp: &mut MacroProcessor, + macro_name: &str, + args: &mut [String], +) -> Result<String, SMPError> { + let Some(macro_body) = smp.macros.get(&args[0]) else { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("{:?} is not a macro", args[0]), + )); + return Ok(String::new()); + }; + let MacroType::Array(array) = macro_body else { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("{:?} is not a macro of type array", args[0]), + )); + return Ok(String::new()); + }; + Ok(array.len().to_string()) +} + #[cfg(feature = "time")] fn smp_builtin_format_time( smp: &mut MacroProcessor, @@ -632,6 +658,10 @@ impl MacroProcessor { String::from("array_each"), MacroType::Function(smp_builtin_array_each), ); + self.define_macro( + String::from("array_size"), + MacroType::Function(smp_builtin_array_size), + ); #[cfg(feature = "time")] self.define_macro( String::from("format_time"), |