use std::cmp::Ordering;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Confidence {
Low,
Medium,
High,
}
impl PartialOrd for Confidence {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Confidence {
fn cmp(&self, other: &Self) -> Ordering {
self.rank().cmp(&other.rank())
}
}
impl Confidence {
fn rank(self) -> u8 {
match self {
Self::Low => 0,
Self::Medium => 1,
Self::High => 2,
}
}
pub fn label(self) -> &'static str {
match self {
Self::Low => "low",
Self::Medium => "medium",
Self::High => "high",
}
}
}
#[derive(Debug, Clone)]
pub enum ProposalKind {
BundledRuleset {
uri: String,
},
Rule {
kind: String,
yaml: String,
},
}
#[derive(Debug, Clone)]
pub struct Evidence {
pub message: String,
}
#[derive(Debug, Clone)]
pub struct Proposal {
pub id: String,
pub kind: ProposalKind,
pub confidence: Confidence,
pub evidence: Vec<Evidence>,
pub summary: String,
}
impl Proposal {
pub fn rule_id(&self) -> &str {
&self.id
}
pub fn is_bundled(&self) -> bool {
matches!(self.kind, ProposalKind::BundledRuleset { .. })
}
pub fn bundled_uri(&self) -> Option<&str> {
match &self.kind {
ProposalKind::BundledRuleset { uri } => Some(uri.as_str()),
ProposalKind::Rule { .. } => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn confidence_orders_low_below_high() {
assert!(Confidence::Low < Confidence::Medium);
assert!(Confidence::Medium < Confidence::High);
assert!(Confidence::High > Confidence::Low);
}
#[test]
fn confidence_floor_filters_with_partial_ord() {
let xs = [Confidence::Low, Confidence::Medium, Confidence::High];
let surviving: Vec<_> = xs.iter().filter(|c| **c >= Confidence::Medium).collect();
assert_eq!(surviving.len(), 2);
}
#[test]
fn proposal_bundled_helpers_round_trip() {
let p = Proposal {
id: "alint://bundled/rust@v1".into(),
kind: ProposalKind::BundledRuleset {
uri: "alint://bundled/rust@v1".into(),
},
confidence: Confidence::High,
evidence: vec![],
summary: "Cargo.toml at root".into(),
};
assert!(p.is_bundled());
assert_eq!(p.bundled_uri(), Some("alint://bundled/rust@v1"));
assert_eq!(p.rule_id(), "alint://bundled/rust@v1");
}
}