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
/*!
# beard

In opposition to [mustache]. Here the goal instead of mustache is
to leverage as much as possible rust's type system to detect error
case and therefor to make the rendering deterministic. If you are
looking for something that is going to be portable outside of rust
you should checkout [mustache].

[`beard`] is a macro that will generate the necessary rust code
to serialise the given _template_. You can achieve the same thing
by writing the code yourself (calling [std::io::Write] appropriate
methods). [`beard`] is simply an help to do that and to make it
easier to maintain the templates.

# Example

```
use beard::beard;
# use std::io::Write as _;
#
# fn render() -> Result<String, std::io::Error> {
    let name = "Arthur";
    let list = ["Bubble Bath", "Unicorn Crunchy Oat"];

#    let mut output = Vec::new();
    beard! {
        output,
        "Hi " { name } "\n"
        "\n"
        "Confirmation order about the following items:\n"
        for item in ( list ) {
            " * " { item } "\n"
        }
        "\n"
        "Your order will be ship to you once everything is ready.\n"
    };
#    Ok(String::from_utf8(output).unwrap())
# }
# let output = render().unwrap();
```

The Example below will generate a string in the `output`:

```text
Hi Arthur

Confirmation order about the following items:
 * Bubble Bath
 * Unicorn Crunch Oat

Your order will be ship to you once everything is ready.
```

[`beard`]: ./macro.beard.html
[mustache]: https://mustache.github.io/mustache.5.html
*/

/// macro to call to generate the function stream of generating
/// formatted output.
///
/// The difference here with [`std::fmt::format`] is that instead
/// generating a string based on some formatting parameters
/// the [`beard`] macro generates a string based on the declarative
/// flow.
#[macro_export]
macro_rules! beard {
    ($($any:tt)*) => {
        $crate::beard_internal!($($any)*);
    };
}

/// use this internal macro to hide the details of the macro away
///
/// this is not really useful for the user documentation anyway.
#[macro_export]
#[doc(hidden)]
macro_rules! beard_internal {
    ($output:ident, ) => {
    };

    ($output:ident, | | $statement:block $($any:tt)*) => {
        {
            $statement
        }
        $crate::beard_internal!($output, $($any)*);
    };
    ($output:ident, || $statement:block $($any:tt)*) => {
        {
            $statement
        }
        $crate::beard_internal!($output, $($any)*);
    };


    ($output:ident, $text:literal $($any:tt)*) => {
        $output.write_all($text.as_bytes())?;
        $crate::beard_internal!($output, $($any)*);
    };
    ($output:ident, [ $statement:block ] $($any:tt)*) => {
        $output.write_all(
             $statement.as_ref()
        )?;
        $crate::beard_internal!($output, $($any)*);
    };
    ($output:ident, $statement:block $($any:tt)*) => {
        $output.write_all(
             $statement.to_string().as_bytes()
        )?;
        $crate::beard_internal!($output, $($any)*);
    };

    ($output:ident, if ( $condition:expr ) { $($statement:tt)+ } else { $($alternative:tt)+ } $($any:tt)*) => {
        if $condition {
            $crate::beard_internal!($output, $($statement)+);
        } else {
            $crate::beard_internal!($output, $($alternative)+);
        }
        $crate::beard_internal!($output, $($any)*);
    };
    ($output:ident, if let $condition:pat = ( $value:expr ) { $($statement:tt)+ } $($any:tt)*) => {
        if let $condition = $value {
            $crate::beard_internal!($output, $($statement)+);
        }
        $crate::beard_internal!($output, $($any)*);
    };
    ($output:ident, if ( $condition:expr ) { $($statement:tt)+ } $($any:tt)*) => {
        if $condition {
            $crate::beard_internal!($output, $($statement)+);
        }
        $crate::beard_internal!($output, $($any)*);
    };

    ($output:ident, for $value:pat in ($into_iter:expr) { $($statement:tt)+ } $($any:tt)*) => {
        for $value in $into_iter.into_iter() {
            #![allow(clippy::into_iter_on_ref, array_into_iter)]
            $crate::beard_internal!($output, $($statement)+);
        }
        $crate::beard_internal!($output, $($any)*);
    };
}

#[test]
fn test() {
    use std::io::Write as _;

    const EXPECTED: &str = r##"Variables can be formatted as follow: value.
Statement works too: 3 (so you can do special formatting if you want).
The length of the stuff is not null value
print thing: one
print thing: two
"##;

    fn render() -> Result<String, std::io::Error> {
        let value = "value";
        let stuff = ["one", "two"];
        let optionals = [Some(1), None];

        let mut output = Vec::new();
        beard! {
            output,
            "Variables can be formatted as follow: " { value } ".\n"
            "Statement works too: " { 1 + 2} " (so you can do special formatting if you want).\n"
            if (value == "something") {
                "This test is not rendered" { value }
            }

            " as bytes directly: " [ { value.as_bytes() } ] "\n"

            if (!stuff.is_empty()) {
                "The length of the stuff is not null " { value } "\n"
            } else {
                "oops\n"
            }


            for optional in ( optionals ) {
                if let Some(value) = ( optional ) {
                    "Optional value set " { value } "\n"
                }
                if let None = (optional) {
                    "Optional value not set\n"
                }
            }

            for (_index, thing) in (stuff.iter().enumerate()) {

                "print thing: " { thing } "\n"
            }

            | | { output.write_all(b"something custom")?; }
        };
        Ok(String::from_utf8(output).unwrap())
    }

    let message = render().unwrap();

    assert_eq!(EXPECTED, message);
}