1use crate::v3;
4#[cfg(feature = "v4")]
5use crate::v4;
6use alloc::string::String;
7use core::fmt;
8
9pub type Result<T> = core::result::Result<T, Error>;
11
12#[derive(Clone, Debug, Eq, PartialEq)]
14#[non_exhaustive]
15pub enum Error {
16 InvalidComponent {
18 component: String,
20 },
21
22 InvalidMetric {
24 metric_type: v3::metric::MetricType,
26
27 value: String,
29 },
30
31 #[cfg(feature = "v4")]
32 InvalidMetricV4 {
34 metric_type: v4::MetricType,
36
37 value: String,
39 },
40
41 #[cfg(feature = "v4")]
42 MissingMandatoryMetricV4 {
44 metric_type: v4::MetricType,
46 },
47
48 #[cfg(feature = "v4")]
49 DuplicateMetricV4 {
51 metric_type: v4::MetricType,
53 },
54
55 #[cfg(feature = "v4")]
56 InvalidNomenclatureV4 {
58 nomenclature: String,
60 },
61
62 InvalidPrefix {
64 prefix: String,
66 },
67
68 InvalidSeverity {
70 name: String,
72 },
73
74 UnknownMetric {
76 name: String,
78 },
79
80 UnsupportedVersion {
82 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 {}