1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#[derive(Clone)]
pub struct Quantile(f64, String);
impl Quantile {
pub fn new(quantile: f64) -> Quantile {
let clamped = quantile.max(0.0);
let clamped = clamped.min(1.0);
let display = clamped * 100.0;
let raw_label = format!("{}", clamped);
let label = match raw_label.as_str() {
"0" => "min".to_string(),
"1" => "max".to_string(),
_ => {
let raw = format!("p{}", display);
raw.replace(".", "")
},
};
Quantile(clamped, label)
}
pub fn label(&self) -> &str {
self.1.as_str()
}
pub fn value(&self) -> f64 {
self.0
}
}
pub fn parse_quantiles(quantiles: &[f64]) -> Vec<Quantile> {
quantiles.iter()
.map(|f| Quantile::new(*f))
.collect()
}