1use core::cmp::Ordering;
7use core::hash::{Hash, Hasher};
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub enum Severity {
12 Info,
14 Warning,
16 Error,
18}
19
20#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub enum Fidelity {
23 Equivalent,
25 Approximated,
27 Drawn,
29 Dropped,
31}
32
33#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
35pub enum ParamType {
36 UInt,
38 Int,
40 Float,
42 Text,
44 Key,
46}
47
48#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
50pub struct Param {
51 name: &'static str,
52 ty: ParamType,
53}
54
55impl Param {
56 pub(crate) const fn new(name: &'static str, ty: ParamType) -> Self {
57 Self { name, ty }
58 }
59
60 pub const fn plugin(name: &'static str, ty: ParamType) -> Self {
62 Self::new(name, ty)
63 }
64
65 pub const fn name(&self) -> &'static str {
67 self.name
68 }
69
70 pub const fn ty(&self) -> ParamType {
72 self.ty
73 }
74}
75
76#[derive(Clone, Copy, Debug)]
78pub struct Code {
79 name: &'static str,
80 severity: Severity,
81 fidelity: Option<Fidelity>,
82 params: &'static [Param],
83 group_by: &'static [&'static str],
84}
85
86impl Code {
87 pub(crate) const fn new(
88 name: &'static str,
89 severity: Severity,
90 fidelity: Option<Fidelity>,
91 params: &'static [Param],
92 group_by: &'static [&'static str],
93 ) -> Self {
94 Self {
95 name,
96 severity,
97 fidelity,
98 params,
99 group_by,
100 }
101 }
102
103 pub const fn plugin(
106 name: &'static str,
107 severity: Severity,
108 fidelity: Option<Fidelity>,
109 params: &'static [Param],
110 group_by: &'static [&'static str],
111 ) -> Self {
112 Self::new(name, severity, fidelity, params, group_by)
113 }
114
115 pub const fn name(&self) -> &'static str {
117 self.name
118 }
119
120 pub const fn severity(&self) -> Severity {
122 self.severity
123 }
124
125 pub const fn fidelity(&self) -> Option<Fidelity> {
127 self.fidelity
128 }
129
130 pub const fn params(&self) -> &'static [Param] {
132 self.params
133 }
134
135 pub const fn group_by(&self) -> &'static [&'static str] {
137 self.group_by
138 }
139
140 pub fn param(&self, name: &str) -> Option<Param> {
142 self.params.iter().find(|param| param.name == name).copied()
143 }
144}
145
146impl PartialEq for Code {
147 fn eq(&self, other: &Self) -> bool {
148 self.name == other.name
149 }
150}
151
152impl Eq for Code {}
153
154impl PartialOrd for Code {
155 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
156 Some(self.cmp(other))
157 }
158}
159
160impl Ord for Code {
161 fn cmp(&self, other: &Self) -> Ordering {
162 self.name.cmp(other.name)
163 }
164}
165
166impl Hash for Code {
167 fn hash<H: Hasher>(&self, state: &mut H) {
168 self.name.hash(state);
169 }
170}
171
172#[cfg(test)]
173mod tests {
174 use crate::codes;
175
176 #[test]
177 fn all_codes_are_sorted_unique_and_well_formed() {
178 let names: Vec<&str> = codes::ALL.iter().map(|code| code.name()).collect();
179 let mut sorted = names.clone();
180 sorted.sort_unstable();
181 sorted.dedup();
182 assert_eq!(names, sorted);
183 for name in names {
184 assert_eq!(name.split('.').count(), 3, "{name}");
185 }
186 }
187
188 #[test]
189 fn looks_up_params_by_name() {
190 let code = codes::BUDGET_RECORDS_EXCEEDED;
191 assert!(code.param("limit").is_some());
192 assert!(code.param("missing").is_none());
193 }
194}