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 IntegrityImpactToTheSubsequentSystem {
None,
Low,
High,
}
impl Default for IntegrityImpactToTheSubsequentSystem {
fn default() -> Self {
Self::High
}
}
impl Metric for IntegrityImpactToTheSubsequentSystem {
const TYPE: MetricType = MetricType::SI;
fn as_str(self) -> &'static str {
match self {
IntegrityImpactToTheSubsequentSystem::None => "N",
IntegrityImpactToTheSubsequentSystem::Low => "L",
IntegrityImpactToTheSubsequentSystem::High => "H",
}
}
}
impl fmt::Display for IntegrityImpactToTheSubsequentSystem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", Self::name(), self.as_str())
}
}
impl FromStr for IntegrityImpactToTheSubsequentSystem {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"N" => Ok(IntegrityImpactToTheSubsequentSystem::None),
"L" => Ok(IntegrityImpactToTheSubsequentSystem::Low),
"H" => Ok(IntegrityImpactToTheSubsequentSystem::High),
_ => 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::ModifiedIntegrityImpactToTheSubsequentSystem},
},
};
use alloc::borrow::ToOwned;
use core::str::FromStr;
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub(crate) enum MergedIntegrityImpactToTheSubsequentSystem {
Safety,
High,
Low,
None,
}
impl Default for MergedIntegrityImpactToTheSubsequentSystem {
fn default() -> Self {
Self::High
}
}
impl FromStr for MergedIntegrityImpactToTheSubsequentSystem {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"S" => Ok(MergedIntegrityImpactToTheSubsequentSystem::Safety),
"H" => Ok(MergedIntegrityImpactToTheSubsequentSystem::High),
"L" => Ok(MergedIntegrityImpactToTheSubsequentSystem::Low),
"N" => Ok(MergedIntegrityImpactToTheSubsequentSystem::None),
_ => Err(Error::InvalidMetricV4 {
metric_type: MetricType::SI,
value: s.to_owned(),
}),
}
}
}
impl MetricLevel for MergedIntegrityImpactToTheSubsequentSystem {
fn level(self) -> f64 {
match self {
Self::Safety => 0.0,
Self::High => 0.1,
Self::Low => 0.2,
Self::None => 0.3,
}
}
}
impl IntegrityImpactToTheSubsequentSystem {
pub(crate) fn merge(
self,
value: Option<ModifiedIntegrityImpactToTheSubsequentSystem>,
) -> MergedIntegrityImpactToTheSubsequentSystem {
match value {
Some(ModifiedIntegrityImpactToTheSubsequentSystem::NotDefined) | None => match self
{
Self::High => MergedIntegrityImpactToTheSubsequentSystem::High,
Self::Low => MergedIntegrityImpactToTheSubsequentSystem::Low,
Self::None => MergedIntegrityImpactToTheSubsequentSystem::None,
},
Some(ModifiedIntegrityImpactToTheSubsequentSystem::High) => {
MergedIntegrityImpactToTheSubsequentSystem::High
}
Some(ModifiedIntegrityImpactToTheSubsequentSystem::Low) => {
MergedIntegrityImpactToTheSubsequentSystem::Low
}
Some(ModifiedIntegrityImpactToTheSubsequentSystem::Negligible) => {
MergedIntegrityImpactToTheSubsequentSystem::None
}
Some(ModifiedIntegrityImpactToTheSubsequentSystem::Safety) => {
MergedIntegrityImpactToTheSubsequentSystem::Safety
}
}
}
}
}