oxblade 0.1.0

Blade templates for Laravel for Rust: Blade's markup and directives, Laravel's helpers, Rust's expressions. Community package, not affiliated with Laravel.
Documentation
use std::collections::HashMap;

use crate::{
    ast::{EchoKind, ExprKind, Node, NodeKind, PatternKind},
    span::Span,
};

/// Locals in straight-line loop output can be read from the iteration's value slot.
#[derive(Debug)]
pub(crate) struct RowProgram {
    pub(crate) name: String,
    pub(crate) output: Vec<RowOp>,
}

#[derive(Debug)]
pub(crate) enum RowOp {
    Text(usize),
    Slot {
        kind: EchoKind,
        field: Option<String>,
        receiver: Span,
        expression: Span,
    },
}

pub(super) fn compile(nodes: &[Node], rows: &mut HashMap<Span, RowProgram>) {
    for node in nodes {
        match &node.kind {
            NodeKind::Loop {
                pattern,
                body,
                empty,
                ..
            } => {
                if let PatternKind::Binding(name) = &pattern.kind
                    && let Some(output) = body
                        .iter()
                        .enumerate()
                        .map(|(index, node)| row_op(node, index, name))
                        .collect()
                {
                    rows.insert(
                        node.span,
                        RowProgram {
                            name: name.clone(),
                            output,
                        },
                    );
                }
                compile(body, rows);
                if let Some(empty) = empty {
                    compile(empty, rows);
                }
            }
            NodeKind::Conditional(branches) => {
                for branch in branches {
                    compile(&branch.body, rows);
                }
            }
            NodeKind::Switch { arms, .. } => {
                for arm in arms {
                    compile(&arm.body, rows);
                }
            }
            NodeKind::PushIf(branches) => {
                for branch in branches {
                    compile(&branch.body, rows);
                }
            }
            NodeKind::Component(component) => compile(&component.body, rows),
            NodeKind::Slot(slot) => compile(&slot.body, rows),
            NodeKind::While { body, .. }
            | NodeKind::Section { body, .. }
            | NodeKind::Block { body, .. } => compile(body, rows),
            _ => {}
        }
    }
}

fn row_op(node: &Node, index: usize, name: &str) -> Option<RowOp> {
    let (kind, expression) = match &node.kind {
        NodeKind::Text(_) => return Some(RowOp::Text(index)),
        NodeKind::Echo(kind, expression) => (*kind, expression),
        _ => return None,
    };
    let (receiver, field) = match &expression.kind {
        ExprKind::Field {
            receiver,
            name,
            optional: false,
        } => (receiver.as_ref(), Some(name.clone())),
        _ => (expression, None),
    };
    if !matches!(&receiver.kind, ExprKind::Path(path) if matches!(path.as_slice(), [part] if part == name))
    {
        return None;
    }
    Some(RowOp::Slot {
        kind,
        field,
        receiver: receiver.span,
        expression: expression.span,
    })
}