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
#![deny(missing_docs)]
#![warn(noop_method_call)]
#![deny(unreachable_pub)]
#![deny(clippy::await_holding_lock)]
#![deny(clippy::cargo_common_metadata)]
#![warn(clippy::clone_on_ref_ptr)]
#![warn(clippy::cognitive_complexity)]
#![deny(clippy::debug_assert_with_mut_call)]
#![deny(clippy::exhaustive_enums)]
#![deny(clippy::exhaustive_structs)]
#![deny(clippy::expl_impl_clone_on_copy)]
#![deny(clippy::fallible_impl_from)]
#![deny(clippy::large_stack_arrays)]
#![warn(clippy::manual_ok_or)]
#![deny(clippy::missing_docs_in_private_items)]
#![warn(clippy::option_option)]
#![warn(clippy::rc_buffer)]
#![deny(clippy::ref_option_ref)]
#![warn(clippy::trait_duplication_in_bounds)]
#![warn(clippy::unseparated_literal_suffix)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
InvalidInteger,
InvalidString,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::InvalidInteger => write!(f, "Integer was not member of this enumeration"),
Error::InvalidString => write!(f, "String was not member of this enumeration"),
}
}
}
impl std::error::Error for Error {}
#[macro_export]
macro_rules! caret_int {
{
$(#[$meta:meta])*
$v:vis struct $name:ident ( $numtype:ty ) {
$(
$(#[$item_meta:meta])*
$id:ident = $num:literal
),*
$(,)?
}
} => {
#[derive(PartialEq,Eq,Copy,Clone)]
$(#[$meta])*
$v struct $name($numtype);
impl From<$name> for $numtype {
fn from(val: $name) -> $numtype { val.0 }
}
impl From<$numtype> for $name {
fn from(num: $numtype) -> $name { $name(num) }
}
impl $name {
$(
$( #[$item_meta] )*
pub const $id: $name = $name($num) ; )*
fn to_str(self) -> Option<&'static str> {
match self {
$( $name::$id => Some(stringify!($id)), )*
_ => None,
}
}
$v fn is_recognized(self) -> bool {
matches!(self,
$( $name::$id )|*)
}
$v fn from_name(name: &str) -> Option<Self> {
match name {
$( stringify!($id) => Some($name::$id), )*
_ => None
}
}
fn get(self) -> $numtype {
self.into()
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.to_str() {
Some(s) => write!(f, "{}", s),
None => write!(f, "{}", self.0),
}
}
}
impl std::fmt::Debug for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}({})", stringify!($name), self)
}
}
};
}