feat: added remaining primitive templates meta/number and meta/bool

This commit is contained in:
selene 2025-11-26 01:05:34 +01:00
parent 0f968888bf
commit 32dae06d7e
Signed by: sel
SSH key fingerprint: SHA256:33R/4Rx5Lu4o81LyJyXdMrmP5CJ6j7j1Soo0Dn7mKc0
4 changed files with 79 additions and 6 deletions

View file

@ -6,9 +6,9 @@ fn main() -> Result<(), String> {
let modules = item_registry();
// change these!
let object_type = "meta/text";
let object_type = "meta/number";
let input = json!({
"value": "owo"
"value": 1312
});
let function = "local";
@ -24,8 +24,7 @@ fn main() -> Result<(), String> {
Some(ModuleItem::Calculator(calc)) => calc(&some_object)?,
Some(_) => return Err(format!("if you're trying to run a ModuleItem::Function and not a ModuleItem::Calculator, you'll have to add the match arm for that")),
None => {
println!("the ModuleItem `{}:func:{}` doesn't exist. this is not necessarily bad in this example (if you changed `object_type`)", object_type, function);
some_object.clone() // does nothing(?) i hope
return Err(format!("the ModuleItem `{}:func:{}` doesn't exist. this is not necessarily bad in this example (if you changed `object_type`)", object_type, function));
}
};

View file

@ -1,14 +1,27 @@
use crate::modules::ModuleItem;
use std::collections::HashMap;
pub mod dummy;
pub mod text;
mod dummy;
mod text;
mod number;
mod bool;
pub fn registry() -> HashMap<&'static str, ModuleItem> {
let mut map = HashMap::new();
map.insert("meta/dummy", ModuleItem::Template(dummy::TEMPLATE));
// meta/text
map.insert("meta/text", ModuleItem::Template(text::TEMPLATE));
map.insert("meta/text:func:validator", ModuleItem::Validator(text::validate));
map.insert("meta/text:func:local", ModuleItem::Calculator(text::local));
// meta/number
map.insert("meta/number", ModuleItem::Template(number::TEMPLATE));
map.insert("meta/number:func:validator", ModuleItem::Validator(number::validate));
map.insert("meta/number:func:local", ModuleItem::Calculator(number::local));
// meta/bool
map.insert("meta/bool", ModuleItem::Template(bool::TEMPLATE));
map.insert("meta/bool:func:validator", ModuleItem::Validator(bool::validate));
map
}

View file

@ -0,0 +1,19 @@
use crate::ObjectInstance;
pub const TEMPLATE: &str = r#"
[input.value]
transforms = []
subobjects = []
conditions = []
duplicates = false
"#;
pub fn validate(obj: &ObjectInstance) -> Result<(), String> {
let value = obj.input.get("value")
.ok_or("input.value must exist")?;
let _value = value.as_bool()
.ok_or("input.value must be a boolean")?;
Ok(())
}

View file

@ -0,0 +1,42 @@
use serde_json::json;
use crate::ObjectInstance;
pub const TEMPLATE: &str = r#"
[input.value]
transforms = []
subobjects = []
conditions = []
duplicates = false
[local.length]
transforms = ["meta/number:value"]
subobjects = []
conditions = []
duplicates = false
"#;
pub fn validate(obj: &ObjectInstance) -> Result<(), String> {
let value = obj.input.get("value")
.ok_or("input.value must exist")?;
let _value = value.as_number()
.ok_or("input.value must be a number")?;
Ok(())
}
pub fn local(obj: &ObjectInstance) -> Result<ObjectInstance, String> {
let _value = obj.input.get("value")
.ok_or("input.value must exist")?;
let value = _value.as_number()
.ok_or("input.value must be a number")?;
let mut new = obj.clone();
new.local = json!({
"length": value.to_string().len()
});
Ok(new)
}