summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 704b31041d8dbf7cc7eac5705aed544aecc06bd1 (plain)
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
//! Template struct for storing template strings and their parameters.
//!
//! The template supports two types of parameters:
//! - Simple parameters: key-value pairs used to replace simple placeholders (`<<<key>>>` format) in the template.
//! - Implementation parameters: for implementation blocks (`>>>>>>>>> block_name` and `@@@ >>> block_name` format),
//!   can contain multiple parameter sets, each corresponding to an implementation instance.
//!
//! # Examples
//! ```
//! use just_template::Template;
//!
//! let mut tmpl = Template::from("Hello, <<<name>>>!".to_string());
//! tmpl.insert_param("name".to_string(), "World".to_string());
//! assert_eq!(tmpl.to_string(), "Hello, World!");
//! ```
//!
//! Using the `tmpl_param!` macro makes it easier to add simple parameters:
//! ```
//! use just_template::{Template, tmpl_param};
//!
//! let mut tmpl = Template::from("<<<a>>> + <<<b>>> = <<<c>>>".to_string());
//! tmpl_param!(tmpl, a = 1, b = 2, c = 3);
//! assert_eq!(tmpl.to_string(), "1 + 2 = 3");
//! ```
//!
//! Using the `tmpl!` macro adds implementation block parameters:
//! ```
//! use just_template::{Template, tmpl};
//!
//! let mut tmpl = Template::from("
//! >>>>>>>>>> arms
//! @@@ >>> arms
//!     <<<crate_name>>> => Some(<<<crate_name>>>::exec(data, params).await),
//! @@@ <<<
//! ".trim().to_string());
//! tmpl!(tmpl += {
//!     arms {
//!         (crate_name = "my"),
//!         (crate_name = "you")
//!     }
//! });
//! // Output the expanded template
//! let expanded = tmpl.to_string();
//! assert_eq!(expanded, "
//!     my => Some(my::exec(data, params).await),
//!     you => Some(you::exec(data, params).await),
//! ".trim().to_string());
//! ```
mod template;
pub use template::*; // Re-export template to just_template

pub mod expand;
pub mod test;

#[macro_export]
macro_rules! tmpl_param {
    ($template:ident, $($key:ident = $value:expr),* $(,)?) => {{
        $(
            $template.insert_param(stringify!($key).to_string(), $value.to_string());
        )*
    }};
}

#[macro_export]
macro_rules! tmpl {
    ($template:ident += {
        $($name:ident {
            $(($($key:ident = $value:expr),* $(,)?)),*
            $(,)?
        }),*
    }) => {{
        $(
            let $name = $template.add_impl(stringify!($name).to_string());
            $(
                $name.push({
                    let mut params = std::collections::HashMap::new();
                    $(params.insert(stringify!($key).to_string(), $value.to_string());)*
                    params
                });
            )*
        )*
    }};
}