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
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

use regex::Regex;

pub struct TemplateNest<'a> {
    /// Delimiters used in the template. It is a tuple of two strings,
    /// representing the start and end delimiters.
    delimiters: (&'a str, &'a str),

    /// Name label used to identify the template to be used.
    label: &'a str,

    /// Template extension, appended on label to identify the template.
    extension: &'a str,

    /// Directory where templates are located.
    directory: PathBuf,
}

/// Represents an indexed template file.
struct TemplateFileIndex {
    /// Contents of the file.
    contents: String,

    /// Variables in the template file.
    variables: Vec<TemplateFileVariable>,
}

/// Represents the variables in a template file.
struct TemplateFileVariable {
    name: String,

    /// Start & End positions of the complete variable string. i.e. including
    /// the delimeters.
    start_position: usize,
    end_position: usize,
}

/// Represents a variable in a template hash, can be a string, another template
/// hash or an array of template hash.
pub enum Filling {
    Text(String),
    List(Vec<Filling>),
    Template(HashMap<String, Filling>),
}

impl TemplateNest<'_> {
    /// Creates a new instance of TemplateNest with the specified directory.
    pub fn new(directory_str: &str) -> Result<Self, String> {
        let directory = PathBuf::from(directory_str);
        if !directory.is_dir() {
            return Err(format!("Expected directory at: {}", directory_str));
        }

        let label = &"TEMPLATE";
        let extension = &"html";
        let delimiters = ("<!--%", "%-->");

        Ok(Self {
            directory,
            delimiters,
            label,
            extension,
        })
    }

    /// Given a template name, returns the "index" of the template file, it
    /// contains the contents of the file and all the variables that are
    /// present.
    fn index(&self, template_name: &str) -> Result<TemplateFileIndex, String> {
        let file = self
            .directory
            .join(format!("{}.{}", template_name, self.extension));
        if !file.is_file() {
            return Err(format!("Expected file at: {}", file.display()));
        }

        let contents = match fs::read_to_string(&file) {
            Ok(file_contents) => file_contents,
            Err(err) => {
                return Err(format!("Error reading file: {}", err));
            }
        };

        let mut variables = vec![];
        // Capture all the variables in the template.
        let re = Regex::new(&format!("{}(.+?){}", self.delimiters.0, self.delimiters.1)).unwrap();
        for cap in re.captures_iter(&contents) {
            let whole_capture = cap.get(0).unwrap();

            variables.push(TemplateFileVariable {
                name: cap[1].trim().to_string(),
                start_position: whole_capture.start(),
                end_position: whole_capture.end(),
            });
        }

        let file_index = TemplateFileIndex {
            contents,
            variables,
        };
        Ok(file_index)
    }

    /// Given a TemplateHash, it parses the TemplateHash and renders a String
    /// output.
    pub fn render(&self, filling: &Filling) -> Result<String, String> {
        match filling {
            Filling::Text(text) => Ok(text.to_string()),
            Filling::List(list) => {
                let mut render = "".to_string();
                for f in list {
                    render.push_str(&self.render(f)?);
                }
                Ok(render)
            }
            Filling::Template(template_hash) => {
                let template_label: &Filling = template_hash
                    .get(self.label)
                    .ok_or("Expected name label in template hash")?;

                // template_name must contain a string, it cannot be a template hash or
                // a vec of template hash.
                if let Filling::Text(name) = template_label {
                    let template_index = self.index(name)?;
                    let mut rendered = String::from(&template_index.contents);

                    // Iterate through all variables in reverse. We do this because we
                    // don't want to mess up all the indexed positions.
                    for var in template_index.variables.iter().rev() {
                        let mut render = "".to_string();
                        if let Some(value) = template_hash.get(&var.name) {
                            render.push_str(&self.render(value)?);
                        }
                        rendered.replace_range(var.start_position..var.end_position, &render);
                    }

                    // Trim trailing without cloning `rendered'.
                    let len_withoutcrlf = rendered.trim_end().len();
                    rendered.truncate(len_withoutcrlf);

                    Ok(rendered)
                } else {
                    Err("Name label should be of type Filling::Text(_)".to_string())
                }
            }
        }
    }
}

// The below macros are adapted from the json-rust macros (https://docs.rs/json/latest/json/#macros)
#[macro_export]
macro_rules! filling_list {
    //[] => ($crate::JsonValue::new_array());
    [] => { "".to_string() };

    // Handles for token tree items
    [@ITEM($( $i:expr, )*) $item:tt, $( $cont:tt )+] => {
        $crate::filling_list!(
            @ITEM($( $i, )* $crate::filling_text!($item), )
                $( $cont )*
        )
    };
    (@ITEM($( $i:expr, )*) $item:tt,) => ({
        $crate::filling_list!(@END $( $i, )* $crate::filling_text!($item), )
    });
    (@ITEM($( $i:expr, )*) $item:tt) => ({
        $crate::filling_list!(@END $( $i, )* $crate::filling_text!($item), )
    });

    // Handles for expression items
    [@ITEM($( $i:expr, )*) $item:expr, $( $cont:tt )+] => {
        $crate::filling_list!(
            @ITEM($( $i, )* $crate::filling_text!($item), )
                $( $cont )*
        )
    };
    (@ITEM($( $i:expr, )*) $item:expr,) => ({
        $crate::filling_list!(@END $( $i, )* $crate::filling_text!($item), )
    });
    (@ITEM($( $i:expr, )*) $item:expr) => ({
        $crate::filling_list!(@END $( $i, )* $crate::filling_text!($item), )
    });

    // Construct the actual array
    (@END $( $i:expr, )*) => ({
        let size = 0 $( + {let _ = &$i; 1} )*;
        let mut vec: Vec<Filling> = Vec::with_capacity(size);

        $(
            vec.push($i);
        )*

            $crate::Filling::List( vec )
    });

    // Entry point to the macro
    ($( $cont:tt )+) => {
        $crate::filling_list!(@ITEM() $($cont)*)
    };
}

/// Helper crate for converting types into `Filling::Text`. It's used
/// internally by the `filling!` and `filling_list!` macros.

#[macro_export]
macro_rules! filling_text {
    //( null ) => { $crate::Null };
    ( null ) => { "".to_string() };
    ( [$( $token:tt )*] ) => {
        // 10
        $crate::filling_list![ $( $token )* ]
    };
    ( {$( $token:tt )*} ) => {
        $crate::filling!{ $( $token )* }
    };
    { $value:expr } => { $crate::Filling::Text($value.to_string()) };
}

/// Helper macro for creating instances of `Filling`.
#[macro_export]
macro_rules! filling {
    {} => { "".to_string() };

    // Handles for different types of keys
    (@ENTRY($( $k:expr => $v:expr, )*) $key:ident: $( $cont:tt )*) => {
        $crate::filling!(@ENTRY($( $k => $v, )*) stringify!($key) => $($cont)*)
    };
    (@ENTRY($( $k:expr => $v:expr, )*) $key:literal: $( $cont:tt )*) => {
        $crate::filling!(@ENTRY($( $k => $v, )*) $key => $($cont)*)
    };
    (@ENTRY($( $k:expr => $v:expr, )*) [$key:expr]: $( $cont:tt )*) => {
        $crate::filling!(@ENTRY($( $k => $v, )*) $key => $($cont)*)
    };

    // Handles for token tree values
    (@ENTRY($( $k:expr => $v:expr, )*) $key:expr => $value:tt, $( $cont:tt )+) => {
        $crate::filling!(
            @ENTRY($( $k => $v, )* $key => $crate::filling_text!($value), )
                $( $cont )*
        )
    };
    (@ENTRY($( $k:expr => $v:expr, )*) $key:expr => $value:tt,) => ({
        $crate::filling!(@END $( $k => $v, )* $key => $crate::filling_text!($value), )
    });
    (@ENTRY($( $k:expr => $v:expr, )*) $key:expr => $value:tt) => ({
        $crate::filling!(@END $( $k => $v, )* $key => $crate::filling_text!($value), )
    });

    // Handles for expression values
    (@ENTRY($( $k:expr => $v:expr, )*) $key:expr => $value:expr, $( $cont:tt )+) => {
        $crate::filling!(
            @ENTRY($( $k => $v, )* $key => $crate::filling_text!($value), )
                $( $cont )*
        )
    };
    (@ENTRY($( $k:expr => $v:expr, )*) $key:expr => $value:expr,) => ({
        $crate::filling!(@END $( $k => $v, )* $key => $crate::filling_text!($value), )
    });

    (@ENTRY($( $k:expr => $v:expr, )*) $key:expr => $value:expr) => ({
        $crate::filling!(@END $( $k => $v, )* $key => $crate::filling_text!($value), )
    });

    // Construct the actual object
    (@END $( $k:expr => $v:expr, )*) => ({
        let mut params : HashMap<String, Filling> = Default::default();
        $(
            params.insert($k.to_string(), $v);
        )*
            let template = $crate::Filling::Template( params );
        template
    });

    // Entry point to the macro
    ($key:tt: $( $cont:tt )+) => {
        $crate::filling!(@ENTRY() $key: $($cont)*)
    };

    // Legacy macro
    ($( $k:expr => $v:expr, )*) => {
        $crate::filling!(@END $( $k => $crate::filling_text!($v), )*)
    };
    ($( $k:expr => $v:expr ),*) => {
        $crate::filling!(@END $( $k => $crate::filling_text!($v), )*)
    };
}

#[cfg(test)]
mod tests {
    use crate::TemplateNest;
    use crate::{filling, filling_list, Filling};
    use std::collections::HashMap;

    #[test]
    fn initialize() -> Result<(), String> {
        TemplateNest::new("templates")?;
        Ok(())
    }

    #[test]
    fn render_simple_page() -> Result<(), String> {
        let nest = TemplateNest::new("templates")?;
        let simple_page = filling!(
            "TEMPLATE": "00-simple-page",
            "variable": "Simple Variable",
            "simple_component":  {
                "TEMPLATE":"01-simple-component",
                "variable": "Simple Variable in Simple Component"
            }
        );
        let simple_page_output = filling!(
            "TEMPLATE": "output/01-simple-page",
        );
        assert_eq!(
            nest.render(&simple_page)?,
            nest.render(&simple_page_output)?,
        );
        Ok(())
    }

    #[test]
    fn render_incomplete_page() -> Result<(), String> {
        let nest = TemplateNest::new("templates")?;
        let incomplete_page = filling!(
            "TEMPLATE": "00-simple-page",
            "variable": "Simple Variable",
            "simple_component":  {
                "TEMPLATE":"01-simple-component",
            }
        );
        let incomplete_page_output = filling!(
            "TEMPLATE": "output/03-incomplete-page",
        );
        assert_eq!(
            nest.render(&incomplete_page)?,
            nest.render(&incomplete_page_output)?
        );
        Ok(())
    }

    #[test]
    fn render_complex_page() -> Result<(), String> {
        let nest = TemplateNest::new("templates")?;
        let complex_page = filling!(
            "TEMPLATE": "10-complex-page",
            "title": "Complex Page",
            "pre_body": {
                "TEMPLATE": "18-styles",
            },
            "navigation": {
                "TEMPLATE": "11-navigation",
                "banner": {
                    "TEMPLATE": "12-navigation-banner",
                },
                "items": [
                    { "TEMPLATE": "13-navigation-item-00-services" },
                    { "TEMPLATE": "13-navigation-item-01-resources" },
                ]
            },
            "hero_section": {
                "TEMPLATE": "14-hero-section",
            },
            "main_content": [
                { "TEMPLATE": "15-isdc-card", },
                {
                    "TEMPLATE": "16-vb-brand-cards",
                    "cards": [
                        {
                            "TEMPLATE": "17-vb-brand-card-00",
                            "parent_classes": "p-card brand-card col-4",
                        },
                        {
                            "TEMPLATE": "17-vb-brand-card-01",
                            "parent_classes": "p-card brand-card col-4",
                        },
                        {
                            "TEMPLATE": "17-vb-brand-card-02",
                            "parent_classes": "p-card brand-card col-4",
                        },
                    ]
                }
            ],
            "post_footer": {
                "TEMPLATE": "19-scripts"
            }
        );
        let complex_page_output = filling!(
            "TEMPLATE": "output/02-complex-page",
        );
        assert_eq!(
            nest.render(&complex_page)?,
            nest.render(&complex_page_output)?
        );

        Ok(())
    }

    #[test]
    fn render_array_of_template_hash() -> Result<(), String> {
        let nest = TemplateNest::new("templates")?;
        let page = filling_list!([
            {
                "TEMPLATE": "01-simple-component",
                "variable": "This is a variable",
            }, {
                "TEMPLATE": "01-simple-component",
                "variable": "This is another variable",
            }
        ]);
        let page_output = filling!(
            "TEMPLATE": "output/13-render-with-array-of-template-hash",
        );
        assert_eq!(nest.render(&page)?, nest.render(&page_output)?);

        Ok(())
    }
}