| 1 |
|
| 2 |
|
| 3 |
use crate::severity::Severity; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd)] |
| 10 |
pub struct Score(f64); |
| 11 |
|
| 12 |
impl Score { |
| 13 |
|
| 14 |
pub fn new(score: f64) -> Score { |
| 15 |
Score(score) |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
pub fn value(self) -> f64 { |
| 20 |
self.0 |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 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 |
|
| 55 |
impl From<f64> for Score { |
| 56 |
fn from(score: f64) -> Score { |
| 57 |
Score(score) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
impl From<Score> for f64 { |
| 62 |
fn from(score: Score) -> f64 { |
| 63 |
score.value() |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl From<Score> for Severity { |
| 68 |
fn from(score: Score) -> Severity { |
| 69 |
score.severity() |
| 70 |
} |
| 71 |
} |