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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use super::*;

/// Use TemplarBuilder for creating a new customized instance of Templar.
///
/// # Usage
///
/// ```
/// # use templar::*;
/// let builder = TemplarBuilder::default(); // Default filters/functions preloaded
/// let templar = builder.build();
/// ```
pub struct TemplarBuilder {
    functions: HashMap<String, Arc<functions::Function>>,
    filters: HashMap<String, Arc<filters::Filter>>,
}

impl Default for TemplarBuilder {
    fn default() -> TemplarBuilder {
        TemplarBuilder {
            functions: functions::default_functions(),
            filters: filters::default_filters(),
        }
    }
}

impl TemplarBuilder {
    /// Create a new empty context with no filters or functions. Generally, you should be using the
    /// default context.
    pub fn new() -> TemplarBuilder {
        TemplarBuilder {
            functions: Default::default(),
            filters: Default::default(),
        }
    }

    /// Add a function to the configuration with the name specified
    pub fn add_function<T: 'static + Fn(Data) -> Data + Send + Sync>(
        &mut self,
        name: &str,
        val: T,
    ) -> &mut Self {
        self.functions.insert(name.into(), Arc::new(val));
        self
    }

    /// Remove the specified function name from the configuration
    pub fn remove_function(&mut self, name: &str) -> &mut Self {
        self.functions.remove(name);
        self
    }

    /// Add a filter to the configuration with the specified signature
    pub fn add_filter<T: 'static + Fn(Data, Data) -> Data + Send + Sync>(
        &mut self,
        name: &str,
        val: T,
    ) -> &mut Self {
        self.filters.insert(name.into(), Arc::new(val));
        self
    }

    /// Remove the specified filter name from the configuration
    pub fn remove_filter(&mut self, name: &str) -> &mut Self {
        self.filters.remove(name);
        self
    }

    /// Build a new templar instance with this configuration
    pub fn build(self) -> Templar {
        let functions = self.functions;
        let filters = self.filters;
        Templar { functions, filters }
    }
}