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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum GpsFix {
    Old,
    Current,
}

impl From<bool> for GpsFix {
    fn from(bit: bool) -> Self {
        match bit {
            false => GpsFix::Old,
            true => GpsFix::Current,
        }
    }
}

impl From<GpsFix> for bool {
    fn from(fix: GpsFix) -> bool {
        match fix {
            GpsFix::Old => false,
            GpsFix::Current => true,
        }
    }
}

#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum NmeaSource {
    Other,
    Gll,
    Gga,
    Rmc,
}

impl From<(bool, bool)> for NmeaSource {
    fn from(bits: (bool, bool)) -> Self {
        match bits {
            (false, false) => NmeaSource::Other,
            (false, true) => NmeaSource::Gll,
            (true, false) => NmeaSource::Gga,
            (true, true) => NmeaSource::Rmc,
        }
    }
}

impl From<NmeaSource> for (bool, bool) {
    fn from(source: NmeaSource) -> Self {
        match source {
            NmeaSource::Other => (false, false),
            NmeaSource::Gll => (false, true),
            NmeaSource::Gga => (true, false),
            NmeaSource::Rmc => (true, true),
        }
    }
}

#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Origin {
    Compressed,
    TncBText,
    Software,
    Tbd,
    Kpc3,
    Pico,
    Other,
    Digipeater,
}

impl From<(bool, bool, bool)> for Origin {
    fn from(bits: (bool, bool, bool)) -> Self {
        match bits {
            (false, false, false) => Origin::Compressed,
            (false, false, true) => Origin::TncBText,
            (false, true, false) => Origin::Software,
            (false, true, true) => Origin::Tbd,
            (true, false, false) => Origin::Kpc3,
            (true, false, true) => Origin::Pico,
            (true, true, false) => Origin::Other,
            (true, true, true) => Origin::Digipeater,
        }
    }
}

impl From<Origin> for (bool, bool, bool) {
    fn from(origin: Origin) -> Self {
        match origin {
            Origin::Compressed => (false, false, false),
            Origin::TncBText => (false, false, true),
            Origin::Software => (false, true, false),
            Origin::Tbd => (false, true, true),
            Origin::Kpc3 => (true, false, false),
            Origin::Pico => (true, false, true),
            Origin::Other => (true, true, false),
            Origin::Digipeater => (true, true, true),
        }
    }
}

#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct AprsCompressionType {
    pub gps_fix: GpsFix,
    pub nmea_source: NmeaSource,
    pub origin: Origin,
}

impl From<u8> for AprsCompressionType {
    fn from(byte: u8) -> Self {
        let gps_fix = (byte & (1 << 5)) != 0;
        let nmea_source = (byte & (1 << 4) != 0, byte & (1 << 3) != 0);
        let origin = (
            byte & (1 << 2) != 0,
            byte & (1 << 1) != 0,
            byte & (1 << 0) != 0,
        );

        Self {
            gps_fix: gps_fix.into(),
            nmea_source: nmea_source.into(),
            origin: origin.into(),
        }
    }
}

impl From<AprsCompressionType> for u8 {
    fn from(t: AprsCompressionType) -> u8 {
        let b5: bool = t.gps_fix.into();
        let (b4, b3) = t.nmea_source.into();
        let (b2, b1, b0) = t.origin.into();

        (u8::from(b5) << 5)
            + (u8::from(b4) << 4)
            + (u8::from(b3) << 3)
            + (u8::from(b2) << 2)
            + (u8::from(b1) << 1)
            + (u8::from(b0))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn encode_works() {
        let byte: u8 = 0b00111010;
        let expected = AprsCompressionType {
            gps_fix: GpsFix::Current,
            nmea_source: NmeaSource::Rmc,
            origin: Origin::Software,
        };

        assert_eq!(expected, byte.into());
    }

    #[test]
    fn decode_works() {
        let expected: u8 = 0b00111010;
        let ctype = AprsCompressionType {
            gps_fix: GpsFix::Current,
            nmea_source: NmeaSource::Rmc,
            origin: Origin::Software,
        };

        assert_eq!(expected, ctype.into());
    }
}