[go: up one dir, main page]

cvss/
error.rs

1//! Error types
2
3use crate::v3;
4#[cfg(feature = "v4")]
5use crate::v4;
6use alloc::string::String;
7use core::fmt;
8
9/// Result type with the `cvss` crate's [`Error`] type.
10pub type Result<T> = core::result::Result<T, Error>;
11
12/// Kinds of errors
13#[derive(Clone, Debug, Eq, PartialEq)]
14#[non_exhaustive]
15pub enum Error {
16    /// Invalid component of a CVSS metric group.
17    InvalidComponent {
18        /// Invalid component.
19        component: String,
20    },
21
22    /// Invalid metric for CVSSv3.
23    InvalidMetric {
24        /// The metric that was invalid.
25        metric_type: v3::metric::MetricType,
26
27        /// The value that was provided which is invalid.
28        value: String,
29    },
30
31    #[cfg(feature = "v4")]
32    /// Invalid metric for CVSSv4.
33    InvalidMetricV4 {
34        /// The metric that was invalid.
35        metric_type: v4::MetricType,
36
37        /// The value that was provided which is invalid.
38        value: String,
39    },
40
41    #[cfg(feature = "v4")]
42    /// Missing metric for CVSSv4.
43    MissingMandatoryMetricV4 {
44        /// Prefix which is missing.
45        metric_type: v4::MetricType,
46    },
47
48    #[cfg(feature = "v4")]
49    /// Metric is duplicated for CVSSv4.
50    DuplicateMetricV4 {
51        /// Prefix which is doubled.
52        metric_type: v4::MetricType,
53    },
54
55    #[cfg(feature = "v4")]
56    /// Invalid nomenclature for CVSSv4.
57    InvalidNomenclatureV4 {
58        /// Unknown CBSSv4 nomenclature.
59        nomenclature: String,
60    },
61
62    /// Invalid CVSS string prefix.
63    InvalidPrefix {
64        /// Prefix which is invalid.
65        prefix: String,
66    },
67
68    /// Invalid severity
69    InvalidSeverity {
70        /// Provided name which was unrecognized.
71        name: String,
72    },
73
74    /// Unknown metric name.
75    UnknownMetric {
76        /// Provided name which was unrecognized.
77        name: String,
78    },
79
80    /// Unsupported CVSS version
81    UnsupportedVersion {
82        /// Provided version string.
83        version: String,
84    },
85}
86
87impl fmt::Display for Error {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        match self {
90            Error::InvalidComponent { component } => {
91                write!(f, "invalid CVSS metric group component: `{component}`")
92            }
93            Error::InvalidMetric { metric_type, value } => {
94                write!(
95                    f,
96                    "invalid CVSSv4 {} ({}) metric: `{}`",
97                    metric_type.name(),
98                    metric_type.description(),
99                    value
100                )
101            }
102            #[cfg(feature = "v4")]
103            Error::InvalidMetricV4 { metric_type, value } => {
104                write!(
105                    f,
106                    "invalid CVSSv4 {} ({}) metric: `{}`",
107                    metric_type.name(),
108                    metric_type.description(),
109                    value
110                )
111            }
112            #[cfg(feature = "v4")]
113            Error::DuplicateMetricV4 { metric_type } => {
114                write!(
115                    f,
116                    "duplicate CVSSv4 {} ({}) metric",
117                    metric_type.name(),
118                    metric_type.description(),
119                )
120            }
121            #[cfg(feature = "v4")]
122            Error::MissingMandatoryMetricV4 { metric_type } => {
123                write!(
124                    f,
125                    "missing mandatory CVSSv4 {} ({}) metric",
126                    metric_type.name(),
127                    metric_type.description(),
128                )
129            }
130            #[cfg(feature = "v4")]
131            Error::InvalidNomenclatureV4 { nomenclature } => {
132                write!(f, "invalid CVSSv4 nomenclature: `{}`", nomenclature)
133            }
134            Error::InvalidPrefix { prefix } => {
135                write!(f, "invalid CVSS string prefix: `{prefix}`")
136            }
137            Error::InvalidSeverity { name } => {
138                write!(f, "invalid CVSS Qualitative Severity Rating: `{name}`")
139            }
140            Error::UnknownMetric { name } => write!(f, "unknown CVSS metric name: `{name}`"),
141            Error::UnsupportedVersion { version } => {
142                write!(f, "unsupported CVSS version: {version}")
143            }
144        }
145    }
146}
147
148#[cfg(feature = "std")]
149#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
150impl std::error::Error for Error {}