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
/// This macro performs a compile-time check to validate that all variants of an enum
/// are as provided in the macro invocation.
///
/// # Example
///
/// ```rust
/// use assert_enum_variants::assert_enum_variants;
///
/// #[allow(dead_code)]
/// pub enum MyEnum {
/// A,
/// B(u32),
/// C {
/// a: String,
/// b: u32,
/// },
/// }
///
/// // This will compile successfully
/// // because all variants of `MyEnum` are accounted for.
/// assert_enum_variants!(MyEnum, { A, B, C });
/// ```
///
/// It will fail to compile if any of the variants are missing or if there are any
/// extra variants.
///
/// # Example of faliure due to missing variants
///
/// ```rust,compile_fail
/// use assert_enum_variants::assert_enum_variants;
///
/// #[allow(dead_code)]
/// pub enum MyEnum {
/// A,
/// B(u32),
/// C {
/// a: String,
/// b: u32,
/// },
/// }
///
/// // This will fail to compile
/// // because the `C` variant is missing.
/// assert_enum_variants!(MyEnum, { A, B });
///```
///
/// # Example of failure due to extra variants
///
/// ```rust,compile_fail
/// use assert_enum_variants::assert_enum_variants;
///
/// #[allow(dead_code)]
/// pub enum MyEnum {
/// A,
/// B(u32),
/// C {
/// a: String,
/// b: u32,
/// },
/// }
///
/// // This will fail to compile
/// // because the `D` variant is not present on `MyEnum`.
/// assert_enum_variants!(MyEnum, { A, B, C, D });
/// ```
///
/// # Reasons for using this macro
///
/// Let's say you're writing some code that needs to handle all variants of an enum
/// but there could be a situation that none of the variants fits.
///
/// ```rust
/// enum ResumeFileFormat {
/// Pdf,
/// Docx,
/// Doc,
/// }
///
/// // ...
///
/// impl ResumeFileFormat {
/// fn from_extension(ext: &str) -> Option<Self> {
/// use ResumeFileFormat::{Pdf, Docx, Doc};
///
/// let file_format: ResumeFileFormat = match ext {
/// "pdf" => Pdf,
/// "docx" => Docx,
/// "doc" => Doc,
/// _ => return None,
/// };
///
/// Some(file_format)
/// }
/// }
/// ```
///
/// Notice that due to a wildcard pattern, the compiler will not warn you if you
/// add a new variant to the enum and forget to modify the `from_extension`.
///
/// That is, unless you use the [`assert_enum_variants!`] macro.
///
/// The following code will fail to compile if you add a new variant to the enum
/// and forget to modify the `from_extension` function.
///
/// ```rust,compile_fail
/// use assert_enum_variants::assert_enum_variants;
///
/// enum ResumeFileFormat {
/// Pdf,
/// Docx,
/// Doc,
/// Json,
/// }
///
/// // ...
///
/// impl ResumeFileFormat {
/// fn from_extension(ext: &str) -> Option<Self> {
/// use ResumeFileFormat::{Pdf, Docx, Doc};
///
/// // This will fail to compile because the `Json` variant is missing.
/// assert_enum_variants!(ResumeFileFormat, { Pdf, Docx, Doc });
///
/// let file_format: ResumeFileFormat = match ext {
/// "pdf" => Pdf,
/// "docx" => Docx,
/// "doc" => Doc,
/// _ => return None,
/// };
///
/// Some(file_format)
/// }
/// }
/// ```