reddb_server/modules/common/
mod.rs1use std::fmt;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
4pub enum Severity {
5 #[default]
6 Info,
7 Low,
8 Medium,
9 High,
10 Critical,
11}
12
13impl Severity {
14 pub fn as_str(&self) -> &'static str {
15 match self {
16 Self::Info => "info",
17 Self::Low => "low",
18 Self::Medium => "medium",
19 Self::High => "high",
20 Self::Critical => "critical",
21 }
22 }
23
24 pub fn as_upper(&self) -> &'static str {
25 match self {
26 Self::Info => "INFO",
27 Self::Low => "LOW",
28 Self::Medium => "MEDIUM",
29 Self::High => "HIGH",
30 Self::Critical => "CRITICAL",
31 }
32 }
33
34 pub fn from_str(s: &str) -> Option<Self> {
35 match s.to_ascii_lowercase().as_str() {
36 "info" | "informational" | "none" => Some(Self::Info),
37 "low" => Some(Self::Low),
38 "medium" | "moderate" => Some(Self::Medium),
39 "high" => Some(Self::High),
40 "critical" | "crit" => Some(Self::Critical),
41 _ => None,
42 }
43 }
44
45 pub fn cvss_range(&self) -> (f32, f32) {
46 match self {
47 Self::Info => (0.0, 0.0),
48 Self::Low => (0.1, 3.9),
49 Self::Medium => (4.0, 6.9),
50 Self::High => (7.0, 8.9),
51 Self::Critical => (9.0, 10.0),
52 }
53 }
54
55 pub fn from_cvss(score: f32) -> Self {
56 if score >= 9.0 {
57 Self::Critical
58 } else if score >= 7.0 {
59 Self::High
60 } else if score >= 4.0 {
61 Self::Medium
62 } else if score > 0.0 {
63 Self::Low
64 } else {
65 Self::Info
66 }
67 }
68
69 pub fn weight(&self) -> u8 {
70 match self {
71 Self::Info => 0,
72 Self::Low => 1,
73 Self::Medium => 2,
74 Self::High => 3,
75 Self::Critical => 4,
76 }
77 }
78
79 pub fn color_code(&self) -> &'static str {
80 match self {
81 Self::Info => "\x1b[37m",
82 Self::Low => "\x1b[36m",
83 Self::Medium => "\x1b[33m",
84 Self::High => "\x1b[31m",
85 Self::Critical => "\x1b[91m",
86 }
87 }
88}
89
90impl fmt::Display for Severity {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 write!(f, "{}", self.as_upper())
93 }
94}