jens/
file.rs

1use crate::{
2    block::Block,
3    parser::{self, template::Template},
4};
5
6#[derive(Debug)]
7pub struct File {
8    pub templates: Vec<Template>,
9}
10
11impl File {
12    // TODO: Add custom parse error
13    pub fn parse(content: &str) -> Result<Self, ()> {
14        parser::parse(content)
15            .map(|templates| File { templates })
16            .map_err(|_| ())
17    }
18
19    /// Find a template in the template definition file.
20    pub fn template_opt(&self, template_name: &str) -> Option<Block> {
21        for t in &self.templates {
22            if t.name == template_name {
23                return Some(t.into());
24            }
25        }
26        None
27    }
28
29    /// Find a template in the template definition file. Panics if not found.
30    pub fn template(&self, template_name: &str) -> Block {
31        self.template_opt(template_name).unwrap()
32    }
33}