femstache 0.1.7

Embrace the feminine mustache to unlock your templating dreams! Or something, I don't know. :3
Documentation
use std::collections::BTreeMap;

use femstache_eval::{EvalError, StringValue, TemplateEnv, TemplateInput, Value};
use femstache_loader::{TemplateLoader, TemplateSource};
use femstache_parser::Template;
use femstache_types::{Ty, TyContext, TyError, TyStore};

use crate::{
    FemstacheError, FemstacheResult,
    output::{EscapeMode, Output},
    renderer::TemplateRenderer,
};

fn make_builtins(store: &mut TyStore) -> Vec<(String, Value)> {
    let mut builtins = Vec::new();
    let arg_ty_id = store.intern(Ty::String);
    let ret_ty_id = store.intern(Ty::String);
    builtins.push((
        "raw".to_owned(),
        Value::builtin(arg_ty_id, ret_ty_id, |v| {
            let Some(s) = v.into_string() else {
                return Err(EvalError::InvalidType);
            };
            Ok(Value::String(match s {
                StringValue::Regular(s) => StringValue::Raw(s),
                StringValue::Raw(s) => StringValue::Raw(s),
            }))
        }),
    ));
    builtins
}

/// Encapsulates all of the template loading and rendering logic; this is most
/// likely what you want.
#[derive(Debug)]
pub struct Femstache {
    loader: TemplateLoader,
    escape: EscapeMode,
}

impl Femstache {
    /// Create a [Femstache] instance from a [TemplateSource] and [EscapeMode]
    pub fn new(source: TemplateSource, escape: EscapeMode) -> Self {
        let loader = TemplateLoader::new(source);
        Self { loader, escape }
    }

    /// Render the template at `path` in the store, with `inputs` as the inputs
    pub fn render(&self, path: &str, inputs: &impl TemplateInput) -> FemstacheResult<String> {
        let template = self.loader.load(path)?;
        let mut ctx = TyContext::new(self.loader.clone());
        let builtins = make_builtins(ctx.store_mut());
        for (name, value) in &builtins {
            let Some(builtin) = value.as_builtin() else {
                panic!("only builtins are supported as builtins");
            };
            let fun_ty_id = ctx.store_mut().intern(Ty::Fun(builtin.arg, builtin.ret));
            ctx.add_builtin(name, fun_ty_id);
        }
        ctx.infer_document(template.document())?;
        for (name, ty_id) in ctx.inputs() {
            // TODO: handle optional
            let Some(value) = inputs.get(name) else {
                return Err(FemstacheError::TypeError(TyError::VarNotFound(
                    name.to_owned(),
                )));
            };
            if !value.conforms_to(ctx.store(), *ty_id) {
                return Err(FemstacheError::TypeError(TyError::VarInvalidType(
                    name.to_owned(),
                )));
            }
        }
        let mut env = TemplateEnv::new(inputs);
        for (name, value) in &builtins {
            env.set(name, value.clone());
        }
        let mut renderer = TemplateRenderer::new(env, BTreeMap::new(), Some(self.loader.clone()));
        let mut output = Output::string(self.escape);
        renderer.render_document(template.document(), &mut output)?;
        Ok(output.into_inner())
    }
}

/// Render a template using the specified input
///
/// ```rust
/// # use std::collections::BTreeMap;
/// # use femstache::Template;
/// let tpl = Template::parse("hello, {{place}}! :3c").unwrap();
///
/// let mut map = BTreeMap::new();
/// map.insert("place", "delicious world");
///
/// assert_eq!(femstache::render(&tpl, &map).unwrap(), "hello, delicious world! :3c");
/// ```
pub fn render(template: &Template, inputs: &impl TemplateInput) -> FemstacheResult<String> {
    let mut ctx = TyContext::new(TemplateLoader::new(TemplateSource::none()));
    ctx.infer_document(template.document()).unwrap();
    for (name, ty_id) in ctx.inputs() {
        // TODO: handle optional
        let Some(value) = inputs.get(name) else {
            // TODO: better errors
            return Err(FemstacheError::TypeError(TyError::Conflicts(Vec::new())));
        };
        if !value.conforms_to(ctx.store(), *ty_id) {
            // TODO: better errors
            return Err(FemstacheError::TypeError(TyError::Conflicts(Vec::new())));
        }
    }
    let env = TemplateEnv::new(inputs);
    // TODO: it should be possible to set this
    let blocks = BTreeMap::new();
    let mut renderer = TemplateRenderer::new(env, blocks, None);
    let mut output = Output::string(EscapeMode::None);
    renderer.render_document(template.document(), &mut output)?;
    Ok(output.into_inner())
}