redgold_schema/
trust.rs

1use crate::structs::{TrustData, Weighting};
2
3/// Level of precision here is 3 digits. I.e. review rating of 90.5 is 905.
4const DEFAULT_TRUST_DIVISOR: f64 = 1000.0;
5
6pub trait FloatRoundedConvert {
7    fn to_rounded_i64(&self) -> i64;
8}
9
10pub trait FloatRoundedConverti64 {
11    fn to_rounded_f64(&self) -> f64;
12}
13
14impl FloatRoundedConvert for f64 {
15    fn to_rounded_i64(&self) -> i64 {
16        (*self * DEFAULT_TRUST_DIVISOR) as i64
17    }
18}
19
20impl FloatRoundedConverti64 for i64 {
21    fn to_rounded_f64(&self) -> f64 {
22        (*self as f64) / DEFAULT_TRUST_DIVISOR
23    }
24}
25
26impl TrustData {
27    pub fn with_label(&mut self, label: f64) -> &mut Self {
28        let weighting = Weighting::from_float_basis(label, DEFAULT_TRUST_DIVISOR as i64);
29        self.label_rating = Some(weighting);
30        self
31    }
32    pub fn label(&self) -> f64 {
33        self.maybe_label().expect("label is not set")
34    }
35    pub fn maybe_label(&self) -> Option<f64> {
36        self.label_rating.clone().map(|l| l.to_float())
37    }
38
39    pub fn from_label(label: f64) -> Self {
40        let mut t = TrustData::default();
41        t.with_label(label);
42        t
43    }
44}