use core::cmp::Ordering;
use core::hash::{Hash, Hasher};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Severity {
Info,
Warning,
Error,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Fidelity {
Equivalent,
Approximated,
Drawn,
Dropped,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ParamType {
UInt,
Int,
Float,
Text,
Key,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Param {
name: &'static str,
ty: ParamType,
}
impl Param {
pub(crate) const fn new(name: &'static str, ty: ParamType) -> Self {
Self { name, ty }
}
pub const fn plugin(name: &'static str, ty: ParamType) -> Self {
Self::new(name, ty)
}
pub const fn name(&self) -> &'static str {
self.name
}
pub const fn ty(&self) -> ParamType {
self.ty
}
}
#[derive(Clone, Copy, Debug)]
pub struct Code {
name: &'static str,
severity: Severity,
fidelity: Option<Fidelity>,
params: &'static [Param],
group_by: &'static [&'static str],
}
impl Code {
pub(crate) const fn new(
name: &'static str,
severity: Severity,
fidelity: Option<Fidelity>,
params: &'static [Param],
group_by: &'static [&'static str],
) -> Self {
Self {
name,
severity,
fidelity,
params,
group_by,
}
}
pub const fn plugin(
name: &'static str,
severity: Severity,
fidelity: Option<Fidelity>,
params: &'static [Param],
group_by: &'static [&'static str],
) -> Self {
Self::new(name, severity, fidelity, params, group_by)
}
pub const fn name(&self) -> &'static str {
self.name
}
pub const fn severity(&self) -> Severity {
self.severity
}
pub const fn fidelity(&self) -> Option<Fidelity> {
self.fidelity
}
pub const fn params(&self) -> &'static [Param] {
self.params
}
pub const fn group_by(&self) -> &'static [&'static str] {
self.group_by
}
pub fn param(&self, name: &str) -> Option<Param> {
self.params.iter().find(|param| param.name == name).copied()
}
}
impl PartialEq for Code {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Eq for Code {}
impl PartialOrd for Code {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Code {
fn cmp(&self, other: &Self) -> Ordering {
self.name.cmp(other.name)
}
}
impl Hash for Code {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}
#[cfg(test)]
mod tests {
use crate::codes;
#[test]
fn all_codes_are_sorted_unique_and_well_formed() {
let names: Vec<&str> = codes::ALL.iter().map(|code| code.name()).collect();
let mut sorted = names.clone();
sorted.sort_unstable();
sorted.dedup();
assert_eq!(names, sorted);
for name in names {
assert_eq!(name.split('.').count(), 3, "{name}");
}
}
#[test]
fn looks_up_params_by_name() {
let code = codes::BUDGET_RECORDS_EXCEEDED;
assert!(code.param("limit").is_some());
assert!(code.param("missing").is_none());
}
}