aprs_parser/
via.rs

1use std::io::{self, Write};
2
3use Callsign;
4
5#[derive(Eq, PartialEq, Clone, Debug)]
6pub enum Via {
7    Callsign(Callsign, bool),
8    QConstruct(QConstruct),
9}
10
11impl Via {
12    pub fn decode_textual(bytes: &[u8]) -> Option<Self> {
13        if let Some(q) = QConstruct::decode_textual(bytes) {
14            return Some(Self::QConstruct(q));
15        }
16
17        if let Some((c, heard)) = Callsign::decode_textual(bytes) {
18            return Some(Self::Callsign(c, heard));
19        }
20
21        None
22    }
23
24    pub fn encode_textual<W: Write>(&self, w: &mut W) -> io::Result<()> {
25        match self {
26            Self::Callsign(c, heard) => {
27                c.encode_textual(*heard, w)?;
28            }
29            Self::QConstruct(q) => {
30                write!(w, "{}", q.as_textual())?;
31            }
32        }
33
34        Ok(())
35    }
36
37    pub fn callsign(&self) -> Option<(&Callsign, bool)> {
38        match self {
39            Self::Callsign(c, heard) => Some((c, *heard)),
40            Self::QConstruct(_) => None,
41        }
42    }
43
44    pub fn callsign_mut(&mut self) -> Option<(&mut Callsign, &mut bool)> {
45        match self {
46            Self::Callsign(c, heard) => Some((c, heard)),
47            Self::QConstruct(_) => None,
48        }
49    }
50}
51
52// Can't be encoded/decoded as ax.25
53// These should never go on the air
54#[derive(PartialEq, Eq, Copy, Clone, Debug)]
55pub enum QConstruct {
56    AC,
57    AX,
58    AU,
59    Ao,
60    AO,
61    AS,
62    Ar,
63    AR,
64    AZ,
65    AI,
66}
67
68impl QConstruct {
69    pub fn decode_textual(bytes: &[u8]) -> Option<Self> {
70        let q = match bytes {
71            b"qAC" => QConstruct::AC,
72            b"qAX" => QConstruct::AX,
73            b"qAU" => QConstruct::AU,
74            b"qAo" => QConstruct::Ao,
75            b"qAO" => QConstruct::AO,
76            b"qAS" => QConstruct::AS,
77            b"qAr" => QConstruct::Ar,
78            b"qAR" => QConstruct::AR,
79            b"qAZ" => QConstruct::AZ,
80            b"qAI" => QConstruct::AI,
81            _ => return None,
82        };
83
84        Some(q)
85    }
86
87    pub fn as_textual(&self) -> &'static str {
88        match self {
89            QConstruct::AC => "qAC",
90            QConstruct::AX => "qAX",
91            QConstruct::AU => "qAU",
92            QConstruct::Ao => "qAo",
93            QConstruct::AO => "qAO",
94            QConstruct::AS => "qAS",
95            QConstruct::Ar => "qAr",
96            QConstruct::AR => "qAR",
97            QConstruct::AZ => "qAZ",
98            QConstruct::AI => "qAI",
99        }
100    }
101}