feat!: added mostly foundational code, and a playground of sorts to interact with it
This commit is contained in:
parent
6c524b0741
commit
9a28c1a1dd
10 changed files with 417 additions and 0 deletions
58
server/src/lib.rs
Normal file
58
server/src/lib.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
pub mod modules;
|
||||
pub mod types;
|
||||
use crate::modules::{ModuleItem, item_registry};
|
||||
use crate::types::{ObjectInstance, ObjectTemplate};
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
impl ObjectInstance {
|
||||
pub fn validate(obj: &ObjectInstance) -> Result<(), String> {
|
||||
let modules = item_registry();
|
||||
let object_type = &obj.object_type;
|
||||
|
||||
let template: ObjectTemplate = match modules.get(object_type.as_str()) {
|
||||
Some(ModuleItem::Template(template_str)) => toml::from_str(template_str).unwrap(),
|
||||
Some(_) => panic!("{} is not a ModuleItem::Template", object_type),
|
||||
None => return Err(format!("template {} doesn't exist", object_type))
|
||||
};
|
||||
|
||||
match modules.get(format!("{}:func:validator", object_type).as_str()) {
|
||||
Some(ModuleItem::Validator(validate)) => validate(&obj)?,
|
||||
Some(_) => panic!("{}:func:validator is not a ModuleItem::Validator", object_type),
|
||||
None => (),
|
||||
};
|
||||
|
||||
for (name, property) in template.input {
|
||||
for transform in property.transforms {
|
||||
let destination = transform.split(":").collect::<Vec<&str>>();
|
||||
|
||||
let temp_object = ObjectInstance {
|
||||
object_type: destination[0].to_string(),
|
||||
input: json!({
|
||||
destination[1]: obj.input.get(&name)
|
||||
}),
|
||||
..ObjectInstance::default()
|
||||
};
|
||||
|
||||
match modules.get(format!("{}:func:validator", &destination[0]).as_str()) {
|
||||
Some(ModuleItem::Validator(validate)) => validate(&temp_object)?,
|
||||
Some(_) => panic!("{}:func:validator is not a ModuleItem::Validator", object_type),
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn from_template(object_type: &str, input: Value) -> Result<Self, String> {
|
||||
let instance = ObjectInstance {
|
||||
object_type: object_type.to_string(),
|
||||
input: json!(input),
|
||||
..ObjectInstance::default()
|
||||
};
|
||||
|
||||
ObjectInstance::validate(&instance)?;
|
||||
Ok(instance)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue