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 Integrity {
None,
Low,
High,
}
impl Metric for Integrity {
const TYPE: MetricType = MetricType::I;
fn score(self) -> f64 {
match self {
Integrity::None => 0.0,
Integrity::Low => 0.22,
Integrity::High => 0.56,
}
}
fn as_str(self) -> &'static str {
match self {
Integrity::None => "N",
Integrity::Low => "L",
Integrity::High => "H",
}
}
}
impl fmt::Display for Integrity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", Self::name(), self.as_str())
}
}
impl FromStr for Integrity {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"N" => Ok(Integrity::None),
"L" => Ok(Integrity::Low),
"H" => Ok(Integrity::High),
_ => Err(Error::InvalidMetric {
metric_type: Self::TYPE,
value: s.to_owned(),
}),
}
}
}