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 ExploitMaturity {
NotDefined,
Unreported,
ProofOfConcept,
Attacked,
}
impl Default for ExploitMaturity {
fn default() -> Self {
Self::NotDefined
}
}
impl Metric for ExploitMaturity {
const TYPE: MetricType = MetricType::E;
fn as_str(self) -> &'static str {
match self {
ExploitMaturity::NotDefined => "X",
ExploitMaturity::Attacked => "A",
ExploitMaturity::ProofOfConcept => "P",
ExploitMaturity::Unreported => "U",
}
}
}
impl fmt::Display for ExploitMaturity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", Self::name(), self.as_str())
}
}
impl FromStr for ExploitMaturity {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"X" => Ok(ExploitMaturity::NotDefined),
"A" => Ok(ExploitMaturity::Attacked),
"P" => Ok(ExploitMaturity::ProofOfConcept),
"U" => Ok(ExploitMaturity::Unreported),
_ => 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},
};
use alloc::borrow::ToOwned;
use core::str::FromStr;
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub(crate) enum MergedExploitMaturity {
Attacked,
ProofOfConcept,
Unreported,
}
impl Default for MergedExploitMaturity {
fn default() -> Self {
Self::Attacked
}
}
impl FromStr for MergedExploitMaturity {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"A" => Ok(MergedExploitMaturity::Attacked),
"P" => Ok(MergedExploitMaturity::ProofOfConcept),
"U" => Ok(MergedExploitMaturity::Unreported),
_ => Err(Error::InvalidMetricV4 {
metric_type: MetricType::E,
value: s.to_owned(),
}),
}
}
}
impl ExploitMaturity {
pub(crate) fn merge(self) -> MergedExploitMaturity {
match self {
Self::Attacked => MergedExploitMaturity::Attacked,
Self::ProofOfConcept => MergedExploitMaturity::ProofOfConcept,
Self::Unreported => MergedExploitMaturity::Unreported,
Self::NotDefined => MergedExploitMaturity::Attacked,
}
}
}
impl MetricLevel for MergedExploitMaturity {
fn level(self) -> f64 {
match self {
Self::Unreported => 0.2,
Self::ProofOfConcept => 0.1,
Self::Attacked => 0.0,
}
}
}
}