use crate::{Error, Metric, MetricType, Result};
use alloc::borrow::ToOwned;
use core::{fmt, str::FromStr};
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum AttackComplexity {
High,
Low,
}
#[allow(clippy::derivable_impls)]
impl Default for AttackComplexity {
fn default() -> AttackComplexity {
AttackComplexity::High
}
}
impl Metric for AttackComplexity {
const TYPE: MetricType = MetricType::AC;
fn score(self) -> f64 {
match self {
AttackComplexity::High => 0.44,
AttackComplexity::Low => 0.77,
}
}
fn as_str(self) -> &'static str {
match self {
AttackComplexity::High => "H",
AttackComplexity::Low => "L",
}
}
}
impl fmt::Display for AttackComplexity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", Self::name(), self.as_str())
}
}
impl FromStr for AttackComplexity {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"H" => Ok(AttackComplexity::High),
"L" => Ok(AttackComplexity::Low),
_ => Err(Error::InvalidMetric {
metric_type: Self::TYPE,
value: s.to_owned(),
}),
}
}
}