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
86
//! Module providing temperature sensor functionality.

use super::{FromSensorTemplate, SensorMetadataWithLocation, SensorTemplate, SensorTemplateError, Sensors};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
pub struct TemperatureSensor {
    #[serde(flatten)]
    pub metadata: SensorMetadataWithLocation,
    pub unit: String,
    pub value: f64,
}

#[derive(Debug, Clone)]
pub struct TemperatureSensorTemplate {
    pub metadata: SensorMetadataWithLocation,
    pub unit: String,
}

impl FromSensorTemplate<TemperatureSensorTemplate> for TemperatureSensor {
    fn try_from_template(
        template: &TemperatureSensorTemplate,
        value: &str,
    ) -> Result<Self, SensorTemplateError> {
        Ok(Self {
            metadata: template.metadata.clone(),
            unit: template.unit.clone(),
            value: value.parse()?,
        })
    }
}

impl SensorTemplate for TemperatureSensorTemplate {
    fn try_to_sensor(&self, value_str: &str, sensors: &mut Sensors) -> Result<(), SensorTemplateError> {
        sensors
            .temperature
            .push(TemperatureSensor::try_from_template(self, value_str)?);
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_template() {
        let template = TemperatureSensorTemplate {
            metadata: SensorMetadataWithLocation {
                location: "Main Room".into(),
                description: Some("Centre of main room on ground floor".into()),
                ..Default::default()
            },
            unit: "C".into(),
        };

        let mut sensors = Sensors::default();
        template.to_sensor("24.1", &mut sensors);

        assert_eq!(
            "[{\"location\":\"Main Room\",\"description\":\"Centre of main room on ground floor\",\"unit\":\"C\",\"value\":24.1}]",
            serde_json::to_string(&sensors.temperature).unwrap()
        );
    }

    #[test]
    fn test_template_bad_float() {
        let template = TemperatureSensorTemplate {
            metadata: SensorMetadataWithLocation {
                location: "Main Room".into(),
                description: Some("Centre of main room on ground floor".into()),
                ..Default::default()
            },
            unit: "C".into(),
        };

        let mut sensors = Sensors::default();
        let result = template.try_to_sensor("twenty four point one", &mut sensors);

        assert!(result.is_err());
        assert_eq!(
            "sensor float value cannot be parsed",
            result.err().unwrap().to_string()
        );
    }
}