Debian Codegraph @

src/v3/metric.rs

raw symbols SCIP
1 //! CVSS v3.0/v3.1 metrics.
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 /// Trait for CVSSv3 metrics.
11 pub trait Metric: Copy + Clone + Debug + Display + Eq + FromStr + Ord {
12 /// [`MetricType`] of this metric.
13 const TYPE: MetricType;
14
15 /// Get the name of this metric.
16 fn name() -> &'static str {
17 Self::TYPE.name()
18 }
19
20 /// Get CVSS v3.1 score for this metric.
21 fn score(self) -> f64;
22
23 /// Get `str` describing this metric's value
24 fn as_str(self) -> &'static str;
25 }
26
27 /// Enum over all of the available CVSSv3 metrics.
28 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
29 #[non_exhaustive]
30 pub enum MetricType {
31 /// Availability Impact (A)
32 A,
33
34 /// Attack Complexity (AC)
35 AC,
36
37 /// Attack Vector (AV)
38 AV,
39
40 /// Confidentiality Impact (C)
41 C,
42
43 /// Integrity Impact (I)
44 I,
45
46 /// Privileges Required (PR)
47 PR,
48
49 /// Scope (S)
50 S,
51
52 /// User Interaction (UI)
53 UI,
54 }
55
56 impl MetricType {
57 /// Get the name of this metric (i.e. acronym)
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 /// Get a description of this metric.
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 }