use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum RiskLevel {
None,
Info,
Low,
Medium,
High,
Unknown,
Error,
}
impl fmt::Display for RiskLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RiskLevel::None => write!(f, "none"),
RiskLevel::Info => write!(f, "info"),
RiskLevel::Low => write!(f, "low"),
RiskLevel::Medium => write!(f, "medium"),
RiskLevel::High => write!(f, "high"),
RiskLevel::Unknown => write!(f, "unknown"),
RiskLevel::Error => write!(f, "error"),
}
}
}
#[derive(Debug, Clone)]
pub struct Indicator {
pub id: String,
pub name: String,
pub description: String,
pub value: String,
pub risk: RiskLevel,
}
impl Indicator {
pub fn new(
id: impl Into<String>,
name: impl Into<String>,
description: impl Into<String>,
value: impl Into<String>,
risk: RiskLevel,
) -> Self {
Self {
id: id.into(),
name: name.into(),
description: description.into(),
value: value.into(),
risk,
}
}
}
impl fmt::Display for Indicator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}: {} = {} ({})",
self.id, self.name, self.value, self.risk
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_risk_level_ordering() {
assert!(RiskLevel::None < RiskLevel::Info);
assert!(RiskLevel::Info < RiskLevel::Low);
assert!(RiskLevel::Low < RiskLevel::Medium);
assert!(RiskLevel::Medium < RiskLevel::High);
}
#[test]
fn test_risk_level_display() {
assert_eq!(RiskLevel::High.to_string(), "high");
assert_eq!(RiskLevel::None.to_string(), "none");
assert_eq!(RiskLevel::Error.to_string(), "error");
}
#[test]
fn test_indicator_new() {
let ind = Indicator::new("test_id", "Test Name", "Test description", "True", RiskLevel::Medium);
assert_eq!(ind.id, "test_id");
assert_eq!(ind.name, "Test Name");
assert_eq!(ind.value, "True");
assert_eq!(ind.risk, RiskLevel::Medium);
}
#[test]
fn test_indicator_display() {
let ind = Indicator::new("vba", "VBA Macros", "Contains VBA macros", "True", RiskLevel::Medium);
let display = format!("{}", ind);
assert!(display.contains("vba"));
assert!(display.contains("True"));
assert!(display.contains("medium"));
}
}