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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::fmt::{Debug, Display};

///! TODO: doc
use chrono;
use serde::{self, Deserialize, Serialize};

mod serde_impl;

/// conductivity in µS/cm
#[derive(Copy, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
pub struct Conductivity(f64);

impl Conductivity {
    pub fn from_us_per_cm(value: f64) -> Conductivity {
        value.into()
    }
    pub fn as_us_per_cm(&self) -> f64 {
        return self.0;
    }
}
impl From<f64> for Conductivity {
    fn from(value: f64) -> Self {
        Conductivity(value)
    }
}
impl Display for Conductivity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} µS/cm", self.0)
    }
}
impl Debug for Conductivity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}

/// temperature in °C
#[derive(Copy, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
pub struct Temperature(f64);

impl Temperature {
    pub fn from_celsius(value: f64) -> Temperature {
        value.into()
    }
    pub fn as_celsius(&self) -> f64 {
        return self.0;
    }
    pub fn as_kelvin(&self) -> f64 {
        return self.0 + 273.15;
    }
}
impl From<f64> for Temperature {
    fn from(value: f64) -> Self {
        Temperature(value)
    }
}
impl Display for Temperature {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} °C", self.0)
    }
}
impl Debug for Temperature {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}

/// measurement point
///
/// Both conductivity and temperature can be a successful or failed measurement, hence the `Option` types.
/// There is a success-guaranteed counterpart of this type [`SuccessfulMeasurement`]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Measurement {
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub conductivity: Option<Conductivity>,
    pub temperature: Option<Temperature>,
}

/// successful measurement point
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct SuccessfulMeasurement {
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub conductivity: Conductivity,
    pub temperature: Option<Temperature>,
}