| 1 |
|
| 2 |
|
| 3 |
use crate::{Metric, MetricType, error::Error}; |
| 4 |
use alloc::borrow::ToOwned; |
| 5 |
use core::{fmt, str::FromStr}; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 16 |
pub enum PrivilegesRequired { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
High, |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
Low, |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
None, |
| 38 |
} |
| 39 |
|
| 40 |
impl PrivilegesRequired { |
| 41 |
|
| 42 |
pub fn scoped_score(self, scope_change: bool) -> f64 { |
| 43 |
match self { |
| 44 |
PrivilegesRequired::High => { |
| 45 |
if scope_change { |
| 46 |
0.50 |
| 47 |
} else { |
| 48 |
0.27 |
| 49 |
} |
| 50 |
} |
| 51 |
PrivilegesRequired::Low => { |
| 52 |
if scope_change { |
| 53 |
0.68 |
| 54 |
} else { |
| 55 |
0.62 |
| 56 |
} |
| 57 |
} |
| 58 |
PrivilegesRequired::None => 0.85, |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
impl Metric for PrivilegesRequired { |
| 64 |
const TYPE: MetricType = MetricType::PR; |
| 65 |
|
| 66 |
fn score(self) -> f64 { |
| 67 |
self.scoped_score(false) |
| 68 |
} |
| 69 |
|
| 70 |
fn as_str(self) -> &'static str { |
| 71 |
match self { |
| 72 |
PrivilegesRequired::High => "H", |
| 73 |
PrivilegesRequired::Low => "L", |
| 74 |
PrivilegesRequired::None => "N", |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
impl fmt::Display for PrivilegesRequired { |
| 80 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 81 |
write!(f, "{}:{}", Self::name(), self.as_str()) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
impl FromStr for PrivilegesRequired { |
| 86 |
type Err = Error; |
| 87 |
|
| 88 |
fn from_str(s: &str) -> Result<Self, Error> { |
| 89 |
match s { |
| 90 |
"H" => Ok(PrivilegesRequired::High), |
| 91 |
"L" => Ok(PrivilegesRequired::Low), |
| 92 |
"N" => Ok(PrivilegesRequired::None), |
| 93 |
_ => Err(Error::InvalidMetric { |
| 94 |
metric_type: Self::TYPE, |
| 95 |
value: s.to_owned(), |
| 96 |
}), |
| 97 |
} |
| 98 |
} |
| 99 |
} |