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
|
// arg_util.rs
// Copyright (C) 2019-2020 The Nitrocli Developers
// SPDX-License-Identifier: GPL-3.0-or-later
macro_rules! count {
($head:ident) => { 1 };
($head:ident, $($tail:ident),*) => {
1 + count!($($tail),*)
}
}
/// Translate an optional source into an optional destination.
macro_rules! tr {
($dst:tt, $src:tt) => {
$dst
};
($dst:tt) => {};
}
macro_rules! Command {
( $(#[$docs:meta])* $name:ident, [
$( $(#[$doc:meta])* $var:ident$(($inner:ty))? => $exec:expr, ) *
] ) => {
$(#[$docs])*
#[derive(Debug, PartialEq, structopt::StructOpt)]
pub enum $name {
$(
$(#[$doc])*
$var$(($inner))?,
)*
}
#[allow(unused_qualifications)]
impl $name {
pub fn execute(
self,
ctx: &mut crate::Context<'_>,
) -> anyhow::Result<()> {
match self {
$(
$name::$var$((tr!(args, $inner)))? => $exec(ctx $(,tr!(args, $inner))?),
)*
}
}
}
};
}
/// A macro for generating an enum with a set of simple (i.e., no
/// parameters) variants and their textual representations.
// TODO: Right now we hard code the derives we create. We may want to
// make this set configurable.
macro_rules! Enum {
( $(#[$docs:meta])* $name:ident, [ $( $var:ident => $str:expr, ) *] ) => {
$(#[$docs])*
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum $name {
$(
$var,
)*
}
enum_int! {$name, [
$( $var => $str, )*
]}
};
}
macro_rules! enum_int {
( $name:ident, [ $( $var:ident => $str:expr, ) *] ) => {
impl $name {
#[allow(unused)]
pub fn all(&self) -> [$name; count!($($var),*) ] {
$name::all_variants()
}
pub fn all_variants() -> [$name; count!($($var),*) ] {
[
$(
$name::$var,
)*
]
}
#[allow(unused)]
pub fn all_str() -> [&'static str; count!($($var),*)] {
[
$(
$str,
)*
]
}
}
impl ::std::convert::AsRef<str> for $name {
fn as_ref(&self) -> &'static str {
match *self {
$(
$name::$var => $str,
)*
}
}
}
impl ::std::fmt::Display for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "{}", self.as_ref())
}
}
impl ::std::str::FromStr for $name {
type Err = ::std::string::String;
fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> {
match s {
$(
$str => Ok($name::$var),
)*
_ => Err(
format!(
"expected one of {}",
$name::all_str().join(", "),
)
)
}
}
}
};
}
#[cfg(test)]
mod tests {
Enum! {Command, [
Var1 => "var1",
Var2 => "2",
Var3 => "crazy",
]}
#[test]
fn all_variants() {
assert_eq!(
Command::all_variants(),
[Command::Var1, Command::Var2, Command::Var3]
)
}
#[test]
fn text_representations() {
assert_eq!(Command::Var1.as_ref(), "var1");
assert_eq!(Command::Var2.as_ref(), "2");
assert_eq!(Command::Var3.as_ref(), "crazy");
}
}
|