| 1 |
|
| 2 |
|
| 3 |
use crate::v3; |
| 4 |
#[cfg(feature = "v4")] |
| 5 |
use crate::v4; |
| 6 |
use alloc::string::String; |
| 7 |
use core::fmt; |
| 8 |
|
| 9 |
|
| 10 |
pub type Result<T> = core::result::Result<T, Error>; |
| 11 |
|
| 12 |
|
| 13 |
#[derive(Clone, Debug, Eq, PartialEq)] |
| 14 |
#[non_exhaustive] |
| 15 |
pub enum Error { |
| 16 |
|
| 17 |
InvalidComponent { |
| 18 |
|
| 19 |
component: String, |
| 20 |
}, |
| 21 |
|
| 22 |
|
| 23 |
InvalidMetric { |
| 24 |
|
| 25 |
metric_type: v3::metric::MetricType, |
| 26 |
|
| 27 |
|
| 28 |
value: String, |
| 29 |
}, |
| 30 |
|
| 31 |
#[cfg(feature = "v4")] |
| 32 |
|
| 33 |
InvalidMetricV4 { |
| 34 |
|
| 35 |
metric_type: v4::MetricType, |
| 36 |
|
| 37 |
|
| 38 |
value: String, |
| 39 |
}, |
| 40 |
|
| 41 |
#[cfg(feature = "v4")] |
| 42 |
|
| 43 |
MissingMandatoryMetricV4 { |
| 44 |
|
| 45 |
metric_type: v4::MetricType, |
| 46 |
}, |
| 47 |
|
| 48 |
#[cfg(feature = "v4")] |
| 49 |
|
| 50 |
DuplicateMetricV4 { |
| 51 |
|
| 52 |
metric_type: v4::MetricType, |
| 53 |
}, |
| 54 |
|
| 55 |
#[cfg(feature = "v4")] |
| 56 |
|
| 57 |
InvalidNomenclatureV4 { |
| 58 |
|
| 59 |
nomenclature: String, |
| 60 |
}, |
| 61 |
|
| 62 |
|
| 63 |
InvalidPrefix { |
| 64 |
|
| 65 |
prefix: String, |
| 66 |
}, |
| 67 |
|
| 68 |
|
| 69 |
InvalidSeverity { |
| 70 |
|
| 71 |
name: String, |
| 72 |
}, |
| 73 |
|
| 74 |
|
| 75 |
UnknownMetric { |
| 76 |
|
| 77 |
name: String, |
| 78 |
}, |
| 79 |
|
| 80 |
|
| 81 |
UnsupportedVersion { |
| 82 |
|
| 83 |
version: String, |
| 84 |
}, |
| 85 |
} |
| 86 |
|
| 87 |
impl 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")))] |
| 150 |
impl std::error::Error for Error {} |