laravel-template-engine 0.5.0

Templates for emails, documents and notifications that your team edits, stored in the database
Documentation
//! What an editor UI reads: the template types, the filters and the syntax cheat sheet.

use serde_json::json;

use crate::assets::assets;
use crate::filters;
use crate::sandbox::{ArgKind, FilterSpec, TESTS};
use crate::schema::types;

/// The syntax cheat sheet for people editing templates, in Markdown.
pub const CHEAT_SHEET: &str = include_str!("cheat_sheet.md");

/// Everything an editor needs, as JSON:
///
/// - `types`: each template type with its variable tree (name, path, type, description, whether
///   it's optional, the fields of objects and the items of lists), its recipient variables and
///   its example data;
/// - `filters`: name, description, example and arguments of every filter templates may use,
///   the application's own included;
/// - `tests`: the names `is` accepts, such as `defined`;
/// - `images`: the pictures templates can show with `<img src="name">`: name, type and size;
/// - `fonts`: the font families PDF documents can use;
/// - `layouts`: the names of the layouts templates can choose;
/// - `channels`: the channels notification templates can write for, `sms` and `slack` first;
/// - `cheat_sheet`: [`CHEAT_SHEET`].
///
/// # Errors
///
/// Fails when a type's example doesn't serialize.
pub async fn schema() -> laravel::Result<serde_json::Value> {
    let mut specs = filters::builtin();
    let assets = assets().await?;
    specs.extend(assets.filters.iter().map(|f| f.spec.clone()));
    let images: Vec<serde_json::Value> = assets
        .images
        .iter()
        .map(|image| json!({ "name": image.name, "content_type": image.content_type, "size": image.bytes.len() }))
        .collect();
    let fonts: Vec<&str> = assets.fonts.iter().map(|font| font.family.as_str()).collect();
    let layouts = assets.layout_names();
    let channels: Vec<serde_json::Value> = crate::notification::builtin_channels()
        .iter()
        .chain(assets.channels.iter())
        .map(|channel| json!({ "name": channel.name, "description": channel.description }))
        .collect();
    let types = types()
        .iter()
        .map(crate::TypeInfo::to_json)
        .collect::<laravel::Result<Vec<_>>>()?;
    Ok(json!({
        "types": types,
        "filters": specs.iter().map(filter_json).collect::<Vec<_>>(),
        "tests": TESTS,
        "images": images,
        "fonts": fonts,
        "layouts": layouts,
        "channels": channels,
        "cheat_sheet": CHEAT_SHEET,
    }))
}

fn filter_json(spec: &FilterSpec) -> serde_json::Value {
    let args: Vec<serde_json::Value> = spec
        .args
        .iter()
        .map(|arg| {
            let kind = match arg.kind {
                ArgKind::Text => "text",
                ArgKind::Number => "number",
                ArgKind::Any => "any",
            };
            json!({ "name": arg.name, "type": kind, "required": arg.required })
        })
        .collect();
    json!({ "name": spec.name, "description": spec.description, "example": spec.example, "args": args })
}