aprs_encode/aprs/
position.rs

1use crate::{ddm::{DdmLatitude, DdmLongitude}, errors::PackError, stack_str::PackArrayString};
2
3/// Represents the global position of an APRS packet
4#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
5pub struct AprsPosition {
6    pub longitude: DdmLongitude,
7    pub latitude: DdmLatitude,
8}
9
10impl AprsPosition {
11    pub fn new(latitude: DdmLatitude, longitude: DdmLongitude) -> Self {
12        Self {
13            longitude,
14            latitude,
15        }
16    }
17
18
19}
20
21impl PackArrayString for AprsPosition {
22    fn pack_into<const SIZE: usize>(
23        &self,
24        s: &mut arrayvec::ArrayString<SIZE>,
25    ) -> Result<(), PackError> {
26        
27        // pack data
28        self.latitude.pack_into(s)?;
29        s.try_push('/')?;
30        self.longitude.pack_into(s)?;
31        
32        Ok(())
33    }
34}
35
36#[cfg(test)]
37mod tests {
38
39    use arrayvec::ArrayString;
40
41    use super::*;
42
43    #[test]
44    fn test_position_packing() {
45
46        // Build a buffer
47        let mut buffer = ArrayString::<128>::new();
48
49        // Add the packet
50        let data = AprsPosition {
51            latitude: 42.981312.into(),
52            longitude: (-81.257472).into()
53        };
54
55        // Pack
56        data.pack_into(&mut buffer).unwrap();
57
58        assert_eq!(*buffer, *"4258.87N/08115.44W");
59
60    }
61}