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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use crate::context::*;
use crate::default_functions::{val_length, val_range, val_stringify};
use crate::exec::*;
use crate::expr::*;
use serde_json::Value;
use std::collections::HashMap;
use std::fs;
use std::io;
use std::path::PathBuf;
use std::sync::Mutex;

/// HTML template rendering engine that should be constructed for once.
pub struct Dojang {
    /// Mapping between the template file name and the renderer along with the file content.
    templates: HashMap<String, (Executer, String)>,

    /// Map of the registered functions.
    functions: HashMap<String, FunctionContainer>,

    /// Files read from "include". Those are cached here.
    includes: Mutex<HashMap<String, String>>,
}

impl Dojang {
    /// Creates a template engine.
    pub fn new() -> Self {
        let mut functions = HashMap::<String, FunctionContainer>::new();
        functions.insert(
            "length".to_string(),
            FunctionContainer::F1(Box::new(val_length)),
        );

        functions.insert(
            "range".to_string(),
            FunctionContainer::F1(Box::new(val_range)),
        );

        functions.insert(
            "json_stringify".to_string(),
            FunctionContainer::F1(Box::new(val_stringify)),
        );

        Dojang {
            templates: HashMap::new(),
            functions,
            includes: Mutex::new(HashMap::new()),
        }
    }

    /// Adds a template file to the engine.
    ///
    /// If there is already an existing template with same name, this will return error.
    ///
    /// # Arguments
    ///
    /// * `file_name` - Name of the template file. Template datas are identified by this name.
    /// * `template` - Actual template data. Should be using EJS syntax.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut dojang = dojang::Dojang::new();
    ///
    /// // Constructs the template "tmpl" with the content "<%= 1 + 1 %>".
    /// dojang.add("tmpl".to_string(), "<%= 1 + 1 %>".to_string());
    /// ```
    pub fn add(&mut self, file_name: String, template: String) -> Result<&Self, String> {
        if self.templates.contains_key(&file_name) {
            return Err(format!("{} is already added as a template", file_name));
        }

        self.templates.insert(
            file_name,
            (Executer::new(Parser::parse(&template)?)?, template),
        );

        Ok(self)
    }

    /// Adds a function that can be used in the template.
    ///
    /// If there is already an existing function with same name, this will return error.
    /// Use the appropriate function based on the number of parameters that the function take.
    /// (e.g for functions taking 2 params, use add_function_2). Functions with 4 params are
    /// supported at max.
    ///
    /// Note that the parameter of the function must be convertible to Value. Supported types are
    /// String, i64, f64 and boolean.
    ///
    /// # Arguments
    ///
    /// * `function_name` - Name of the function.
    /// * `function` - The body of the function.
    ///
    /// # Examples
    ///
    /// ```
    /// use serde_json::Value;
    /// use dojang::dojang::Dojang;
    ///
    /// fn func(a: i64) -> i64 { a + 1 }
    /// fn func2(mut a: String, b: String) -> String {
    ///     a.push_str(&b); a.push_str("hi"); a
    /// }
    ///
    /// let mut dj = Dojang::new();
    ///
    /// dj.add_function_1("func".to_string(), func);
    /// dj.add_function_2("func2".to_string(), func2);
    /// ```

    pub fn add_function_1<T: 'static, V: 'static>(
        &mut self,
        function_name: String,
        function: fn(T) -> V,
    ) -> Result<&Self, String>
    where
        T: From<Operand>,
        V: Into<Operand>,
    {
        if self.functions.contains_key(&function_name) {
            return Err(format!("{} is already added as a function", function_name));
        }

        self.functions
            .insert(function_name, to_function_container1(function));
        Ok(self)
    }

    pub fn add_function_2<T1: 'static, T2: 'static, V: 'static>(
        &mut self,
        function_name: String,
        function: fn(T1, T2) -> V,
    ) -> Result<&Self, String>
    where
        T1: From<Operand>,
        T2: From<Operand>,
        V: Into<Operand>,
    {
        if self.functions.contains_key(&function_name) {
            return Err(format!("{} is already added as a function", function_name));
        }

        self.functions
            .insert(function_name, to_function_container2(function));
        Ok(self)
    }

    pub fn add_function_3<T1: 'static, T2: 'static, T3: 'static, V: 'static>(
        &mut self,
        function_name: String,
        function: fn(T1, T2, T3) -> V,
    ) -> Result<&Self, String>
    where
        T1: From<Operand>,
        T2: From<Operand>,
        T3: From<Operand>,
        V: Into<Operand>,
    {
        if self.functions.contains_key(&function_name) {
            return Err(format!("{} is already added as a function", function_name));
        }

        self.functions
            .insert(function_name, to_function_container3(function));
        Ok(self)
    }

    pub fn add_function_4<T1: 'static, T2: 'static, T3: 'static, T4: 'static, V: 'static>(
        &mut self,
        function_name: String,
        function: fn(T1, T2, T3, T4) -> V,
    ) -> Result<&Self, String>
    where
        T1: From<Operand>,
        T2: From<Operand>,
        T3: From<Operand>,
        T4: From<Operand>,
        V: Into<Operand>,
    {
        if self.functions.contains_key(&function_name) {
            return Err(format!("{} is already added as a function", function_name));
        }

        self.functions
            .insert(function_name, to_function_container4(function));
        Ok(self)
    }

    /// Load files under the provided directory as templates.
    ///
    /// Note that it does not recursively visit every underlying directories. Only the files that
    /// live in the current directory will be added to the engine.
    ///
    /// If the file is not readable, then it will be ignored (will show an error message)
    ///
    /// # Arguments
    ///
    /// * `dir_name` - Name of the directory.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut dojang = dojang::Dojang::new();
    ///
    /// // Add every files under ./tests as a template.
    /// dojang.load("./tests");
    /// ```
    pub fn load(&mut self, dir_name: &str) -> Result<&mut Self, String> {
        match get_all_file_path_under_dir(dir_name) {
            Ok(files) => {
                for file in files {
                    let file_name = file
                        .file_name()
                        .unwrap()
                        .to_os_string()
                        .into_string()
                        .unwrap();

                    if self.templates.contains_key(&file_name) {
                        println!("Template {} is already added", file_name);
                        continue;
                    }

                    let file_content = std::fs::read_to_string(&file);

                    if file_content.is_err() {
                        println!(
                            "Unable to read file '{:?}', error : {:?}",
                            file, file_content
                        );
                        continue;
                    }

                    let file_content = file_content.unwrap();
                    self.templates.insert(
                        file_name,
                        (Executer::new(Parser::parse(&file_content)?)?, file_content),
                    );
                }
            }
            Err(e) => return Err(e.to_string()),
        }

        Ok(self)
    }

    /// Render the page with the provided context.
    ///
    /// # Arguments
    ///
    /// * `file_name` : Name of the template file that should be rendered.
    /// * `value` : JSON value that is provided as a context. Note that this function consumes the
    /// json data.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut dojang = dojang::Dojang::new();
    ///
    /// // Render 'template_file' with the provided context.
    /// dojang.load("./tests").unwrap().render("template_file", serde_json::from_str(r#"{ "test" : { "title" : "Welcome to Dojang"} }"#).unwrap());
    /// ```
    pub fn render(&mut self, file_name: &str, value: Value) -> Result<String, String> {
        if let Some((executer, file_content)) = self.templates.get(&file_name.to_string()) {
            executer.render(
                &mut Context::new(value),
                &self.templates,
                &self.functions,
                file_content,
                &mut self.includes,
            )
        } else {
            Err(format!("Template {} is not found", file_name))
        }
    }
}

fn get_all_file_path_under_dir(dir_name: &str) -> io::Result<Vec<PathBuf>> {
    fs::read_dir(dir_name)?
        .into_iter()
        .map(|x| x.map(|entry| entry.path()))
        .collect()
}

pub fn to_function_container1<T: 'static + From<Operand>, V: 'static + Into<Operand>>(
    func: fn(T) -> V,
) -> FunctionContainer {
    FunctionContainer::F1(Box::new(move |v: Operand| -> Operand {
        func(v.into()).into()
    }))
}

pub fn to_function_container2<
    'a,
    T1: 'static + From<Operand>,
    T2: 'static + From<Operand>,
    V: 'static + Into<Operand>,
>(
    func: fn(T1, T2) -> V,
) -> FunctionContainer {
    FunctionContainer::F2(Box::new(move |v1: Operand, v2: Operand| -> Operand {
        func(v1.into(), v2.into()).into()
    }))
}

pub fn to_function_container3<
    'a,
    T1: 'static + From<Operand>,
    T2: 'static + From<Operand>,
    T3: 'static + From<Operand>,
    V: 'static + Into<Operand>,
>(
    func: fn(T1, T2, T3) -> V,
) -> FunctionContainer {
    FunctionContainer::F3(Box::new(
        move |v1: Operand, v2: Operand, v3: Operand| -> Operand {
            func(v1.into(), v2.into(), v3.into()).into()
        },
    ))
}

pub fn to_function_container4<
    'a,
    T1: 'static + From<Operand>,
    T2: 'static + From<Operand>,
    T3: 'static + From<Operand>,
    T4: 'static + From<Operand>,
    V: 'static + Into<Operand>,
>(
    func: fn(T1, T2, T3, T4) -> V,
) -> FunctionContainer {
    FunctionContainer::F4(Box::new(
        move |v1: Operand, v2: Operand, v3: Operand, v4: Operand| -> Operand {
            func(v1.into(), v2.into(), v3.into(), v4.into()).into()
        },
    ))
}

#[test]
fn render() {
    let template = "<% if a == 1 { %> Hi <% } else { %><%= a %><% } %>".to_string();
    let mut dojang = Dojang::new();
    assert!(dojang.add("some_template".to_string(), template).is_ok());

    assert_eq!(
        dojang
            .render(
                "some_template",
                serde_json::from_str(r#"{ "a" : 1 }"#).unwrap()
            )
            .unwrap(),
        " Hi "
    );

    assert_eq!(
        dojang
            .render(
                "some_template",
                serde_json::from_str(r#"{ "a" : 2 }"#).unwrap()
            )
            .unwrap(),
        "2"
    );
}

#[test]
fn render_from_dir() {
    let mut dojang = Dojang::new();
    assert!(dojang.load("./tests").is_ok());

    println!(
        "{}",
        dojang
            .render(
                "test.html",
                serde_json::from_str(r#"{ "test" : { "title" : "Welcome to Dojang"} }"#).unwrap()
            )
            .unwrap()
    );

    assert_eq!(
        dojang
            .render(
                "test.html",
                serde_json::from_str(r#"{ "test" : { "title" : "Welcome to Dojang"} }"#).unwrap()
            )
            .unwrap(),
        r#"<html>
  Welcome to Dojang
  <body>
    <p>some content</p><p>x is0</p><p>x is1</p><p>x is2</p><p>x is3</p><p>x is4</p><p>x is5</p><p>x is6</p><p>x is7</p><p>x is8</p><p>x is9</p>
  </body>
</html>
"#
    );
}

#[cfg(test)]
fn func(a: i64, b: i64) -> i64 {
    a + b
}

#[test]
fn render_function() {
    let template = r#"func(a,b) = <%= func(a, b) %>"#.to_string();
    let mut dojang = Dojang::new();
    assert!(dojang.add("some_template".to_string(), template).is_ok());
    assert!(dojang.add_function_2("func".to_string(), func).is_ok());

    assert_eq!(
        dojang
            .render("some_template", serde_json::json!({"a" : 1, "b" : 2 }))
            .unwrap(),
        "func(a,b) = 3"
    );
}

#[test]
fn for_use_range_function() {
    let template = r#"<% for i in range(v) { %><%= i %><% } %>"#.to_string();
    let mut dojang = Dojang::new();
    assert!(dojang.add("some_template".to_string(), template).is_ok());

    assert_eq!(
        dojang
            .render("some_template", serde_json::json!({"v" : 10}))
            .unwrap(),
        "0123456789"
    );
}

#[test]
fn use_length_function() {
    let template = r#"<%= length(s) %>"#.to_string();
    let mut dojang = Dojang::new();
    assert!(dojang.add("some_template".to_string(), template).is_ok());

    assert_eq!(
        dojang
            .render("some_template", serde_json::json!({"s" : "abc"}))
            .unwrap(),
        "3"
    );
}

#[test]
fn use_json_stringify_function() {
    let template = r#"<%- json_stringify(s) %>"#.to_string();
    let mut dojang = Dojang::new();
    assert!(dojang.add("some_template".to_string(), template).is_ok());

    assert_eq!(
        dojang
            .render("some_template", serde_json::json!({"s" : [1,2,3]}))
            .unwrap(),
        "[1,2,3]"
    );
}