dejavu_parser/utils/
mod.rs

1use crate::dejavu::{ElementNode, ExpressionNode, TemplateIfNode, TemplateLNode, TemplateRNode, TextElementNode};
2
3impl TextElementNode {
4    pub fn pure_space(&self) -> bool {
5        matches!(self, Self::TextSpace { .. })
6    }
7    pub fn write_buffer(&self, w: &mut String) {
8        match self {
9            TextElementNode::Escape(_) => w.push_str("<%"),
10            TextElementNode::TextSpace(s) => w.push_str(&s.text),
11            TextElementNode::TextWord(s) => w.push_str(&s.text),
12        }
13    }
14}
15
16/// ```dejavu
17/// <% if %>
18///    then
19/// <% else if %>
20///    text
21/// <% else %>
22///    text
23/// <% end %>
24/// ```
25impl TemplateIfNode {
26    pub fn rights(&self) -> Vec<&TemplateRNode> {
27        let mut out = Vec::with_capacity(self.if_else_if.len() + 1);
28        out.push(&self.if_begin.template_r);
29        for term in &self.if_else_if {
30            out.push(&term.template_r)
31        }
32        match &self.if_else {
33            Some(s) => out.push(&s.template_r),
34            None => {}
35        }
36        out
37    }
38    pub fn lefts(&self) -> Vec<&TemplateLNode> {
39        let mut out = Vec::with_capacity(self.if_else_if.len() + 1);
40        for term in &self.if_else_if {
41            out.push(&term.template_l)
42        }
43        match &self.if_else {
44            Some(s) => out.push(&s.template_l),
45            None => {}
46        }
47        out.push(&self.if_end.template_l);
48        out
49    }
50    pub fn conditions(&self) -> Vec<&ExpressionNode> {
51        let mut out = Vec::with_capacity(self.if_else_if.len() + 1);
52        out.push(&self.if_begin.expression);
53        for term in &self.if_else_if {
54            out.push(&term.expression)
55        }
56        out
57    }
58    pub fn bodies(&self) -> Vec<&[ElementNode]> {
59        let mut out = Vec::with_capacity(self.if_else_if.len() + 1);
60        out.push(self.if_begin.element.as_slice());
61        for term in &self.if_else_if {
62            out.push(term.element.as_slice())
63        }
64        match &self.if_else {
65            Some(s) => out.push(s.element.as_slice()),
66            None => {}
67        }
68        out
69    }
70}