| 1 |
|
| 2 |
|
| 3 |
use crate::{Error, Result}; |
| 4 |
use alloc::borrow::ToOwned; |
| 5 |
use core::{fmt, str::FromStr}; |
| 6 |
|
| 7 |
#[cfg(feature = "serde")] |
| 8 |
use { |
| 9 |
alloc::string::String, |
| 10 |
serde::{Deserialize, Serialize, de, ser}, |
| 11 |
}; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)] |
| 26 |
pub enum Severity { |
| 27 |
|
| 28 |
None, |
| 29 |
|
| 30 |
|
| 31 |
Low, |
| 32 |
|
| 33 |
|
| 34 |
Medium, |
| 35 |
|
| 36 |
|
| 37 |
High, |
| 38 |
|
| 39 |
|
| 40 |
Critical, |
| 41 |
} |
| 42 |
|
| 43 |
impl Severity { |
| 44 |
|
| 45 |
pub fn as_str(self) -> &'static str { |
| 46 |
match self { |
| 47 |
Severity::None => "none", |
| 48 |
Severity::Low => "low", |
| 49 |
Severity::Medium => "medium", |
| 50 |
Severity::High => "high", |
| 51 |
Severity::Critical => "critical", |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
impl FromStr for Severity { |
| 57 |
type Err = Error; |
| 58 |
|
| 59 |
fn from_str(s: &str) -> Result<Self> { |
| 60 |
match s.to_ascii_lowercase().as_str() { |
| 61 |
"none" => Ok(Severity::None), |
| 62 |
"low" => Ok(Severity::Low), |
| 63 |
"medium" => Ok(Severity::Medium), |
| 64 |
"high" => Ok(Severity::High), |
| 65 |
"critical" => Ok(Severity::Critical), |
| 66 |
_ => Err(Error::InvalidSeverity { name: s.to_owned() }), |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
impl fmt::Display for Severity { |
| 72 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 73 |
f.write_str(self.as_str()) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
#[cfg(feature = "serde")] |
| 78 |
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))] |
| 79 |
impl<'de> Deserialize<'de> for Severity { |
| 80 |
fn deserialize<D: de::Deserializer<'de>>( |
| 81 |
deserializer: D, |
| 82 |
) -> core::result::Result<Self, D::Error> { |
| 83 |
String::deserialize(deserializer)? |
| 84 |
.parse() |
| 85 |
.map_err(de::Error::custom) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
#[cfg(feature = "serde")] |
| 90 |
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))] |
| 91 |
impl Serialize for Severity { |
| 92 |
fn serialize<S: ser::Serializer>( |
| 93 |
&self, |
| 94 |
serializer: S, |
| 95 |
) -> core::result::Result<S::Ok, S::Error> { |
| 96 |
self.as_str().serialize(serializer) |
| 97 |
} |
| 98 |
} |