use crate::{Error, Metric, MetricType};
use alloc::borrow::ToOwned;
use core::{fmt, str::FromStr};
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum AttackVector {
Physical,
Local,
Adjacent,
Network,
}
impl Metric for AttackVector {
const TYPE: MetricType = MetricType::AV;
fn score(self) -> f64 {
match self {
AttackVector::Physical => 0.20,
AttackVector::Local => 0.55,
AttackVector::Adjacent => 0.62,
AttackVector::Network => 0.85,
}
}
fn as_str(self) -> &'static str {
match self {
AttackVector::Physical => "P",
AttackVector::Local => "L",
AttackVector::Adjacent => "A",
AttackVector::Network => "N",
}
}
}
impl fmt::Display for AttackVector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", Self::name(), self.as_str())
}
}
impl FromStr for AttackVector {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
match s {
"P" => Ok(AttackVector::Physical),
"L" => Ok(AttackVector::Local),
"A" => Ok(AttackVector::Adjacent),
"N" => Ok(AttackVector::Network),
_ => Err(Error::InvalidMetric {
metric_type: Self::TYPE,
value: s.to_owned(),
}),
}
}
}