Debian Codegraph @

src/v4/metric.rs

raw symbols SCIP
1 //! CVSS v4 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 pub mod base;
11 pub mod environmental;
12 pub mod supplemental;
13 pub mod threat;
14
15 /// Trait for CVSS 4.0 metrics.
16 pub trait Metric: Copy + Clone + Debug + Display + Eq + FromStr + Ord + Default {
17 /// [`MetricType`] of this metric.
18 const TYPE: MetricType;
19
20 /// Get the name of this metric.
21 fn name() -> &'static str {
22 Self::TYPE.name()
23 }
24
25 /// Get `str` describing this metric's value
26 fn as_str(self) -> &'static str;
27 }
28
29 #[cfg(feature = "std")]
30 /// Some metrics have a level associated with them.
31 pub(crate) trait MetricLevel {
32 /// Metric level used in scoring.
33 fn level(self) -> f64;
34 }
35
36 /// Enum over all of the available metrics.
37 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
38 #[non_exhaustive]
39 pub enum MetricType {
40 /// Attack Complexity (AC)
41 AC,
42 /// Attack Requirements (AT)
43 AT,
44 /// Attack Vector (AV)
45 AV,
46 /// Privileges Required (PR)
47 PR,
48 /// Availability Impact to the Subsequent System (SA)
49 SA,
50 /// Confidentiality Impact to the Subsequent System (SC)
51 SC,
52 /// Integrity Impact to the Subsequent System (SI)
53 SI,
54 /// User Interaction (UI)
55 UI,
56 /// Availability Impact to the Vulnerable System (VA)
57 VA,
58 /// Confidentiality Impact to the Vulnerable System (VC)
59 VC,
60 /// Integrity Impact to the Vulnerable System (VI)
61 VI,
62 /// Exploit Maturity (E)
63 E,
64 /// Availability Requirements (AR)
65 AR,
66 /// Confidentiality Requirements (CR)
67 CR,
68 /// Integrity Requirements (IR)
69 IR,
70 /// Modified Attack Complexity (AC)
71 MAC,
72 /// Modified Attack Requirements (MAT)
73 MAT,
74 /// Modified Attack Vector (MAV)
75 MAV,
76 /// Modified Privileges Required (MPR)
77 MPR,
78 /// Modified Availability Impact to the Subsequent System (MSA)
79 MSA,
80 /// Modified Confidentiality Impact to the Subsequent System (MSC)
81 MSC,
82 /// Modified Integrity Impact to the Subsequent System (MSI)
83 MSI,
84 /// Modified User Interaction (MUI)
85 MUI,
86 /// Modified Availability Impact to the Vulnerable System (MVA)
87 MVA,
88 /// Modified Confidentiality Impact to the Vulnerable System (MVC)
89 MVC,
90 /// Modified Integrity Impact to the Vulnerable System (MVI)
91 MVI,
92 /// Automatable (AU)
93 AU,
94 /// Recovery (R)
95 R,
96 /// Vulnerability Response Effort (RE)
97 RE,
98 /// Safety (S)
99 S,
100 /// Provider Urgency (U)
101 U,
102 /// Value Density (V)
103 V,
104 }
105
106 impl MetricType {
107 /// Get the name of this metric (i.e. acronym)
108 pub fn name(self) -> &'static str {
109 match self {
110 Self::AC => "AC",
111 Self::AT => "AT",
112 Self::AV => "AV",
113 Self::PR => "PR",
114 Self::SA => "SA",
115 Self::SC => "SC",
116 Self::SI => "SI",
117 Self::UI => "UI",
118 Self::VA => "VA",
119 Self::VC => "VC",
120 Self::VI => "VI",
121 Self::E => "E",
122 Self::AR => "AR",
123 Self::CR => "CR",
124 Self::IR => "IR",
125 Self::MAC => "MAC",
126 Self::MAT => "MAT",
127 Self::MAV => "MAV",
128 Self::MPR => "MPR",
129 Self::MSA => "MSA",
130 Self::MSC => "MSC",
131 Self::MSI => "MSI",
132 Self::MUI => "MUI",
133 Self::MVA => "MVA",
134 Self::MVC => "MVC",
135 Self::MVI => "MVI",
136 Self::AU => "AU",
137 Self::R => "R",
138 Self::RE => "RE",
139 Self::S => "S",
140 Self::U => "U",
141 Self::V => "V",
142 }
143 }
144
145 /// Get a description of this metric.
146 pub fn description(self) -> &'static str {
147 match self {
148 Self::AC => "Attack Complexity",
149 Self::AT => "Attack Requirements",
150 Self::AV => "Attack Vector",
151 Self::PR => "Privileges Required",
152 Self::SA => "Availability Impact to the Subsequent System",
153 Self::SC => "Confidentiality Impact to the Subsequent System",
154 Self::SI => "Integrity Impact to the Subsequent System",
155 Self::UI => "User Interaction",
156 Self::VA => "Availability Impact to the Vulnerable System",
157 Self::VC => "Confidentiality Impact to the Vulnerable System",
158 Self::VI => "Integrity Impact to the Vulnerable System",
159 Self::E => "Exploit Maturity",
160 Self::AR => "Availability Requirements",
161 Self::CR => "Confidentiality Requirements",
162 Self::IR => "Integrity Requirements",
163 Self::MAC => "Modified Attack Complexity",
164 Self::MAT => "Modified Attack Requirements",
165 Self::MAV => "Modified Attack Vector",
166 Self::MPR => "Modified Privileges Required",
167 Self::MSA => "Modified Availability Impact to the Subsequent System",
168 Self::MSC => "Modified Confidentiality Impact to the Subsequent System",
169 Self::MSI => "Modified Integrity Impact to the Subsequent System",
170 Self::MUI => "Modified User Interaction",
171 Self::MVA => "Modified Availability Impact to the Vulnerable System",
172 Self::MVC => "Modified Confidentiality Impact to the Vulnerable System",
173 Self::MVI => "Modified Integrity Impact to the Vulnerable System",
174 Self::AU => "Automatable",
175 Self::R => "Recovery",
176 Self::RE => "Vulnerability Response Effort",
177 Self::S => "Safety",
178 Self::U => "Provider Urgency",
179 Self::V => "Value Density",
180 }
181 }
182 }
183
184 impl Display for MetricType {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186 f.write_str(self.name())
187 }
188 }
189
190 impl FromStr for MetricType {
191 type Err = Error;
192
193 fn from_str(s: &str) -> Result<Self> {
194 match s {
195 "AC" => Ok(Self::AC),
196 "AT" => Ok(Self::AT),
197 "AV" => Ok(Self::AV),
198 "PR" => Ok(Self::PR),
199 "SA" => Ok(Self::SA),
200 "SC" => Ok(Self::SC),
201 "SI" => Ok(Self::SI),
202 "UI" => Ok(Self::UI),
203 "VA" => Ok(Self::VA),
204 "VC" => Ok(Self::VC),
205 "VI" => Ok(Self::VI),
206 "E" => Ok(Self::E),
207 "AR" => Ok(Self::AR),
208 "CR" => Ok(Self::CR),
209 "IR" => Ok(Self::IR),
210 "MAC" => Ok(Self::MAC),
211 "MAT" => Ok(Self::MAT),
212 "MAV" => Ok(Self::MAV),
213 "MPR" => Ok(Self::MPR),
214 "MSA" => Ok(Self::MSA),
215 "MSC" => Ok(Self::MSC),
216 "MSI" => Ok(Self::MSI),
217 "MUI" => Ok(Self::MUI),
218 "MVA" => Ok(Self::MVA),
219 "MVC" => Ok(Self::MVC),
220 "MVI" => Ok(Self::MVI),
221 "AU" => Ok(Self::AU),
222 "R" => Ok(Self::R),
223 "RE" => Ok(Self::RE),
224 "S" => Ok(Self::S),
225 "U" => Ok(Self::U),
226 "V" => Ok(Self::V),
227 _ => Err(Error::UnknownMetric { name: s.to_owned() }),
228 }
229 }
230 }