[go: up one dir, main page]

cvss/v3/
score.rs

1//! CVSS v3.1 scores
2
3use crate::severity::Severity;
4
5/// CVSS v3.1 scores.
6///
7/// Formula described in CVSS v3.1 Specification: Section 5:
8/// <https://www.first.org/cvss/specification-document#t20>
9#[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd)]
10pub struct Score(f64);
11
12impl Score {
13    /// Create a new score object
14    pub fn new(score: f64) -> Score {
15        Score(score)
16    }
17
18    /// Get the score as a floating point value
19    pub fn value(self) -> f64 {
20        self.0
21    }
22
23    /// Round the score up to the algorithm described in
24    /// CVSS v3.1: Appendix A - Floating Point Rounding.
25    ///
26    /// <https://www.first.org/cvss/specification-document#t25>
27    #[cfg(feature = "std")]
28    pub fn roundup(self) -> Score {
29        let score_int = (self.0 * 100_000.0) as u64;
30
31        if score_int % 10000 == 0 {
32            Score((score_int as f64) / 100_000.0)
33        } else {
34            let score_floor = ((score_int as f64) / 10_000.0).floor();
35            Score((score_floor + 1.0) / 10.0)
36        }
37    }
38
39    /// Convert the numeric score into a `Severity`
40    pub fn severity(self) -> Severity {
41        if self.0 < 0.1 {
42            Severity::None
43        } else if self.0 < 4.0 {
44            Severity::Low
45        } else if self.0 < 7.0 {
46            Severity::Medium
47        } else if self.0 < 9.0 {
48            Severity::High
49        } else {
50            Severity::Critical
51        }
52    }
53}
54
55impl From<f64> for Score {
56    fn from(score: f64) -> Score {
57        Score(score)
58    }
59}
60
61impl From<Score> for f64 {
62    fn from(score: Score) -> f64 {
63        score.value()
64    }
65}
66
67impl From<Score> for Severity {
68    fn from(score: Score) -> Severity {
69        score.severity()
70    }
71}