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
use crate::units::{Error as RawError, Raw, Result as RawSensorResult};
use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialOrd, PartialEq, Hash)]
pub struct Accuracy(u8);
impl Accuracy {
pub fn from_percent(percent: u8) -> Self {
Self(percent)
}
pub fn as_percent(self) -> u8 {
self.0
}
}
impl Raw for Accuracy {
fn from_raw(raw: &str) -> RawSensorResult<Self> {
raw.trim()
.parse::<u8>()
.map(Accuracy::from_percent)
.map_err(|_| RawError::from(raw))
}
fn to_raw(&self) -> Cow<str> {
Cow::Owned(self.as_percent().to_string())
}
}
impl fmt::Display for Accuracy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}%", self.as_percent())
}
}
impl Eq for Accuracy {}
impl Ord for Accuracy {
fn cmp(&self, other: &Self) -> Ordering {
self.0.cmp(&other.0)
}
}