colap 0.2.0

A lightweight, human-friendly configuration language parser & code generator
#[derive(Debug, Clone, Default)]
pub struct {{struct_name}} {
{{#each fields}}
    pub {{name}}: {{type}},
{{/each}}
}

impl {{struct_name}} {
    pub fn from_model(model: &colap::model::config_model::ConfigModel) -> Self {
        Self::from_entity(model, model.root_id())
    }
    
    pub fn from_entity(model: &colap::model::config_model::ConfigModel, id: usize) -> Self {
        let mut result = Self::default();
        {{#each field_initializers}}
        {{#if is_entity}}
        {{#if (eq type "String")}} {{!-- Handle String fields as primitives, not entities --}}
        if let Some(node) = model.get_node(id) {
            let node_b = node.borrow();
            if let colap::model::config_model::ConfigNode::Entity(ent) = &*node_b {
                if let Some(val) = ent.fields.get("{{original_name}}") {
                    if let colap::model::config_model::ConfigValue::String(s) = val {
                        result.{{name}} = s.clone();
                    }
                }
            }
        }
        {{else if (eq type "Models")}} {{!-- Handle Models type specifically --}}
        result.{{name}} = Models::from_children(model, model.find_child_entity_by_name(id, "{{original_name}}").unwrap_or(0));
        {{else}}
        {{#if is_plural}}
        result.{{name}} = {{type}}::from_children(model, id);
        {{else}}
        result.{{name}} = model.find_child_entity_by_name(id, "{{original_name}}").map(|child_id| {{type}}::from_entity(model, child_id)).unwrap_or_default();
        {{/if}}
        {{/if}}
        {{else}}
        if let Some(node) = model.get_node(id) {
            let node_b = node.borrow();
            if let colap::model::config_model::ConfigNode::Entity(ent) = &*node_b {
                if let Some(val) = ent.fields.get("{{original_name}}") {
                    {{#if (eq type "String")}}
                    if let colap::model::config_model::ConfigValue::String(s) = val {
                        result.{{name}} = s.clone();
                    }
                    {{/if}}
                    {{#if (eq type "i64")}}
                    if let colap::model::config_model::ConfigValue::Integer(i) = val {
                        result.{{name}} = *i;
                    }
                    {{/if}}
                    {{#if (eq type "f64")}}
                    if let colap::model::config_model::ConfigValue::Float(f) = val {
                        result.{{name}} = *f;
                    }
                    {{/if}}
                    {{#if (eq type "bool")}}
                    if let colap::model::config_model::ConfigValue::Boolean(b) = val {
                        result.{{name}} = *b;
                    }
                    {{/if}}
                }
            }
        }
        {{/if}}
        {{/each}}
        result
    }
}