finish implementing the modular object format #3

Open
sel wants to merge 8 commits from connections into main
Showing only changes of commit 226b880fb6 - Show all commits

View file

@ -6,16 +6,18 @@ use crate::types::{ObjectInstance, ObjectTemplate};
use serde_json::{Value, json}; use serde_json::{Value, json};
fn get_template(object_type: &str) -> Result<ObjectTemplate, String> { impl ObjectTemplate {
let modules = item_registry(); fn from_object_type(object_type: &str) -> Result<ObjectTemplate, String> {
let modules = item_registry();
let template: ObjectTemplate = match modules.get(object_type) {
Some(ModuleItem::Template(template_str)) => toml::from_str(template_str).unwrap(), let template: ObjectTemplate = match modules.get(object_type) {
Some(_) => panic!("{} is not a ModuleItem::Template", object_type), Some(ModuleItem::Template(template_str)) => toml::from_str(template_str).unwrap(),
None => return Err(format!("template {} doesn't exist", object_type)) Some(_) => panic!("{} is not a ModuleItem::Template", object_type),
}; None => return Err(format!("template {} doesn't exist", object_type))
};
Ok(template)
Ok(template)
}
} }
impl ObjectInstance { impl ObjectInstance {
@ -33,7 +35,7 @@ impl ObjectInstance {
pub fn validate(obj: &ObjectInstance) -> Result<(), String> { pub fn validate(obj: &ObjectInstance) -> Result<(), String> {
let modules = item_registry(); let modules = item_registry();
let object_type = &obj.object_type; let object_type = &obj.object_type;
let template = get_template(object_type)?; let template = ObjectTemplate::from_object_type(object_type)?;
match modules.get(format!("{}:func:validator", object_type).as_str()) { match modules.get(format!("{}:func:validator", object_type).as_str()) {
Some(ModuleItem::Validator(validate)) => validate(&obj)?, Some(ModuleItem::Validator(validate)) => validate(&obj)?,
@ -66,7 +68,7 @@ impl ObjectInstance {
pub fn transform(obj: &ObjectInstance, source: &str, destination: &str) -> Result<Self, String> { pub fn transform(obj: &ObjectInstance, source: &str, destination: &str) -> Result<Self, String> {
let object_type = &obj.object_type; let object_type = &obj.object_type;
let template = get_template(object_type)?; let template = ObjectTemplate::from_object_type(object_type)?;
// assuming that the property to transform (source) is 'input.some.thing.nya', // assuming that the property to transform (source) is 'input.some.thing.nya',
// source_parts[0] is the category (input) // source_parts[0] is the category (input)