variable-codegen 0.1.4

TypeScript code generation for the Variable feature flag DSL
Documentation
use serde::Serialize;

/// Context for rendering an entire generated file.
///
/// This is the top-level data structure passed to language templates.
/// Each language's codegen module populates these structs with
/// language-specific type names, formatted values, and naming conventions.
#[derive(Debug, Serialize)]
pub struct FileContext {
    pub structs: Vec<StructContext>,
    pub features: Vec<FeatureContext>,
}

/// Context for a struct type declaration.
#[derive(Debug, Serialize)]
pub struct StructContext {
    pub name: String,
    pub fields: Vec<StructFieldContext>,
}

/// Context for a single field within a struct type.
#[derive(Debug, Serialize)]
pub struct StructFieldContext {
    pub name: String,
    pub type_name: String,
}

/// Context for a single feature within the generated file.
#[derive(Debug, Serialize)]
pub struct FeatureContext {
    pub name: String,
    pub id: u32,
    pub interface_name: String,
    pub defaults_name: String,
    pub variable_ids_name: String,
    pub fn_name: String,
    pub variables: Vec<VariableContext>,
}

/// Context for a single variable within a feature.
#[derive(Debug, Serialize)]
pub struct VariableContext {
    pub name: String,
    pub type_name: String,
    pub default_value: String,
    pub id: u32,
}

/// Render a template string with the given file context.
///
/// Uses Jinja2-style templates via minijinja with `trim_blocks` and
/// `lstrip_blocks` enabled for clean template authoring.
pub fn render(template_str: &str, context: &FileContext) -> String {
    let mut env = minijinja::Environment::new();
    env.set_trim_blocks(true);
    env.set_lstrip_blocks(true);
    env.add_template("output", template_str)
        .expect("template parse error: this is a bug in the embedded template");
    let tmpl = env.get_template("output").unwrap();
    tmpl.render(context)
        .expect("template render error: this is a bug in the template or context")
}