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
#[macro_export]
macro_rules! hook {
($hook_name:expr, $fn_name:ident) => {
$crate::Hook {
name: $hook_name.to_string(),
callback: $fn_name,
}
};
($hook_name:ident $fn_name:ident) => {
$crate::Hook {
name: stringify!($hook_name).to_string(),
callback: $fn_name,
}
};
}
#[macro_export]
macro_rules! log {
($message:expr) => {{
log!($message, Debug)
}};
($message:expr, $level:ident) => {{
$crate::log(
$message.to_string(),
Some($crate::LogLevel::$level),
);
}}
}
#[macro_export]
macro_rules! status_set {
($status_type:ident $message:expr) => {{
let _ = $crate::status_set(
$crate::Status {
status_type: $crate::StatusType::$status_type,
message: $message.to_string()
}
);
}}
}
#[cfg(test)]
mod tests {
#[allow(dead_code)]
mod status_set {
fn it_compiles_correctly() {
status_set!(Maintenance "Doing stuff");
}
}
#[allow(dead_code)]
mod log {
fn it_logs_default() {
log!("This is a test");
}
fn it_logs_specific_level() {
log!("test 2", Warn);
}
}
use super::super::Hook;
fn cb() -> Result<(), String> {
Ok(())
}
#[test]
fn it_makes_a_hook_correctly() {
let h1 = hook!(test cb);
let h2 = Hook {
name: "test".to_string(),
callback: cb,
};
assert_eq!(h1, h2);
}
#[test]
fn it_makes_a_complex_named_hook_correctly() {
let h1 = hook!("config-changed", cb);
let h2 = Hook {
name: "config-changed".to_string(),
callback: cb,
};
assert_eq!(h1, h2);
}
}