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
//! Simple templating functionality through keyword replacement.
//!
//! Replaces `##KEYWORDS##` in Strings.
use std::fmt;
use std::io;
use std::fs;
use std::path::Path;
use std::error::Error;
use std::collections::HashMap;

use regex::{Regex, Captures};
use std::ops::Deref;

/// Simple template style keyword replacement.
///
/// This allows replacing a known set of keywords looking like `##THIS##`.
/// Here it is implemented for `Deref<Target=str>`.
pub trait IsKeyword {
    /// Checks if the whole string is a keyword
    fn is_keyword(&self) -> bool;
    /// Captures keywords from string.
    fn get_keyword(&self) -> Option<String>;
    /// Well, it lists the keywords in a string, duh!
    fn list_keywords(&self) -> Vec<String>;

    /// This one is usefull.
    ///
    /// Takes a clorsure that replaces keywords.
    /// **Careful**, this replaces either way!
    /// If you get a keywords you don't want to replace,
    /// please place it back where you got it from.
    ///
    /// # Example
    /// ```ignore
    /// .map_keywords|keyword| match data.get(keyword){
    ///     Some(content) => String::from(*content),
    ///     None => format!("##{}##", keyword)
    /// }
    /// ```
    ///
    fn map_keywords<F>(&self, closure: F) -> String where F:Fn(&str) -> String;// -> Option<String>;
}

static REGEX: &'static str = r"##([0-9A-Z-]*)##*";

/// Allows very simplistic `##KEYWORD##` replacement.
impl<U:Deref<Target=str>> IsKeyword for U {

    /// Checks if the whole string is a keyword
    fn is_keyword(&self) -> bool{
        Regex::new(REGEX).expect("broken regex").is_match(self)
    }

    /// Captures keywords from string.
    fn get_keyword(&self) -> Option<String> {
        Regex::new(REGEX).expect("broken regex")
            .captures(self)
            .and_then(|caps| caps.get(1).map(|c| c.as_str().to_owned()))
    }

    /// Well, it lists the keywords in a string, duh!
    fn list_keywords(&self) -> Vec<String>{
        Regex::new(REGEX).expect("broken regex")
            .captures_iter(self)
            .filter_map(|c|c.get(1))
            .map(|c|c.as_str().to_owned())
            .collect()
    }

    /// This one is usefull.
    ///
    /// Takes a clorsure that replaces keywords.
    /// **Careful**, this replaces either way!
    /// If you get a keywords you don't want to replace,
    /// please place it back where you got it from.
    ///
    /// # Example
    /// ```ignore
    /// .map_keywords|keyword| match data.get(keyword){
    ///     Some(content) => String::from(*content),
    ///     None => format!("##{}##", keyword)
    /// }
    /// ```
    ///
    fn map_keywords<F>(&self, closure: F) -> String
        where F:Fn(&str) -> String{
        Regex::new(REGEX).expect("broken regex")
            .replace_all(self, |caps:&Captures| {
                closure(caps.get(1).unwrap().as_str())
            }).into()
    }
}


/// Simple templating module
#[derive(Debug)]
pub struct Templater{
    /// content of template file after reading
    pub original: String,

    /// content of filled template
    pub filled: String,
}

impl Templater{

    pub fn new(template:&str) -> Templater{
        Templater{
            original:template.to_owned(),
            filled: String::new(),
        }
    }

    pub fn from_file(path: &Path) -> Result<Templater, io::Error> {
        let template = fs::read_to_string(&path)?;
        Ok(Templater::new(&template))
    }

    pub fn finalize(&mut self) -> Templater {
        self.to_owned()
    }

    /// Creates a finished version of the output.
    ///
    /// If any keywords remain unfilled, `Err` contains a list of left overs.
    pub fn complete(&mut self) -> TemplateResult<Templater>{
        let left_overs = self.filled.list_keywords();

        if left_overs.is_empty(){
            Ok(self.to_owned())
        } else {
            Err(TemplateError::Incomplete(left_overs))
        }
    }

    pub fn fix(&self) -> Self{
        Templater{
            original: self.filled.to_owned(),
            filled: String::new()
        }
    }

    pub fn fill_in_field(&mut self, field: &str, value: &str) -> &mut Templater {
        self.fill_template(|keyword|
                           if keyword == field{
                               value.to_owned()
                           } else {
                               format!("##{}##", keyword)
                           })
    }

    pub fn fill_in_data(&mut self, data: &HashMap<&str,String>) -> &mut Templater {
        self.fill_template(|keyword| match data.get(keyword){
            Some(content) => content.clone(),
            None => format!("##{}##", keyword)
        })
    }

    pub fn list_keywords(&self) -> Vec<String>{
        self.original.list_keywords()
    }

    pub fn fill_template<F>(&mut self, closure: F) -> &mut Templater
        where F:Fn(&str) -> String {
        self.filled = self.original.map_keywords(closure);
        self
    }
}

pub type TemplateResult<T> = Result<T, TemplateError>;

#[derive(Debug,PartialEq)]
pub enum TemplateError{
    Incomplete(Vec<String>)
}

impl fmt::Display for TemplateError{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result{
        match *self {
            TemplateError::Incomplete(ref list) => write!(f, "{} ({:?})", self.description(), list),
        }
    }
}

impl Error for TemplateError{
    fn description(&self) -> &str{ "The template was not filled completely." }
    fn cause(&self) -> Option<&Error>{None}
}

use std::borrow::ToOwned;
impl ToOwned for Templater{
    type Owned = Templater;
    fn to_owned(&self) -> Templater {
        Templater{
            //path :    self.path.to_owned(),
            original: self.original.to_owned(),
            filled:   self.filled.to_owned()
        }
    }
}

#[cfg(test)]
mod test{
    use super::Templater;
    const TEMPLATE:&'static str = r##"This tests ##TEST## for ##ATTR## ##SUBJ##."##;

   #[test]
   fn complete(){
       let filled_in = Templater::new(TEMPLATE)
           .fill_in_data(&hashmap!{
               "TEST" => String::from("templates"),
               "ATTR" => String::from("complete"),
               "SUBJ" => String::from("replacements"),
           }).complete().unwrap();
       assert_eq!(filled_in.filled, "This tests templates for complete replacements.")
   }

   #[test]
   fn chain(){
       let filled_in = Templater::new(TEMPLATE)
           .fill_in_data(&hashmap!{
               "TEST" => String::from("templates")}).fix()
           .fill_in_data(&hashmap!{
               "ATTR" => String::from("complete"),
               "SUBJ" => String::from("replacements") })
       .complete().unwrap();
       assert_eq!(filled_in.filled, "This tests templates for complete replacements.")
   }

   #[test]
   fn not_complete(){
       let filled_in = Templater::new(TEMPLATE)
           .fill_in_data(&hashmap!{
               "TEST" => String::from("templates"),
           }).complete();
       assert!(filled_in.is_err());
       assert_eq!(filled_in.unwrap_err(), super::TemplateError::Incomplete(
               vec![
               String::from("ATTR"),
               String::from("SUBJ")]
               ))
   }

}