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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//! # A Minimal Text Template Engine
//! 
//! ## Overview
//! This library implements templates consisting of text including named placeholders.
//! Placeholders are special character sequences: their names surrounded by `${` and `}`.
//! The example Template `Hello␣${name}` consists of the text `Hello␣` and the placeholder `name`.
//! 
//! Trailing and pending whitespace inside placeholder elements are significant and conditionally
//! included in the output if the value to be inserted for the placeholder is not empty. For
//! example `${title␣}${name}` may evaluate to `Dr.␣X` or `Y` while `${title}␣${name}` may evaluate to
//! `Dr.␣X` or `␣Y`.
//! 
//! Templates are represented by the structure [`Template`].
//! The templates implementation of `From<&str>` can be used to construct templates from strings.
//! Templates can be filled in by using [`fill_in`] or [`try_fill_in`], which replace any 
//! placeholders in the template by the given values. The returned [`Text`] structure is simply
//! a wrapper type around `Vec<&str>` and dereferences to it.
//! 
//! A text representation of the templates and the filled in results can be obtained by using `to_string`.
//! 
//! This library only stores references to the given template strings. It allocates a `Vec`
//! to store a list of text and placeholder elements while parsing a template string. A template,
//! once created, can be used multiple times to fill in different sets of values.
//! 
//! ## Example
//! 
//!     use text_template::*;
//!     use std::collections::HashMap;
//! 
//!     let template = Template::from("Hello ${title }${name}");
//! 
//!     let mut values = HashMap::new();
//!     values.insert("name", "Jane");
//! 
//!     let text = template.fill_in(&values);
//!
//!     assert_eq!(text.to_string(), "Hello Jane");
//!     assert_eq!(template.to_string(), "Hello ${title }${name}");
//!
//! [`Piece`]: enum.Piece.html
//! [`Piece::Text`]: enum.Piece.html#variant.Text
//! [`Piece::Placeholder`]: enum.Piece.html#variant.Placeholder
//! [`Pieces`]: struct.Pieces.html
//! [`Template`]: struct.Template.html
//! [`fill_in`]: struct.Template.html#method.fill_in
//! [`try_fill_in`]: struct.Template.html#method.try_fill_in
//! [`Text`]: struct.Text.html

use std::collections::HashMap;
use std::fmt;
use std::ops::{Deref,DerefMut};


/// Main data structure of this crate.
/// 
/// Use [`Template::from`] to create a template from a `&str` and [`fill_in`] or [`try_fill_in`] to replace
/// the placeholders with some actual values.
/// 
/// Each template is mainly a vector of `Piece`.
/// 
/// [`fill_in`]: struct.Template.html#method.fill_in
/// [`try_fill_in`]: struct.Template.html#method.try_fill_in
/// [`Template::from`]: struct.Template.html#method.from
#[derive(Clone,PartialEq,Debug)]
pub struct Template<'a>(Pieces<'a>);

impl<'a> Deref for Template<'a> {
    type Target = Vec<Piece<'a>>;
    fn deref(&self) -> &Self::Target {
        &(self.0).0
    }
}

impl<'a> DerefMut for Template<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut (self.0).0
    }
}

impl<'a> fmt::Display for Template<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl<'a> Template<'a> {
    pub fn with_pieces(v: Vec<Piece<'a>>) -> Self {
        Template(Pieces(v))
    }

    /// Fills in all placeholder elements. If `lookup_tbl` does not contain the value for a placeholder,
    /// an empty string is inserted instead.
    pub fn fill_in(&'a self, lookup_tbl: &HashMap<&'a str, &'a str>) -> Text<'a> {
        let mut t = Text::new();

        for v in self.0.iter() {
            match v {
                Piece::Text(s) => t.push(s),
                Piece::Placeholder{name, before, after} => {
                    let entry = lookup_tbl.get(name);
                    match entry {
                        Some(value) if value.len() > 0 => {
                            if before.len() > 0 { t.push(before) };
                            t.push(value);
                            if after.len() > 0 { t.push(after) };
                        }
                        _ => {}
                    }
                }
            }
        }
        
        t
    }

    /// Tries to fill in all placeholder elements. If `lookup_tbl` does not contain the value for a placeholder,
    /// a `TemplateError` is returned.
    pub fn try_fill_in(&'a self, lookup_tbl: &HashMap<&'a str, &'a str>) -> Result<Text<'a>, TemplateError> {
        let mut t = Text::new();

        for v in self.0.iter() {
            match v {
                Piece::Text(s) => t.push(s),
                Piece::Placeholder{name, before, after} => {
                    let entry = lookup_tbl.get(name);
                    match entry {
                        Some(value) => {
                            if before.len() > 0 { t.push(before) };
                            t.push(value);
                            if after.len() > 0 { t.push(after) };
                        },
                        None => { return Err(TemplateError) }
                    }
                }
            }
        }
        
        Ok(t)
    }

    /*pub fn pieces(&self) -> &Pieces {
        &self.0
    }*/
}

impl<'a> From<&'a str> for Template<'a> {
    fn from(s: &'a str) -> Self {
        enum State{InText, InPlaceholder};

        let mut v: Vec<Piece> = vec![];
        let mut state = State::InText;
        let mut rest = s;
        
        while rest.len() > 0 {
            match state {
                State::InText => {
                    if let Some(idx) = rest.find("${") {
                        v.push(Piece::Text(&rest[..idx]));
                        rest = &rest[idx+2..];
                        state = State::InPlaceholder;
                    } else {
                        v.push(Piece::Text(rest));
                        rest = &rest[0..0];
                    }
                }
                State::InPlaceholder => {
                    if let Some(idx) = rest.find("}") {
                        let (before, name, after) = trim_split(&rest[..idx]);
                        v.push(Piece::Placeholder{name, before, after});

                        rest = &rest[idx+1..];
                        state = State::InText;                       
                    } else {
                        v.push(Piece::Text(rest));
                        rest = &rest[0..0];
                    }
                }
            }
        }
        Template::with_pieces(v)
    }    
}



/// A vector of pieces.
#[derive(Clone,PartialEq,Debug)]
struct Pieces<'a>(Vec<Piece<'a>>);

impl<'a> Pieces<'a> {
}

impl<'a> Deref for Pieces<'a> {
    type Target = Vec<Piece<'a>>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a> fmt::Display for Pieces<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.iter().map(|v| v.fmt(f) ).collect()
    }
}

/// A piece of template, either text or a placeholder to be substituted.
#[derive(Clone,PartialEq,Debug)]
pub enum Piece<'a> {
    Text(&'a str),
    Placeholder{name: &'a str, before: &'a str, after: &'a str}
}

impl<'a> fmt::Display for Piece<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Piece::Text(s) => { write!(f, "{}", s) },
            Piece::Placeholder{name, before, after} => {
                write!(f, "${{{}{}{}}}", before, name, after)
            }
        }
    }
}


/// Simple wrapper around Vec<&str>, returned from `Template::fill_in`.
/// 
/// Implements `Deref<Vec<&str>>` and `DerefMut<Vec<&str>>`.
#[derive(Debug)]
pub struct Text<'a>(Vec<&'a str>);

impl<'a> Deref for Text<'a> {
    type Target = Vec<&'a str>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a> DerefMut for Text<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<'a> Text<'a> {
    fn new() -> Self {
        Text(Vec::new())
    }
}

impl<'a> fmt::Display for Text<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.iter().map(|v| v.fmt(f) ).collect()
    }
}

#[derive(Debug)]
/// Returned by `Template::try_fill_in` in case of an error.
pub struct TemplateError;

// Paritions s into (whitespace, non-whitespace, whitespace).
fn trim_split
(s: &str) -> (&str, &str, &str) {
    let mut name = s;
    let before = if let Some((last,_)) = s.chars().enumerate().take_while(|(_,v)| v.is_whitespace()).last() {
        name = &name[last+1..];
        &s[..last+1]
    } else {
        ""
    };

    let after = if let Some((first,_)) = name.chars().rev().enumerate().take_while(|(_,v)| v.is_whitespace()).last() {
        let res = &name[name.len() - first - 1..];
        name = &name[..name.len() - first - 1];
        res
    } else {
        ""
    };

    (before, name, after)
}


#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;



    #[test]
    fn split() {
        assert_eq!(trim_split("Hallo"), ("", "Hallo", ""));
        assert_eq!(trim_split(" Hallo"), (" ", "Hallo", ""));
        assert_eq!(trim_split("Hallo "), ("", "Hallo", " "));
        assert_eq!(trim_split(" Hallo "), (" ", "Hallo", " "));
    }

    #[test]
    fn from() {
        assert_eq!(*Template::from(""), vec![]);
        assert_eq!(*Template::from("{"), vec![Piece::Text("{")]);
        assert_eq!(*Template::from("}"), vec![Piece::Text("}")]);
    }

    #[test]
    fn to_string() {
        assert_eq!(Template::from("").to_string(), "");
        assert_eq!(Template::from("{").to_string(), "{");
        assert_eq!(Template::from("}").to_string(), "}");
        assert_eq!(Template::from("${}").to_string(), "${}");
        assert_eq!(Template::from("${x}").to_string(), "${x}");
        assert_eq!(Template::from(" ${x}").to_string(), " ${x}");
        assert_eq!(Template::from("${x} ").to_string(), "${x} ");
        assert_eq!(Template::from("${x }").to_string(), "${x }");
        assert_eq!(Template::from("${ x}").to_string(), "${ x}");
        assert_eq!(Template::from("${ x }").to_string(), "${ x }");         
        assert_eq!(Template::from("Hallo ${name}").to_string(), "Hallo ${name}");
    }

    #[test]
    fn fill_in() {
        let mut dict = HashMap::new();
        dict.insert("k", "v");
        dict.insert("l", "");

        assert_eq!(Template::from("${}").fill_in(&dict).to_string(), "");
        assert_eq!(Template::from("${k}").fill_in(&dict).to_string(), "v");
        assert_eq!(Template::from("${ k }").fill_in(&dict).to_string(), " v ");
        assert_eq!(Template::from("${l}").fill_in(&dict).to_string(), "");
        assert_eq!(Template::from("${ l }").fill_in(&dict).to_string(), "");
    }
}