#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommonState<'a> {
Health,
Errors,
Sensor,
Alert { alert_key: &'a str },
EvidenceSelf,
EvidenceDevice { device: &'a str },
EvidenceNames { ip_slug: &'a str },
EvidenceRelation { relation_id: &'a str },
CatalogEntity { entity_id: &'a str },
CatalogAlias { old_id: &'a str },
CatalogPdns { ip_slug: &'a str },
CatalogIncident { incident_id: &'a str },
CatalogAck { alert_ref: &'a str },
CatalogSilence { id: &'a str },
CatalogEdge { edge_id: &'a str },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CommonFamily {
Health,
Errors,
Sensor,
Alert,
EvidenceSelf,
EvidenceDevice,
EvidenceNames,
EvidenceRelation,
}
impl CommonFamily {
pub const ALL: [CommonFamily; 8] = [
CommonFamily::Health,
CommonFamily::Errors,
CommonFamily::Sensor,
CommonFamily::Alert,
CommonFamily::EvidenceSelf,
CommonFamily::EvidenceDevice,
CommonFamily::EvidenceNames,
CommonFamily::EvidenceRelation,
];
pub fn token(self) -> &'static str {
match self {
CommonFamily::Health => "health",
CommonFamily::Errors => "errors",
CommonFamily::Sensor => "sensor",
CommonFamily::Alert => "alert",
CommonFamily::EvidenceSelf => "evidence_self",
CommonFamily::EvidenceDevice => "evidence_device",
CommonFamily::EvidenceNames => "evidence_names",
CommonFamily::EvidenceRelation => "evidence_relation",
}
}
pub fn prefix(self) -> &'static [&'static str] {
match self {
CommonFamily::Health => &["health"],
CommonFamily::Errors => &["errors"],
CommonFamily::Sensor => &["sensor"],
CommonFamily::Alert => &["alert"],
CommonFamily::EvidenceSelf => &["evidence", "self"],
CommonFamily::EvidenceDevice => &["evidence", "device"],
CommonFamily::EvidenceNames => &["evidence", "names"],
CommonFamily::EvidenceRelation => &["evidence", "relation"],
}
}
pub fn var(self) -> Option<&'static str> {
match self {
CommonFamily::Alert => Some("alert_key"),
CommonFamily::EvidenceDevice => Some("device"),
CommonFamily::EvidenceNames => Some("ip_slug"),
CommonFamily::EvidenceRelation => Some("relation_id"),
CommonFamily::Health
| CommonFamily::Errors
| CommonFamily::Sensor
| CommonFamily::EvidenceSelf => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::grammar::is_valid_plain_chunk;
#[test]
fn family_table_is_grammar_legal_and_distinct() {
let mut tokens = Vec::new();
for f in CommonFamily::ALL {
assert!(
f.prefix().iter().all(|c| is_valid_plain_chunk(c)),
"{f:?} prefix violates RFC 03 §2"
);
assert!(!f.prefix().is_empty(), "{f:?} has no path");
tokens.push(f.token());
}
tokens.sort_unstable();
tokens.dedup();
assert_eq!(tokens.len(), CommonFamily::ALL.len());
}
#[test]
fn evidence_relation_is_a_population_keyed_family() {
assert!(CommonFamily::ALL.contains(&CommonFamily::EvidenceRelation));
assert_eq!(CommonFamily::EvidenceRelation.token(), "evidence_relation");
assert_eq!(
CommonFamily::EvidenceRelation.prefix(),
&["evidence", "relation"]
);
assert_eq!(CommonFamily::EvidenceRelation.var(), Some("relation_id"));
}
}