Skip to main content

hap_model/
unit.rs

1//! HAP characteristic units (the spec `unit` field).
2
3/// A unit a HAP characteristic value can carry.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum Unit {
7    /// `celsius`
8    Celsius,
9    /// `percentage`
10    Percentage,
11    /// `arcdegrees`
12    ArcDegrees,
13    /// `lux`
14    Lux,
15    /// `seconds`
16    Seconds,
17    /// `ppm`
18    Ppm,
19    /// `micrograms/m^3`
20    MicrogramsPerM3,
21}
22
23impl Unit {
24    /// The HAP wire spelling of this unit.
25    pub fn as_str(self) -> &'static str {
26        match self {
27            Unit::Celsius => "celsius",
28            Unit::Percentage => "percentage",
29            Unit::ArcDegrees => "arcdegrees",
30            Unit::Lux => "lux",
31            Unit::Seconds => "seconds",
32            Unit::Ppm => "ppm",
33            Unit::MicrogramsPerM3 => "micrograms/m^3",
34        }
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::Unit;
41
42    #[test]
43    fn wire_spelling() {
44        assert_eq!(Unit::Celsius.as_str(), "celsius");
45        assert_eq!(Unit::MicrogramsPerM3.as_str(), "micrograms/m^3");
46    }
47}