use crate::{Metric, MetricType, error::Error};
use alloc::borrow::ToOwned;
use core::{fmt, str::FromStr};
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum PrivilegesRequired {
High,
Low,
None,
}
impl PrivilegesRequired {
pub fn scoped_score(self, scope_change: bool) -> f64 {
match self {
PrivilegesRequired::High => {
if scope_change {
0.50
} else {
0.27
}
}
PrivilegesRequired::Low => {
if scope_change {
0.68
} else {
0.62
}
}
PrivilegesRequired::None => 0.85,
}
}
}
impl Metric for PrivilegesRequired {
const TYPE: MetricType = MetricType::PR;
fn score(self) -> f64 {
self.scoped_score(false)
}
fn as_str(self) -> &'static str {
match self {
PrivilegesRequired::High => "H",
PrivilegesRequired::Low => "L",
PrivilegesRequired::None => "N",
}
}
}
impl fmt::Display for PrivilegesRequired {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", Self::name(), self.as_str())
}
}
impl FromStr for PrivilegesRequired {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
match s {
"H" => Ok(PrivilegesRequired::High),
"L" => Ok(PrivilegesRequired::Low),
"N" => Ok(PrivilegesRequired::None),
_ => Err(Error::InvalidMetric {
metric_type: Self::TYPE,
value: s.to_owned(),
}),
}
}
}