| 1 |
|
| 2 |
|
| 3 |
use crate::{Error, Result}; |
| 4 |
use alloc::borrow::ToOwned; |
| 5 |
use core::{ |
| 6 |
fmt::{self, Debug, Display}, |
| 7 |
str::FromStr, |
| 8 |
}; |
| 9 |
|
| 10 |
|
| 11 |
pub trait Metric: Copy + Clone + Debug + Display + Eq + FromStr + Ord { |
| 12 |
|
| 13 |
const TYPE: MetricType; |
| 14 |
|
| 15 |
|
| 16 |
fn name() -> &'static str { |
| 17 |
Self::TYPE.name() |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
fn score(self) -> f64; |
| 22 |
|
| 23 |
|
| 24 |
fn as_str(self) -> &'static str; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 29 |
#[non_exhaustive] |
| 30 |
pub enum MetricType { |
| 31 |
|
| 32 |
A, |
| 33 |
|
| 34 |
|
| 35 |
AC, |
| 36 |
|
| 37 |
|
| 38 |
AV, |
| 39 |
|
| 40 |
|
| 41 |
C, |
| 42 |
|
| 43 |
|
| 44 |
I, |
| 45 |
|
| 46 |
|
| 47 |
PR, |
| 48 |
|
| 49 |
|
| 50 |
S, |
| 51 |
|
| 52 |
|
| 53 |
UI, |
| 54 |
} |
| 55 |
|
| 56 |
impl MetricType { |
| 57 |
|
| 58 |
pub fn name(self) -> &'static str { |
| 59 |
match self { |
| 60 |
Self::A => "A", |
| 61 |
Self::AC => "AC", |
| 62 |
Self::AV => "AV", |
| 63 |
Self::C => "C", |
| 64 |
Self::I => "I", |
| 65 |
Self::PR => "PR", |
| 66 |
Self::S => "S", |
| 67 |
Self::UI => "UI", |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
pub fn description(self) -> &'static str { |
| 73 |
match self { |
| 74 |
Self::A => "Availability Impact", |
| 75 |
Self::AC => "Attack Complexity", |
| 76 |
Self::AV => "Attack Vector", |
| 77 |
Self::C => "Confidentiality Impact", |
| 78 |
Self::I => "Integrity Impact", |
| 79 |
Self::PR => "Privileges Required", |
| 80 |
Self::S => "Scope", |
| 81 |
Self::UI => "User Interaction", |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
impl Display for MetricType { |
| 87 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 88 |
f.write_str(self.name()) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
impl FromStr for MetricType { |
| 93 |
type Err = Error; |
| 94 |
|
| 95 |
fn from_str(s: &str) -> Result<Self> { |
| 96 |
match s { |
| 97 |
"A" => Ok(Self::A), |
| 98 |
"AC" => Ok(Self::AC), |
| 99 |
"AV" => Ok(Self::AV), |
| 100 |
"C" => Ok(Self::C), |
| 101 |
"I" => Ok(Self::I), |
| 102 |
"PR" => Ok(Self::PR), |
| 103 |
"S" => Ok(Self::S), |
| 104 |
"UI" => Ok(Self::UI), |
| 105 |
_ => Err(Error::UnknownMetric { name: s.to_owned() }), |
| 106 |
} |
| 107 |
} |
| 108 |
} |