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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*!
# Introduction

This is a simple template tool that works with variable names and
[`HashMap`] of [`String`]. The [`Template`] can be parsed from [`str`]
and then you can render it using the variables in [`HashMap`] and any
shell commands running through [`Exec`].

# Features
- Parse the template from a `str` that's easy to write,
- Support for alternatives in case some variables are not present,
  Use `?` to separate the alternatives, uses whichever it can find first. If `?` is at the end, leaves it blank instead of erroring out.
- Support for literal strings inside the alternative options,
  You can use a literal string `"string"` enclosed in `"` as an alternative if you want to put something instead of blank at the end.
- Support for the date time format using `chrono`,
  You can use any format starting with `%` inside the variable placeholder `{}` to use a date time format supported by chrono.
- Support for any arbitrary commands, etc.
  You can keep any command inside `$(` and `)` to run it and use the result in the template. You can use other format elements inside it.

# Usages
Simple variables:
```rust
# use std::error::Error;
# use std::collections::HashMap;
# use std::path::PathBuf;
# use string_template_plus::{Render, RenderOptions, parse_template};
#
# fn main() -> Result<(), Box<dyn Error>> {
let templ = parse_template("hello {name}").unwrap();
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert("name".into(), "world".into());
let rendered = templ
.render(&RenderOptions {
variables: vars,
..Default::default()
            })
            .unwrap();
assert_eq!(rendered, "hello world");
# Ok(())
# }
```

Safe replace, blank if not present, or literal string if not present:
```rust
# use std::error::Error;
# use std::collections::HashMap;
# use std::path::PathBuf;
# use string_template_plus::{Render, RenderOptions, parse_template};
#
# fn main() -> Result<(), Box<dyn Error>> {
let templ = parse_template("hello {name?} {lastname?\"User\"}").unwrap();
let vars: HashMap<String, String> = HashMap::new();
let rendered = templ
.render(&RenderOptions {
variables: vars,
..Default::default()
            })
            .unwrap();
assert_eq!(rendered, "hello  User");
# Ok(())
# }
```

Alternate, whichever variable it finds first will be replaced:
```rust
# use std::error::Error;
# use std::collections::HashMap;
# use std::path::PathBuf;
# use string_template_plus::{Render, RenderOptions, parse_template};
#
# fn main() -> Result<(), Box<dyn Error>> {
let templ = parse_template("hello {nickname?name}").unwrap();
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert("name".into(), "world".into());
let rendered = templ
.render(&RenderOptions {
variables: vars,
..Default::default()
            })
            .unwrap();
        assert_eq!(rendered, "hello world");
# Ok(())
# }
```

Custom Commands:
```rust
# use std::error::Error;
# use std::collections::HashMap;
# use std::path::PathBuf;
# use string_template_plus::{Render, RenderOptions, parse_template};
#
# fn main() -> Result<(), Box<dyn Error>> {
let templ = parse_template("L=$(printf \"%.2f\" {length})").unwrap();
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert("length".into(), "12.342323".into());
let rendered = templ
.render(&RenderOptions {
wd: PathBuf::from("."),
variables: vars,
shell_commands: true,
            })
            .unwrap();
        assert_eq!(rendered, "L=12.34");
# Ok(())
# }
```

You can turn off Custom Commands for safety:
```rust
# use std::error::Error;
# use std::collections::HashMap;
# use std::path::PathBuf;
# use string_template_plus::{Render, RenderOptions, parse_template};
#
# fn main() -> Result<(), Box<dyn Error>> {
let templ = parse_template("L=$(printf \"%.2f\" {length})").unwrap();
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert("length".into(), "12.342323".into());
let rendered = templ
.render(&RenderOptions {
wd: PathBuf::from("."),
variables: vars,
shell_commands: false,
            })
            .unwrap();
        assert_eq!(rendered, "L=$(printf \"%.2f\" 12.342323)");
# Ok(())
# }
```

Date Time:
```rust
# use std::error::Error;
# use std::collections::HashMap;
# use std::path::PathBuf;
# use chrono::Local;
# use string_template_plus::{Render, RenderOptions, parse_template};
#
# fn main() -> Result<(), Box<dyn Error>> {
let templ = parse_template("hello {name} at {%Y-%m-%d}").unwrap();
let timefmt = Local::now().format("%Y-%m-%d");
let output = format!("hello world at {}", timefmt);
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert("name".into(), "world".into());
let rendered = templ
.render(&RenderOptions {
wd: PathBuf::from("."),
variables: vars,
shell_commands: false,
            })
            .unwrap();
        assert_eq!(rendered, output);
# Ok(())
# }
```

# Limitations
- You cannot use positional arguments in this template system, only named ones. `{}` will be replaced with empty string. Although you can use `"0"`, `"1"`, etc as variable names in the template and the render options variables.
- I haven't tested variety of names, although they should work try to keep the names identifier friendly.
- Currently doesn't have format specifiers, for now you can use the command options with `printf` bash command to format things the way you want.
Like a template `this is $(printf "%.2f" {weight}) kg.` should be rendered with the correct float formatting.
*/
use anyhow::{Context, Error};
use chrono::Local;
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::HashMap;
use std::io::Read;
use std::path::PathBuf;
use subprocess::Exec;

/// Character to separate the variables. If the first variable is not present it'll use the one behind it and so on. Keep it at the end, if you want a empty string instead of error on missing variable.
pub static OPTIONAL_RENDER_CHAR: char = '?';
/// Character that should be in the beginning of the variable to determine it as datetime format.
pub static TIME_FORMAT_CHAR: char = '%';
/// Quote characters to use to make a value literal instead of a variable. In combination with [`OPTIONAL_RENDER_CHAR`] it can be used as a default value when variable(s) is/are not present.
pub static LITERAL_VALUE_QUOTE_CHAR: char = '"';
/// Characters that should be replaced as themselves if presented as a variable
static LITERAL_REPLACEMENTS: [&str; 3] = [
    "",  // to replace {} as empty string.
    "{", // to replace {{} as {
    "}", // to replace {}} as }
];

lazy_static! {
    /// Regex to capture the variable from the template, anything inside `{}`
    pub static ref VARIABLE_REGEX: Regex = Regex::new(r"\{(.*?)\}").unwrap();
    /// Regex to capture the Shell Command part in the template
    pub static ref SHELL_COMMAND_REGEX: Regex = Regex::new(r"[$]\((.*?)\)").unwrap();
}

/// Runs a command and returns the output of the command or the error
fn cmd_output(cmd: &str, wd: &PathBuf) -> Result<String, Error> {
    let mut out: String = String::new();
    Exec::shell(cmd)
        .cwd(wd)
        .stream_stdout()?
        .read_to_string(&mut out)?;
    Ok(out)
}

/// Parts that make up a [`Template`]. You can have literal strings, variables, time date format, command, or optional format with [`OPTIONAL_RENDER_CHAR`].
///
/// [`TemplatePart::Lit`] = Literal Strings like `"hi "` in `"hi {name}"`
/// [`TemplatePart::Var`] = Variable part like `"name"` in `"hi {name}"`
/// [`TemplatePart::Time`] = Date time format like `"%F"` in `"Today: {%F}"`
/// [`TemplatePart::Cmd`] = Command like `"echo world"` in `"hello $(echo world)"`
/// [`TemplatePart::Any`] = Optional format like `"name?age"` in `"hello {name?age}"`
///
/// [`TemplatePart::Cmd`] and [`TemplatePart::Any`] can in turn contain other [`TemplatePart`] inside them. Haven't tested on nesting complex ones within each other though.
#[derive(Debug, Clone)]
pub enum TemplatePart {
    /// Literal string, keep them as they are
    Lit(String),
    /// Variables, use the variable's value in the rendered String
    Var(String),
    /// DateTime format, use [`chrono::Local`] in the given format
    Time(String),
    /// Shell Command, use the output of command in the rendered String
    Cmd(Vec<TemplatePart>),
    /// Multiple variables or [`TemplatePart`]s, use the first one that succeeds
    Any(Vec<TemplatePart>),
}

/// Main Template that get's passed around, consists of `[Vec`] of [`TemplatePart`]
///
/// ```rust
/// # use std::error::Error;
/// # use std::collections::HashMap;
/// # use std::path::PathBuf;
/// # use string_template_plus::{Render, RenderOptions, parse_template};
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
///     let templ = parse_template("hello {nickname?name}. You're $(printf \"%.1f\" {weight})kg").unwrap();
///     let mut vars: HashMap<String, String> = HashMap::new();
///     vars.insert("name".into(), "John".into());
///     vars.insert("weight".into(), "132.3423".into());
///     let rendered = templ
///         .render(&RenderOptions {
///             wd: PathBuf::from("."),
///             variables: vars,
///             shell_commands: true,
///         })
///         .unwrap();
///     assert_eq!(rendered, "hello John. You're 132.3kg");
/// # Ok(())
/// }
pub type Template = Vec<TemplatePart>;

/// Provides the function to render the object with [`RenderOptions`] into [`String`]
pub trait Render {
    fn render(&self, op: &RenderOptions) -> Result<String, Error>;
}

/// Options for the [`Template`] to render into [`String`]
#[derive(Default, Debug, Clone)]
pub struct RenderOptions {
    /// Working Directory for the Shell Commands
    pub wd: PathBuf,
    /// Variables to use for the template
    pub variables: HashMap<String, String>,
    /// Run Shell Commands for the output or not
    pub shell_commands: bool,
}

impl RenderOptions {
    pub fn render(&self, templ: &Template) -> Result<String, Error> {
        templ.render(self)
    }

    /// Makes a [`RenderIter<'a>`] that can generate incremented strings from the given [`Template`] and the [`RenderOptions`]. The Iterator will have `-N` appended where N is the number representing the number of instance.
    ///
    /// ```rust
    /// # use std::error::Error;
    /// # use std::collections::HashMap;
    /// # use string_template_plus::{Render, RenderOptions, parse_template};
    /// #
    /// # fn main() -> Result<(), Box<dyn Error>> {
    ///     let templ = parse_template("hello {name}").unwrap();
    ///     let mut vars: HashMap<String, String> = HashMap::new();
    ///     vars.insert("name".into(), "world".into());
    ///     let options = RenderOptions {
    ///         variables: vars,
    ///         ..Default::default()
    ///     };
    ///     let mut names = options.render_iter(&templ);
    ///     assert_eq!("hello world-1", names.next().unwrap());
    ///     assert_eq!("hello world-2", names.next().unwrap());
    ///     assert_eq!("hello world-3", names.next().unwrap());
    /// # Ok(())
    /// # }
    pub fn render_iter<'a>(&'a self, templ: &'a Template) -> RenderIter<'a> {
        RenderIter {
            template: templ,
            options: self,
            count: 0,
        }
    }
}

/// Render option with [`Iterator`] support. You can use this to get
/// incremented render results. It'll add `-N` to the render
/// [`Template`] where `N` is the count (1,2,3...). It can be useful
/// to make files with a given template.
///
/// ```rust
/// # use std::error::Error;
/// # use std::collections::HashMap;
/// # use string_template_plus::{Render, RenderOptions, RenderIter, parse_template};
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
///     let templ = parse_template("hello {name}").unwrap();
///     let mut vars: HashMap<String, String> = HashMap::new();
///     vars.insert("name".into(), "world".into());
///     let options = RenderOptions {
///         variables: vars,
///         ..Default::default()
///     };
///     let mut names = RenderIter::new(&templ, &options);
///     assert_eq!("hello world-1", names.next().unwrap());
///     assert_eq!("hello world-2", names.next().unwrap());
///     assert_eq!("hello world-3", names.next().unwrap());
/// # Ok(())
/// # }
#[derive(Debug, Clone)]
pub struct RenderIter<'a> {
    template: &'a Template,
    options: &'a RenderOptions,
    count: usize,
}

impl<'a> RenderIter<'a> {
    /// Creates a new [`RenderIter<'a>`] object
    pub fn new(template: &'a Template, options: &'a RenderOptions) -> Self {
        Self {
            template: &template,
            options: &options,
            count: 0,
        }
    }
}

impl<'a> Iterator for RenderIter<'a> {
    type Item = String;
    fn next(&mut self) -> Option<String> {
        self.template.render(&self.options).ok().map(|t| {
            self.count += 1;
            format!("{}-{}", t, self.count)
        })
    }
}

impl Render for TemplatePart {
    fn render(&self, op: &RenderOptions) -> Result<String, Error> {
        match self {
            TemplatePart::Lit(l) => Ok(l.to_string()),
            TemplatePart::Var(v) => op
                .variables
                .get(v)
                .map(|s| s.to_string())
                .context("No such variable in the RenderOptions"),
            TemplatePart::Time(t) => Ok(Local::now().format(t).to_string()),
            TemplatePart::Cmd(c) => {
                let cmd = c.render(op)?;
                if op.shell_commands {
                    cmd_output(&cmd, &op.wd)
                } else {
                    Ok(format!("$({cmd})"))
                }
            }
            TemplatePart::Any(a) => a
                .iter()
                .filter_map(|p| p.render(op).ok())
                .next()
                .context("None of the alternatives given were found"),
        }
    }
}

impl Render for Template {
    fn render(&self, op: &RenderOptions) -> Result<String, Error> {
        self.iter()
            .map(|p| p.render(op))
            .collect::<Result<Vec<String>, Error>>()
            .map(|v| v.join(""))
    }
}

/// Parse a single [`TemplatePart`] from `str`, It can only parse [`TemplatePart::Lit`], [`TemplatePart::Time`], and [`TemplatePart::Var`].
fn parse_single_part(part: &str) -> TemplatePart {
    if LITERAL_REPLACEMENTS.contains(&part) {
        TemplatePart::Lit(part.to_string())
    } else if part.starts_with(LITERAL_VALUE_QUOTE_CHAR) && part.ends_with(LITERAL_VALUE_QUOTE_CHAR)
    {
        TemplatePart::Lit(part[1..(part.len() - 1)].to_string())
    } else if part.starts_with(TIME_FORMAT_CHAR) {
        TemplatePart::Time(part.to_string())
    } else {
        TemplatePart::Var(part.to_string())
    }
}

/// Parse variables in a `str` into [`Template`]. It can parse all types except [`TemplatePart::Cmd`]
fn parse_variables(templ: &str) -> Template {
    let mut last_match = 0usize;
    let mut template_parts = Vec::new();
    for cap in VARIABLE_REGEX.captures_iter(templ) {
        let m = cap.get(0).unwrap();
        template_parts.push(TemplatePart::Lit(templ[last_match..m.start()].to_string()));

        let cg = cap.get(1).unwrap();
        let cap_slice = &templ[cg.start()..cg.end()];
        if cap_slice.contains(OPTIONAL_RENDER_CHAR) {
            let parts = cap_slice
                .split(OPTIONAL_RENDER_CHAR)
                .map(|s| s.trim())
                .map(parse_single_part)
                .collect();

            template_parts.push(TemplatePart::Any(parts));
        } else {
            template_parts.push(parse_single_part(cap_slice));
        }
        last_match = m.end();
    }
    if !templ[last_match..].is_empty() {
        template_parts.push(TemplatePart::Lit(templ[last_match..].to_string()));
    }

    template_parts
}

/// Parses the template from string and makes a [`Template`]. Which you can render later./// Main Template that get's passed around, consists of `[Vec`] of [`TemplatePart`]
///
/// ```rust
/// # use std::error::Error;
/// # use std::collections::HashMap;
/// # use std::path::PathBuf;
/// # use string_template_plus::{Render, RenderOptions, parse_template};
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
///     let templ = parse_template("hello {nickname?name?}. You're $(printf \"%.1f\" {weight})kg").unwrap();
///     let parts = concat!("[Lit(\"hello \"), ",
///                         "Any([Var(\"nickname\"), Var(\"name\"), Lit(\"\")]), ",
///                         "Lit(\". You're \"), ",
///                         "Cmd([Lit(\"printf \\\"%.1f\\\" \"), Var(\"weight\")]), ",
///                         "Lit(\"kg\")]");
///     assert_eq!(parts, format!("{:?}", templ));
/// # Ok(())
/// }
pub fn parse_template(templ_str: &str) -> Result<Template, Error> {
    let mut last_match = 0usize;
    let mut template_parts = Vec::new();
    for cmd in SHELL_COMMAND_REGEX.captures_iter(templ_str) {
        let m = cmd.get(0).unwrap();
        let cmd = cmd.get(1).unwrap();
        let mut pts = parse_variables(&templ_str[last_match..m.start()]);
        template_parts.append(&mut pts);
        template_parts.push(TemplatePart::Cmd(parse_variables(
            &templ_str[cmd.start()..cmd.end()],
        )));
        last_match = m.end();
    }
    let mut pts = parse_variables(&templ_str[last_match..]);
    template_parts.append(&mut pts);
    Ok(template_parts)
}

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

    #[test]
    fn test_lit() {
        let templ = parse_template("hello name").unwrap();
        let mut vars: HashMap<String, String> = HashMap::new();
        vars.insert("name".into(), "world".into());
        let rendered = templ
            .render(&RenderOptions {
                variables: vars,
                ..Default::default()
            })
            .unwrap();
        assert_eq!(rendered, "hello name");
    }

    #[test]
    fn test_vars() {
        let templ = parse_template("hello {name}").unwrap();
        let mut vars: HashMap<String, String> = HashMap::new();
        vars.insert("name".into(), "world".into());
        let rendered = templ
            .render(&RenderOptions {
                variables: vars,
                ..Default::default()
            })
            .unwrap();
        assert_eq!(rendered, "hello world");
    }

    #[test]
    #[should_panic]
    fn test_novars() {
        let templ = parse_template("hello {name}").unwrap();
        let vars: HashMap<String, String> = HashMap::new();
        templ
            .render(&RenderOptions {
                variables: vars,
                ..Default::default()
            })
            .unwrap();
    }

    #[test]
    fn test_novars_opt() {
        let templ = parse_template("hello {name?}").unwrap();
        let vars: HashMap<String, String> = HashMap::new();
        let rendered = templ
            .render(&RenderOptions {
                variables: vars,
                ..Default::default()
            })
            .unwrap();
        assert_eq!(rendered, "hello ");
    }

    #[test]
    fn test_optional() {
        let templ = parse_template("hello {age?name}").unwrap();
        let mut vars: HashMap<String, String> = HashMap::new();
        vars.insert("name".into(), "world".into());
        let rendered = templ
            .render(&RenderOptions {
                variables: vars,
                ..Default::default()
            })
            .unwrap();
        assert_eq!(rendered, "hello world");
    }

    #[test]
    fn test_special_chars() {
        let templ = parse_template("$hello {}? {{}{}}%").unwrap();
        let rendered = templ.render(&RenderOptions::default()).unwrap();
        assert_eq!(rendered, "$hello ? {}%");
    }

    #[test]
    fn test_optional_lit() {
        let templ = parse_template("hello {age?\"20\"}").unwrap();
        let mut vars: HashMap<String, String> = HashMap::new();
        vars.insert("name".into(), "world".into());
        let rendered = templ
            .render(&RenderOptions {
                variables: vars,
                ..Default::default()
            })
            .unwrap();
        assert_eq!(rendered, "hello 20");
    }

    #[test]
    fn test_command() {
        let templ = parse_template("hello $(echo {name})").unwrap();
        let mut vars: HashMap<String, String> = HashMap::new();
        vars.insert("name".into(), "world".into());
        let rendered = templ
            .render(&RenderOptions {
                wd: PathBuf::from("."),
                variables: vars,
                shell_commands: true,
            })
            .unwrap();
        assert_eq!(rendered, "hello world\n");
    }

    #[test]
    fn test_time() {
        let templ = parse_template("hello {name} at {%Y-%m-%d}").unwrap();
        let timefmt = Local::now().format("%Y-%m-%d");
        let output = format!("hello world at {}", timefmt);
        let mut vars: HashMap<String, String> = HashMap::new();
        vars.insert("name".into(), "world".into());
        let rendered = templ
            .render(&RenderOptions {
                wd: PathBuf::from("."),
                variables: vars,
                shell_commands: false,
            })
            .unwrap();
        assert_eq!(rendered, output);
    }

    #[test]
    fn test_var_or_time() {
        let templ = parse_template("hello {name} at {age?%Y-%m-%d}").unwrap();
        let timefmt = Local::now().format("%Y-%m-%d");
        let output = format!("hello world at {}", timefmt);
        let mut vars: HashMap<String, String> = HashMap::new();
        vars.insert("name".into(), "world".into());
        let rendered = templ
            .render(&RenderOptions {
                wd: PathBuf::from("."),
                variables: vars,
                shell_commands: false,
            })
            .unwrap();
        assert_eq!(rendered, output);
    }

    #[test]
    fn test_render_iter() {
        let templ = parse_template("hello {name}").unwrap();
        let mut vars: HashMap<String, String> = HashMap::new();
        vars.insert("name".into(), "world".into());
        let options = RenderOptions {
            variables: vars,
            ..Default::default()
        };
        let mut names = options.render_iter(&templ);
        assert_eq!("hello world-1", names.next().unwrap());
        assert_eq!("hello world-2", names.next().unwrap());
        assert_eq!("hello world-3", names.next().unwrap());
    }
}