aprs_encode/aprs/
header.rs

1use crate::{errors::PackError, stack_str::PackArrayString};
2
3/// Represents the header of an APRS packet
4#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
5pub struct AprsHeader<'a> {
6
7    /// Source callsign
8    pub src_call: &'a str,
9
10    /// Destination callsign
11    pub dest_call: &'a str,
12
13    /// Optional packet path specifier
14    pub path: Option<&'a [&'a str]>,
15}
16
17impl PackArrayString for AprsHeader<'_> {
18
19    /// Pack the header into an `ArrayString`
20    fn pack_into<const SIZE: usize>(
21        &self,
22        s: &mut arrayvec::ArrayString<SIZE>,
23    ) -> Result<(), PackError> {
24        // Push in the src and dest calls
25        s.try_push_str(self.src_call)?;
26        s.try_push('>')?;
27        s.try_push_str(self.dest_call)?;
28
29        // Handle optional path data
30        if self.path.is_some() {
31            // Push in all segments
32            for segment in self.path.as_ref().unwrap().iter() {
33                s.try_push(',')?;
34                s.try_push_str(*segment)?;
35            }
36        }
37
38        Ok(())
39    }
40}
41
42
43#[cfg(test)]
44mod tests {
45
46    use arrayvec::ArrayString;
47
48    use super::*;
49
50    #[test]
51    fn test_header_packing() {
52
53        // Build a buffer
54        let mut buffer = ArrayString::<128>::new();
55
56        // Add the packet
57        let header = AprsHeader{
58            src_call: "va3zza",
59            dest_call: "n0call",
60            path: Some(&["WIDE1-1", "APRSIS"])
61        };
62
63        // Pack
64        header.pack_into(&mut buffer).unwrap();
65
66        assert_eq!(*buffer, *"va3zza>n0call,WIDE1-1,APRSIS");
67
68    }
69}