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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*!
The Templar module
 */

use crate::*;

mod builder;
mod template;

pub use builder::TemplarBuilder;
pub use template::{Template, TemplateTree};

use std::collections::BTreeMap;

lazy_static! {
    static ref GLOBAL: Templar = Templar::default();
}

/// The Templar struct is the primary template parser.
///
/// A new customized instance can be created using TemplarBuilder or alternatively
/// Templar::global() can be used if the default configurations are acceptable.
///
/// # Usage
///
/// ```
/// use templar::*;
///
/// let context = StandardContext::new();
///
/// // parse and render a template, render returns a string
/// let template = Templar::global().parse("This is a template with {{ 'an expression' }}")?;
/// assert_eq!(template.render(&context)?, "This is a template with an expression");
///
/// // parse and execute an expression, this can be converted to most native types
/// let expression = Templar::global().parse_expression("5 + 5")?;
/// assert_eq!(*expression.exec(&context), 10 as i64);
/// # Ok::<(), templar::TemplarError>(())
/// ```
pub struct Templar {
    pub(crate) functions: HashMap<String, Arc<functions::Function>>,
    pub(crate) filters: HashMap<String, Arc<filters::Filter>>,
}

impl Default for Templar {
    fn default() -> Templar {
        TemplarBuilder::default().build()
    }
}

impl Templar {
    /// Retrieve the global default instance of Templar when the defaults meet your needs.
    #[inline]
    pub fn global() -> &'static Templar {
        &GLOBAL
    }

    /// Parse a `Template` or `TemplateTree` value.
    ///
    /// ```
    /// # use templar::*;
    /// # use std::convert::TryInto;
    ///
    /// # let context = StandardContext::new();
    ///
    /// let template: Template = Templar::global().parse("{{ [5, 8, 3] | index(0) }}")?;
    /// assert_eq!(*template.exec(&context), 5 as i64);
    /// # Ok::<(), templar::TemplarError>(())
    /// ```
    #[inline]
    pub fn parse<T: Parseable<U>, U>(&self, data: T) -> Result<U> {
        T::parse_into(data, self)
    }

    /// Parse a JSON string to a TemplateTree. This is useful if you want to parse a configuration
    /// file directly to a context as TemplateTree is directly convertible to a context.
    ///
    /// # Usage
    ///
    /// ```
    /// # use templar::*;
    /// # use unstructured::Document;
    /// # use std::convert::TryInto;
    ///
    /// let json_string = r#"
    /// {
    ///     "key": "{{ script('echo -n test') | key('stdout') }}"
    /// }
    /// "#;
    ///
    /// # let context = StandardContext::new();
    ///
    /// let tree = Templar::global().parse_json(json_string)?;
    /// let template: Template = tree.get_path(&["key"]).try_into()?;
    ///
    /// assert_eq!(template.render(&context)?, "test");
    /// # Ok::<(), templar::TemplarError>(())
    /// ```
    #[inline]
    #[cfg(feature = "json-extension")]
    pub fn parse_json(&self, json: &str) -> Result<TemplateTree> {
        Ok(self.parse(&serde_json::from_str(json).wrap()?)?)
    }

    /// Identical to parse_json except this expects a YAML string.
    #[inline]
    #[cfg(feature = "yaml-extension")]
    pub fn parse_yaml(&self, yml: &str) -> Result<TemplateTree> {
        Ok(self.parse(&serde_yaml::from_str(yml).wrap()?)?)
    }
}

pub trait Parseable<T>: private::Seal {
    fn parse_into(t: Self, templar: &Templar) -> Result<T>;
}

impl Parseable<Template> for &String {
    #[inline]
    fn parse_into(t: Self, templar: &Templar) -> Result<Template> {
        templar.parse_template(t)
    }
}

impl Parseable<Template> for &str {
    #[inline]
    fn parse_into(t: Self, templar: &Templar) -> Result<Template> {
        templar.parse_template(t)
    }
}

impl Parseable<Template> for &InnerData {
    #[inline]
    fn parse_into(t: Self, templar: &Templar) -> Result<Template> {
        Ok(match t {
            InnerData::String(s) => templar.parse_template(&s)?,
            InnerData::Newtype(d) => templar.parse(d.as_ref())?,
            _ => Node::Data(t.clone().into()).into(),
        })
    }
}

impl Parseable<TemplateTree> for &InnerData {
    #[inline]
    fn parse_into(doc: Self, templar: &Templar) -> Result<TemplateTree> {
        let default_context = StandardContext::new();
        Ok(match doc {
            InnerData::Newtype(d) => templar.parse(d.as_ref())?,
            InnerData::Seq(s) => TemplateTree::Sequence(Arc::new(
                s.iter()
                    .map(|i| Ok(templar.parse(i)?))
                    .collect::<Result<Vec<TemplateTree>>>()?,
            )),
            InnerData::Map(map) => TemplateTree::Mapping(Arc::new(
                map.iter()
                    .map(|(k, v)| {
                        Ok((
                            templar
                                .parse::<Self, Template>(k)?
                                .exec(&default_context)
                                .into_inner(),
                            templar.parse(v)?,
                        ))
                    })
                    .collect::<Result<BTreeMap<InnerData, TemplateTree>>>()?,
            )),
            _ => TemplateTree::Template(templar.parse(doc)?),
        })
    }
}

mod private {
    pub trait Seal {}
    impl Seal for &crate::execution::InnerData {}
    impl Seal for &String {}
    impl Seal for &str {}
}