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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct Status {
    map: HashMap<i64, String>,
    default_label: String,
}

impl Status {
    pub fn new(map: HashMap<i64, String>, default_label: String) -> Self {
        Self { map, default_label }
    }

    pub fn convert(&self, value: i64) -> String {
        self.map.get(&value).unwrap_or(&self.default_label).clone()
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Polynomial {
    a: [f64; 6],
}

impl Polynomial {
    pub fn new(a: [f64; 6]) -> Self {
        Self { a }
    }
}

impl Polynomial {
    pub fn convert(&self, x: f64) -> f64 {
        self.a
            .iter()
            .enumerate()
            .fold(0f64, |acc, (i, a)| acc + (a * x.powi(i as i32)))
    }
}

#[derive(Debug, Clone)]
pub enum Integral {
    Status(Status),
    Polynomial(Polynomial),
}

impl From<tlmcmddb::tlm::conversion::Status> for Status {
    fn from(db: tlmcmddb::tlm::conversion::Status) -> Self {
        let map = db.variants.into_iter().map(|v| (v.key, v.value)).collect();
        Self::new(map, db.default_value.unwrap_or_else(|| "OTHER".to_string()))
    }
}

impl From<tlmcmddb::tlm::conversion::Polynomial> for Polynomial {
    fn from(
        tlmcmddb::tlm::conversion::Polynomial {
            a0,
            a1,
            a2,
            a3,
            a4,
            a5,
        }: tlmcmddb::tlm::conversion::Polynomial,
    ) -> Self {
        Self::new([a0, a1, a2, a3, a4, a5])
    }
}