markup-proc-macro 0.15.0

A blazing fast, type-safe template engine for Rust.
Documentation
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
<div align="center">

# markup.rs

A blazing fast, type-safe template engine for Rust.

[![Build](https://img.shields.io/github/actions/workflow/status/utkarshkukreti/markup.rs/build.yml?style=for-the-badge)](https://github.com/utkarshkukreti/markup.rs/actions/workflows/build.yml)
[![Version](https://img.shields.io/crates/v/markup?style=for-the-badge)](https://crates.io/crates/markup)
[![Documentation](https://img.shields.io/docsrs/markup?style=for-the-badge)](https://docs.rs/markup)
[![Downloads](https://img.shields.io/crates/d/markup?style=for-the-badge)](https://crates.io/crates/markup)
[![License](https://img.shields.io/crates/l/markup?style=for-the-badge)](https://crates.io/crates/markup)

</div>

`markup.rs` is a template engine for Rust powered by procedural macros which
parses the template at compile time and generates optimal Rust code to render
the template at run time. The templates may embed Rust code which is type
checked by the Rust compiler enabling full type-safety.

## Features

* Fully type-safe with inline highlighted errors when using editor extensions like [rust-analyzer]https://github.com/rust-analyzer/rust-analyzer.
* Less error-prone and terse syntax inspired by [Haml]https://haml.info/, [Slim]http://slim-lang.com/, and [Pug]https://pugjs.org.
* Zero unsafe code.
* Zero runtime dependencies.
* ⚡ Blazing fast. The fastest in [this]https://github.com/djc/template-benchmarks-rs benchmark among the ones which do not use unsafe code, the second fastest overall.

## Install

```toml
[dependencies]
markup = "0.15.0"
```

## Framework Integration

We have integration examples for the following web frameworks:

1. [Axum]examples/axum/src/main.rs
2. [Rocket]examples/rocket/src/main.rs

## Quick Example

```rust
markup::define! {
    Home<'a>(title: &'a str) {
        @markup::doctype()
        html {
            head {
                title { @title }
                style {
                    "body { background: #fafbfc; }"
                    "#main { padding: 2rem; }"
                }
            }
            body {
                @Header { title }
                #main {
                    p {
                        "This domain is for use in illustrative examples in documents. You may \
                        use this domain in literature without prior coordination or asking for \
                        permission."
                    }
                    p {
                        a[href = "https://www.iana.org/domains/example"] {
                            "More information..."
                        }
                    }
                }
                @Footer { year: 2020 }
            }
        }
    }

    Header<'a>(title: &'a str) {
        header {
            h1 { @title }
        }
    }

    Footer(year: u32) {
        footer {
            "(c) " @year
        }
    }
}

fn main() {
    println!(
        "{}",
        Home {
            title: "Example Domain"
        }
    )
}
```

### Output

```html
<!DOCTYPE html><html><head><title>Example Domain</title><style>body { background: #fafbfc; }#main { padding: 2rem; }</style></head><body><header><h1>Example Domain</h1></header><div id="main"><p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p><p><a href="https://www.iana.org/domains/example">More information...</a></p></div><footer>(c) 2020</footer></body></html>
```

### Output (manually prettified)

```html
<!doctype html>
<html>
  <head>
    <title>Example Domain</title>
    <style>
      body {
        background: #fafbfc;
      }
      #main {
        padding: 2rem;
      }
    </style>
  </head>
  <body>
    <header><h1>Example Domain</h1></header>
    <div id="main">
      <p>
        This domain is for use in illustrative examples in documents. You may
        use this domain in literature without prior coordination or asking for
        permission.
      </p>
      <p>
        <a href="https://www.iana.org/domains/example">More information...</a>
      </p>
    </div>
    <footer>(c) 2020</footer>
  </body>
</html>
```

## Syntax Reference (WIP)

### markup::define! and markup::new!

There are two ways to define templates: `markup::define!` and `markup::new!`.

`markup::define!` defines a template with named arguments. These templates cannot access variables from outer scope. The templates can have generic parameters. Under the hood, `markup::define!` compiles to a Rust struct that implements `markup::Render` and `std::fmt::Display` traits.

<table>
  <tr><th>Code</th></tr>
  <tr><td width="1000px">

  ```rust
  markup::define! {
      Hello<'a>(name: &'a str) {
          "Hello, " @name "!"
      }
      HelloGeneric<T: std::fmt::Display>(name: T) {
          "Hello, " @name.to_string() "!"
      }
  }

  // The template can now be printed directly or written to a stream:
  println!("{}", Hello { name: "World" });
  writeln!(&mut std::io::stdout(), "{}", HelloGeneric { name: "World 2" }).unwrap();

  // The template can also be rendered to a String:
  let string = Hello { name: "World 3" }.to_string();
  println!("{}", string);
  ```
  </td></tr>
  <tr><th>Output</th></tr>
  <tr><td width="1000px">

  ```html
  Hello, World!
  Hello, World 2!
  Hello, World 3!
  ```
  </td></tr>
</table>


`markup::new!` defines a template without any arguments. These can access variables from outer scope.

<table>
  <tr><th>Code</th></tr>
  <tr><td width="1000px">

  ```rust
  let name = "World";
  let template = markup::new! {
      "Hello, " @name "!"
  };

  // The template can now be printed directly or written to a stream:
  println!("{}", template);
  writeln!(&mut std::io::stdout(), "{}", template).unwrap();

  // The template can also be rendered to a String:
  let string = template.to_string();
  println!("{}", string);
  ```
  </td></tr>
  <tr><th>Output</th></tr>
  <tr><td width="1000px">

  ```html
  Hello, World!
  Hello, World!
  Hello, World!
  ```
  </td></tr>
</table>


### Expressions

Templates can have bare literal values, which are rendered as is. They can also have expressions (including function and macro calls) preceded by `@` sign. All strings are HTML-escaped unless they are wrapped in `markup::raw()`.

<table>
  <tr><th>Code</th></tr>
  <tr><td width="1000px">

  ```rust
  markup::define! {
      Expressions(a: i32, b: i32) {
          1 " + " 2 " = " @{1 + 2} '\n'
          @a " - " @b " = " @{a - b} '\n'
          @format!("{} * {} = {}", a, b, a * b) '\n'
          @a " ^ 4 = " @a.pow(4) '\n'

          // All output is escaped by default.
          "<>\n"
          // Escaping can be disabled using `markup::raw()`.
          @markup::raw("<div></div>")
      }
  }

  println!("{}", Expressions { a: 5, b: 3 });
  ```
  </td></tr>
  <tr><th>Output</th></tr>
  <tr><td width="1000px">

  ```html
  1 + 2 = 3
  5 - 3 = 2
  5 * 3 = 15
  5 ^ 4 = 625
  &lt;&gt;
  <div></div>
  ```
  </td></tr>
</table>


### Elements

Elements are defined using a CSS selector-like syntax. Elements can contain other nested elements in braces or be followed by a semicolon for self-closing elements.

<table>
  <tr><th>Code</th></tr>
  <tr><td width="1000px">

  ```rust
  markup::define! {
      Elements(name: &'static str) {
          // Just a div.
          div {}
          '\n'
          // Three nested elements.
          main {
              aside {
                  h3 { "Sidebar" }
              }
          }
          '\n'
          // Self-closing input element.
          input;
          '\n'
          // Element with a name containing dashes.
          $"my-custom-element" {}
          '\n'
          // Element with a dynamic name.
          ${name} {}
      }
  }

  println!("{}", Elements { name: "span" });
  ```
  </td></tr>
  <tr><th>Output</th></tr>
  <tr><td width="1000px">

  ```html
  <div></div>
  <main><aside><h3>Sidebar</h3></aside></main>
  <input>
  <my-custom-element></my-custom-element>
  <span></span>
  ```
  </td></tr>
</table>


### Attributes

Attributes are defined after the element name. `id` and `class` attributes can be defined using CSS selector-like syntax using `#` and `.`. Classes may be specified multiple times using this shorthand syntax. Other attributes are specified in square brackets.

<table>
  <tr><th>Code</th></tr>
  <tr><td width="1000px">

  ```rust
  markup::define! {
      Attributes(id: u32, category: String, data: std::collections::BTreeMap<String, String>) {
          // A div with an id and two classes.
          // Note: Starting with Rust 2021, there must be a space between the
          // element name and `#` due to reserved syntax (https://doc.rust-lang.org/edition-guide/rust-2021/reserving-syntax.html).
          div #foo.bar.baz {}
          '\n'
          // A div with a dynamically computed id and one static and one dynamic class.
          div #{format!("post-{}", id)}.post.{format!("category-{}", category)} {}
          '\n'

          // Boolean attributes are only rendered if true. Specifying no value is the same as `true`.
          input[checked = true];
          '\n'
          input[checked = false];
          '\n'
          input[checked];
          '\n'

          // `Option` attributes are rendered only if they're `Some`.
          input[type = Some("text"), minlength = None::<String>];
          '\n'

          // Attribute names can also be expressions wrapped in braces.
          div[{format!("{}{}", "data-", "post-id")} = id] {}
          '\n'

          // Multiple attributes can be added dynamically using the `..` syntax.
          div[..data.iter().map(|(k, v)| (("data-", k), v))] {}
      }
  }

  println!("{}", Attributes {
      id: 123,
      category: String::from("tutorial"),
      data: [
          (String::from("foo"), String::from("bar")),
          (String::from("baz"), String::from("quux"))
      ].iter().cloned().collect(),
  });
  ```
  </td></tr>
  <tr><th>Output</th></tr>
  <tr><td width="1000px">

  ```html
  <div id="foo" class="bar baz"></div>
  <div id="post-123" class="post category-tutorial"></div>
  <input checked>
  <input>
  <input checked>
  <input type="text">
  <div data-post-id="123"></div>
  <div data-baz="quux" data-foo="bar"></div>
  ```
  </td></tr>
</table>


### @if and @if let

`@if` and `@if let` works similar to Rust.

<table>
  <tr><th>Code</th></tr>
  <tr><td width="1000px">

  ```rust
  markup::define! {
      If(x: u32, y: Option<u32>) {
          @if *x == 1 {
              "x = 1\n"
          } else if *x == 2 {
              "x = 2\n"
          } else {
              "x is neither 1 nor 2\n"
          }

          @if let Some(y) = y {
              "y = " @y "\n"
          } else {
              "y is None\n"
          }
      }
  }

  println!("{}", If { x: 2, y: Some(2) });
  println!("{}", If { x: 3, y: None });
  ```
  </td></tr>
  <tr><th>Output</th></tr>
  <tr><td width="1000px">

  ```html
  x = 2
  y = 2

  x is neither 1 nor 2
  y is None
  ```
  </td></tr>
</table>


### @match

`@match` works similar to Rust, but the branches must be wrapped in braces.

<table>
  <tr><th>Code</th></tr>
  <tr><td width="1000px">

  ```rust
  markup::define! {
      Match(x: Option<u32>) {
          @match x {
              Some(1) | Some(2) => {
                  "x is 1 or 2"
              }
              Some(x) if *x == 3 => {
                  "x is 3"
              }
              None => {
                  "x is None"
              }
              _ => {
                  "x is something else"
              }
          }
      }
  }

  println!("{}", Match { x: None });
  println!("{}", Match { x: Some(2) });
  println!("{}", Match { x: Some(4) });
  ```
  </td></tr>
  <tr><th>Output</th></tr>
  <tr><td width="1000px">

  ```html
  x is None
  x is 1 or 2
  x is something else
  ```
  </td></tr>
</table>


### @for

`@for` works similar to Rust.

<table>
  <tr><th>Code</th></tr>
  <tr><td width="1000px">

  ```rust
  markup::define! {
      For<'a>(xs: &'a [u32]) {
          @for (i, x) in xs.iter().enumerate() {
              @i ": " @x "\n"
          }
      }
  }

  println!("{}", For { xs: &[1, 2, 4, 8] });
  ```
  </td></tr>
  <tr><th>Output</th></tr>
  <tr><td width="1000px">

  ```html
  0: 1
  1: 2
  2: 4
  3: 8
  ```
  </td></tr>
</table>


### Statements

Templates can have statements preceded by `@` sign. The most useful such
statement is `@let` to compute a value for later reuse. `@fn` can be used to
define a function. Also supported are `@struct`, `@mod`, `@impl`, `@const`,
`@static` and more.

<table>
  <tr><th>Code</th></tr>
  <tr><td width="1000px">

  ```rust
  markup::define! {
      Statement(x: i32) {
          @let double_x = x * 2;
          @double_x '\n'

          @fn triple(x: i32) -> i32 { x * 3 }
          @triple(*x)
      }
  }

  println!("{}", Statement { x: 2 });
  ```
  </td></tr>
  <tr><th>Output</th></tr>
  <tr><td width="1000px">

  ```html
  4
  6
  ```
  </td></tr>
</table>