use crate::{
Error, Result,
v4::metric::{Metric, MetricType},
};
use alloc::borrow::ToOwned;
use core::{fmt, str::FromStr};
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum AttackComplexity {
High,
Low,
}
impl Default for AttackComplexity {
fn default() -> Self {
Self::Low
}
}
impl Metric for AttackComplexity {
const TYPE: MetricType = MetricType::AC;
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::InvalidMetricV4 {
metric_type: Self::TYPE,
value: s.to_owned(),
}),
}
}
}
#[cfg(feature = "std")]
pub(crate) mod merge {
use super::*;
use crate::{
Error,
v4::{
MetricType,
metric::{MetricLevel, environmental::ModifiedAttackComplexity},
},
};
use alloc::borrow::ToOwned;
use core::str::FromStr;
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub(crate) enum MergedAttackComplexity {
High,
Low,
}
impl Default for MergedAttackComplexity {
fn default() -> Self {
Self::Low
}
}
impl FromStr for MergedAttackComplexity {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"H" => Ok(MergedAttackComplexity::High),
"L" => Ok(MergedAttackComplexity::Low),
_ => Err(Error::InvalidMetricV4 {
metric_type: MetricType::AC,
value: s.to_owned(),
}),
}
}
}
impl MetricLevel for MergedAttackComplexity {
fn level(self) -> f64 {
match self {
Self::High => 0.1,
Self::Low => 0.0,
}
}
}
impl AttackComplexity {
pub(crate) fn merge(
self,
value: Option<ModifiedAttackComplexity>,
) -> MergedAttackComplexity {
match value {
Some(ModifiedAttackComplexity::NotDefined) | None => match self {
Self::High => MergedAttackComplexity::High,
Self::Low => MergedAttackComplexity::Low,
},
Some(ModifiedAttackComplexity::High) => MergedAttackComplexity::High,
Some(ModifiedAttackComplexity::Low) => MergedAttackComplexity::Low,
}
}
}
}