summaryrefslogtreecommitdiff
path: root/src/macro_processor/macro_processor.rs
diff options
context:
space:
mode:
authorQrius <[email protected]>2024-09-26 00:11:05 +0200
committerQrius <[email protected]>2024-09-26 00:11:05 +0200
commit9970b48411b9816fbfa5b6b100a91baed0d44849 (patch)
treece6826c246a9441c4d9be5d1658bdfbc8e76e8fd /src/macro_processor/macro_processor.rs
parent16f0edd24e5b436e0e5894379248a31a6ec1c029 (diff)
downloadskaldpress-9970b48411b9816fbfa5b6b100a91baed0d44849.tar.gz
skaldpress-9970b48411b9816fbfa5b6b100a91baed0d44849.zip
Add array_size
Diffstat (limited to 'src/macro_processor/macro_processor.rs')
-rw-r--r--src/macro_processor/macro_processor.rs30
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"),