mage 0.2.0

An intuitive and powerful template engine.
Documentation

use keyword::*;
use eval::Expr;


#[derive(Debug, Clone)]
pub struct Node {
    pub node_type: NodeType,
    pub closed: bool,
}

impl Node {
    pub fn new(node_type: NodeType) -> Node {
        Node {
            closed: node_type.is_closed(),
            node_type: node_type,
        }
    }

    pub fn is_include(&self) -> bool {
        match self.node_type {
            NodeType::Include(_) => true,
            _ => false,
        }
    }

    pub fn is_if(&self) -> bool {
        match self.node_type {
            NodeType::If(_) => true,
            _ => false,
        }
    }

    pub fn is_else_if(&self) -> bool {
        match self.node_type {
            NodeType::ElseIf(_) => true,
            _ => false,
        }
    }

    pub fn get_expr(&self) -> &Expr {
        match self.node_type {
            NodeType::If(ref _if) => &_if.expr,
            NodeType::ElseIf(ref else_if) => &else_if.expr,
            NodeType::For(ref _for) => &_for.expr,
            NodeType::Set(ref set) => &set.expr,
            _ => panic!("no expression"),
        }
    }

    pub fn get_if_mut(&mut self) -> &mut If {
        match self.node_type {
            NodeType::If(ref mut _if) => _if,
            _ => panic!("node type is not `if`"),
        }
    }

    pub fn get_include_path(&self) -> &str {
        match self.node_type {
            NodeType::Include(ref include) => &include.path,
            _ => panic!("node type is not `include`"),
        }
    }

    pub fn add_child(&mut self, node: Node) {
        match self.node_type {
            NodeType::Root(ref mut root) => root.nodes.push(Box::new(node)),
            NodeType::If(ref mut _if) => _if.nodes.push(Box::new(node)),
            NodeType::For(ref mut _for) => _for.nodes.push(Box::new(node)),
            NodeType::Else(ref mut _else) => _else.nodes.push(Box::new(node)),
            NodeType::ElseIf(ref mut else_if) => else_if.nodes.push(Box::new(node)),
            _ => panic!("can not add child"),
        }
    }
}


#[derive(Debug, Clone)]
pub enum NodeType {
    Raw(String),
    End,
    Include(Include),
    Expression(Expr),
    Set(Set),
    Root(Root),
    If(If),
    Else(Else),
    ElseIf(ElseIf),
    For(For),
}

impl NodeType {
    fn is_closed(&self) -> bool {
        match *self {
            NodeType::Raw(_) |
            NodeType::End |
            NodeType::Expression(_) |
            NodeType::Include(_) |
            NodeType::Set(_) => true,
            _ => false,
        }
    }
}