Debian Codegraph @

src/v4/metric/environmental/ar.rs

raw symbols SCIP
1 //! Availability Requirements (AR)
2
3 use crate::{
4 Error, Result,
5 v4::metric::{Metric, MetricType},
6 };
7 use alloc::borrow::ToOwned;
8 use core::{fmt, str::FromStr};
9
10 /// Availability Requirements (AR) - CVSS v4.0 Environmental Metric Group
11 ///
12 /// Described in CVSS v4.0 Specification: Section 4.1
13 ///
14 /// > These metrics enable the consumer to customize the assessment depending on
15 /// > the importance of the affected IT asset to the analyst’s organization,
16 /// > measured in terms of Confidentiality, Integrity, and Availability. That
17 /// > is, if an IT asset supports a business function for which Availability is
18 /// > most important, the analyst can assign a greater value to Availability
19 /// > metrics relative to Confidentiality and Integrity. Each Security
20 /// > Requirement has three possible values: Low, Medium, or High, or the
21 /// > default value of Not Defined (X).
22 /// >
23 /// > The full effect on the environmental score is determined by the
24 /// > corresponding Modified Base Impact metrics. Following the concept of
25 /// > assuming “reasonable worst case”, in absence of explicit values, these
26 /// > metrics are set to the default value of Not Defined (X), which is
27 /// > equivalent to the metric value of High (H).
28 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
29 pub enum AvailabilityRequirements {
30 /// Not Defined (X)
31 ///
32 /// > This is the default value. Assigning this value indicates there is
33 /// > insufficient information to choose one of the other values. This has
34 /// > the same effect as assigning High as the worst case.
35 NotDefined,
36 /// Low (L)
37 ///
38 /// > Loss of Availability is likely to have only a limited adverse effect
39 /// > on the organization or individuals associated with the organization
40 /// > (e.g., employees, customers).
41 Low,
42 /// Medium (M)
43 ///
44 /// > Loss of Availability is likely to have a serious adverse effect on the
45 /// > organization or individuals associated with the organization (e.g.,
46 /// > employees, customers).
47 Medium,
48 /// High (H)
49 ///
50 /// > Loss of Availability is likely to have a catastrophic adverse effect
51 /// > on the organization or individuals associated with the organization
52 /// > (e.g., employees, customers).
53 High,
54 }
55
56 impl Default for AvailabilityRequirements {
57 fn default() -> Self {
58 Self::NotDefined
59 }
60 }
61
62 impl Metric for AvailabilityRequirements {
63 const TYPE: MetricType = MetricType::AR;
64
65 fn as_str(self) -> &'static str {
66 match self {
67 AvailabilityRequirements::NotDefined => "X",
68 AvailabilityRequirements::Low => "L",
69 AvailabilityRequirements::Medium => "M",
70 AvailabilityRequirements::High => "H",
71 }
72 }
73 }
74
75 impl fmt::Display for AvailabilityRequirements {
76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 write!(f, "{}:{}", Self::name(), self.as_str())
78 }
79 }
80
81 impl FromStr for AvailabilityRequirements {
82 type Err = Error;
83
84 fn from_str(s: &str) -> Result<Self> {
85 match s {
86 "X" => Ok(AvailabilityRequirements::NotDefined),
87 "L" => Ok(AvailabilityRequirements::Low),
88 "M" => Ok(AvailabilityRequirements::Medium),
89 "H" => Ok(AvailabilityRequirements::High),
90 _ => Err(Error::InvalidMetricV4 {
91 metric_type: Self::TYPE,
92 value: s.to_owned(),
93 }),
94 }
95 }
96 }
97
98 #[cfg(feature = "std")]
99 pub(crate) mod merge {
100 use super::*;
101 use crate::{
102 Error,
103 v4::{MetricType, metric::MetricLevel},
104 };
105 use alloc::borrow::ToOwned;
106 use core::str::FromStr;
107
108 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
109 pub(crate) enum MergedAvailabilityRequirements {
110 Low,
111 Medium,
112 High,
113 }
114
115 impl Default for MergedAvailabilityRequirements {
116 fn default() -> Self {
117 Self::High
118 }
119 }
120
121 impl FromStr for MergedAvailabilityRequirements {
122 type Err = Error;
123
124 fn from_str(s: &str) -> Result<Self> {
125 match s {
126 "L" => Ok(MergedAvailabilityRequirements::Low),
127 "M" => Ok(MergedAvailabilityRequirements::Medium),
128 "H" => Ok(MergedAvailabilityRequirements::High),
129 _ => Err(Error::InvalidMetricV4 {
130 metric_type: MetricType::AR,
131 value: s.to_owned(),
132 }),
133 }
134 }
135 }
136
137 impl AvailabilityRequirements {
138 pub(crate) fn merge(self) -> MergedAvailabilityRequirements {
139 match self {
140 Self::High => MergedAvailabilityRequirements::High,
141 Self::Medium => MergedAvailabilityRequirements::Medium,
142 Self::Low => MergedAvailabilityRequirements::Low,
143 Self::NotDefined => MergedAvailabilityRequirements::High,
144 }
145 }
146 }
147
148 impl MetricLevel for MergedAvailabilityRequirements {
149 fn level(self) -> f64 {
150 // AR_levels = {'H': 0.0, 'M': 0.1, 'L': 0.2}
151 match self {
152 Self::High => 0.0,
153 Self::Medium => 0.1,
154 Self::Low => 0.2,
155 }
156 }
157 }
158 }