1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use std::collections::HashMap;
use error::{ Result, EngineError };

pub trait Loader {
    fn get_source(&self, name: &str) -> Result<String>;
}

#[derive(Debug)]
pub struct ArrayLoader {
    files: HashMap<String, String>,
}

impl ArrayLoader {
    pub fn new(sources: HashMap<String, String>) -> ArrayLoader {
        ArrayLoader {
            files: sources,
        }
    }
}

impl Loader for ArrayLoader {
    fn get_source(&self, name: &str) -> Result<String> {
        match self.files.get(name) {
            Some(contents) => Ok(contents.clone()),
            None => Err(EngineError::TemplateNotFound {
                name: name.into(),
                search_paths: Vec::new()
            }.into()),
        }
    }
}