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
|
use askama::Template;
macro_rules! hello {
() => {
"world"
};
}
#[derive(Template)]
#[template(path = "rust-macros.html")]
struct RustMacrosTemplate {}
#[test]
fn macro_basic() {
let template = RustMacrosTemplate {};
assert_eq!("Hello, world!", template.render().unwrap());
}
mod foo {
macro_rules! hello2 {
() => {
"world"
};
}
pub(crate) use hello2;
}
#[derive(Template)]
#[template(path = "rust-macros-full-path.html")]
struct RustMacrosFullPathTemplate {}
#[test]
fn macro_full_path() {
let template = RustMacrosFullPathTemplate {};
assert_eq!("Hello, world!", template.render().unwrap());
}
macro_rules! call_a_or_b_on_tail {
((a: $a:expr, b: $b:expr, c: $c:expr), call a: $($tail:expr),*) => {
($a)($($tail),*)
};
((a: $a:expr, b: $b:expr, c: $c:expr), call b: $($tail:expr),*) => {
($b)($($tail),*)
};
((a: $a:expr, b: $b:expr, c: $c:expr), call c: $($tail:expr),*) => {
($c)($($tail),*)
};
}
fn year(y: u16, _: &str, _: u8) -> u16 {
y
}
fn month(_: u16, m: &str, _: u8) -> &str {
m
}
fn day(_: u16, _: &str, d: u8) -> u8 {
d
}
#[derive(Template)]
#[template(path = "rust-macro-args.html")]
struct RustMacrosArgTemplate {}
#[test]
fn macro_with_args() {
let template = RustMacrosArgTemplate {};
assert_eq!("2021\nJuly\n2", template.render().unwrap());
}
|