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
use crate::{errors::PackError, stack_str::PackArrayString};
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub struct AprsWx {
wind_direction_degrees: Option<u8>,
wind_speed_mph: Option<u8>,
temperature_f: Option<i8>,
}
impl PackArrayString for AprsWx {
fn pack_into<const SIZE: usize>(
&self,
s: &mut arrayvec::ArrayString<SIZE>,
) -> Result<(), PackError> {
if self.wind_direction_degrees.is_some() {
s.try_push('_')?;
let direction = self.wind_direction_degrees.as_ref().unwrap();
s.try_push(('0' as u8 + ((direction / 100) % 10) as u8) as char)?;
s.try_push(('0' as u8 + ((direction / 10) % 10) as u8) as char)?;
s.try_push(('0' as u8 + (direction % 10) as u8) as char)?;
}
if self.wind_speed_mph.is_some() {
s.try_push('/')?;
let speed = self.wind_speed_mph.as_ref().unwrap();
s.try_push(('0' as u8 + ((speed / 100) % 10) as u8) as char)?;
s.try_push(('0' as u8 + ((speed / 10) % 10) as u8) as char)?;
s.try_push(('0' as u8 + (speed % 10) as u8) as char)?;
}
if self.temperature_f.is_some() {
s.try_push('t')?;
let temp = self.temperature_f.as_ref().unwrap();
s.try_push(('0' as u8 + ((temp / 100) % 10) as u8) as char)?;
s.try_push(('0' as u8 + ((temp / 10) % 10) as u8) as char)?;
s.try_push(('0' as u8 + (temp % 10) as u8) as char)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use arrayvec::ArrayString;
use super::*;
#[test]
fn test_wx_packing() {
let mut buffer = ArrayString::<128>::new();
let data = AprsWx {
wind_speed_mph: Some(10),
wind_direction_degrees: Some(35),
temperature_f: Some(65),
};
data.pack_into(&mut buffer).unwrap();
assert_eq!(*buffer, *"_035/010t065");
}
}