libaprs-engine 1.0.0-rc.1

Protocol-first APRS engine core primitives
Documentation
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
#![forbid(unsafe_code)]

//! Protocol-first APRS engine core primitives.
//!
//! The codec boundary accepts untrusted bytes, preserves them exactly, and
//! fails closed when the packet shape is malformed.

mod diagnostic;
mod transport;

#[cfg(feature = "serde")]
pub mod serde_support;

pub use transport::{
    oversized_input_error, read_all_with_limit, LineTransport, PacketSink, PacketSource,
    TransportErrorCode, DEFAULT_TRANSPORT_READ_LIMIT,
};

/// Conservative upper bound for an APRS packet handled by this skeleton.
pub const MAX_PACKET_LEN: usize = 512;

/// Default parse options used by [`parse_packet`].
pub const DEFAULT_PARSE_OPTIONS: ParseOptions = ParseOptions {
    max_packet_len: MAX_PACKET_LEN,
};

/// Codec configuration for consumers that need a different envelope limit.
///
/// The parser remains fail-closed regardless of this setting. This value only
/// changes the maximum accepted packet length.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ParseOptions {
    /// Maximum accepted packet length in bytes.
    pub max_packet_len: usize,
}

impl ParseOptions {
    /// Creates parse options with a custom maximum packet length.
    #[must_use]
    pub const fn new(max_packet_len: usize) -> Self {
        Self { max_packet_len }
    }
}

impl Default for ParseOptions {
    fn default() -> Self {
        DEFAULT_PARSE_OPTIONS
    }
}

/// Original packet bytes retained without normalization or lossy conversion.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RawPacket {
    bytes: Vec<u8>,
}

impl RawPacket {
    /// Returns the original packet bytes exactly as supplied to the parser.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }
}

/// Structured packet view backed by the preserved raw packet bytes.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParsedPacket {
    raw: RawPacket,
    source_end: usize,
    path_start: usize,
    path_end: usize,
    path_components: Vec<(usize, usize)>,
    payload_start: usize,
}

impl ParsedPacket {
    /// Returns the preserved raw packet.
    #[must_use]
    pub fn raw(&self) -> &RawPacket {
        &self.raw
    }

    /// Returns the source callsign bytes before the `>` separator.
    #[must_use]
    pub fn source(&self) -> &[u8] {
        &self.raw.bytes[..self.source_end]
    }

    /// Returns the destination/path bytes between `>` and `:`.
    #[must_use]
    pub fn path(&self) -> &[u8] {
        &self.raw.bytes[self.path_start..self.path_end]
    }

    /// Returns the destination bytes, which are the first path component.
    #[must_use]
    pub fn destination(&self) -> &[u8] {
        let (start, end) = self.path_components[0];
        &self.raw.bytes[start..end]
    }

    /// Returns digipeater path component byte views after the destination.
    #[must_use]
    pub fn digipeaters(&self) -> Vec<&[u8]> {
        self.path_components[1..]
            .iter()
            .map(|(start, end)| &self.raw.bytes[*start..*end])
            .collect()
    }

    /// Returns all path component byte views, including destination first.
    #[must_use]
    pub fn path_components(&self) -> Vec<&[u8]> {
        self.path_components
            .iter()
            .map(|(start, end)| &self.raw.bytes[*start..*end])
            .collect()
    }

    /// Returns the payload bytes after the `:` separator.
    #[must_use]
    pub fn payload(&self) -> &[u8] {
        &self.raw.bytes[self.payload_start..]
    }

    /// Returns the APRS data type identifier from the first payload byte.
    #[must_use]
    pub fn data_type_identifier(&self) -> DataTypeIdentifier {
        DataTypeIdentifier::from_byte(self.raw.bytes[self.payload_start])
    }

    /// Returns payload bytes after the data type identifier.
    #[must_use]
    pub fn information(&self) -> &[u8] {
        &self.raw.bytes[self.payload_start + 1..]
    }

    /// Returns a semantic view of the APRS information field.
    #[must_use]
    pub fn aprs_data(&self) -> AprsData<'_> {
        parse_aprs_data(
            self.data_type_identifier(),
            self.information(),
            self.destination(),
        )
    }

    /// Returns a structured diagnostic summary for observability.
    #[must_use]
    pub fn summary(&self) -> PacketSummary<'_> {
        PacketSummary::from_packet(self)
    }

    /// Serializes the parsed packet into a compact JSON diagnostic string.
    #[must_use]
    pub fn to_json(&self) -> String {
        diagnostic::packet_to_json(self)
    }
}

/// Structured packet diagnostic summary.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PacketSummary<'a> {
    /// Source address bytes.
    pub source: &'a [u8],
    /// Destination address bytes.
    pub destination: &'a [u8],
    /// APRS data type identifier name.
    pub data_type: &'static str,
    /// APRS semantic kind name.
    pub semantic: &'static str,
    /// Decoded coordinates when the semantic family supports them.
    pub coordinates: Option<Coordinates>,
    /// NMEA checksum details when present.
    pub nmea_checksum: Option<NmeaChecksum>,
    /// Telemetry sequence number when present and numeric.
    pub telemetry_sequence: Option<u16>,
    /// Mic-E speed/course details when present and decodable.
    pub mic_e_speed_course: Option<MicESpeedCourse>,
}

impl<'a> PacketSummary<'a> {
    fn from_packet(packet: &'a ParsedPacket) -> Self {
        let data = packet.aprs_data();
        Self {
            source: packet.source(),
            destination: packet.destination(),
            data_type: packet.data_type_identifier().name(),
            semantic: data.kind_name(),
            coordinates: summary_coordinates(data),
            nmea_checksum: summary_nmea_checksum(data),
            telemetry_sequence: summary_telemetry_sequence(data),
            mic_e_speed_course: summary_mic_e_speed_course(data),
        }
    }
}

/// Parser and policy orchestration engine.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Engine {
    policy: Policy,
    counters: Counters,
}

impl Engine {
    /// Creates an engine with the provided policy.
    #[must_use]
    pub fn new(policy: Policy) -> Self {
        Self {
            policy,
            counters: Counters::default(),
        }
    }

    /// Processes one packet through codec, semantics, and policy.
    pub fn process(&mut self, input: &[u8]) -> EngineResult {
        match parse_packet(input) {
            Ok(packet) => {
                let semantic = packet.aprs_data();
                match self.policy.evaluate(&packet, &semantic) {
                    PolicyDecision::Accept => {
                        self.counters.accepted = self.counters.accepted.saturating_add(1);
                        EngineResult::Accepted { packet }
                    }
                    PolicyDecision::Reject(reason) => {
                        self.counters.rejected = self.counters.rejected.saturating_add(1);
                        EngineResult::Rejected { packet, reason }
                    }
                }
            }
            Err(error) => {
                self.counters.malformed = self.counters.malformed.saturating_add(1);
                EngineResult::ParseError(error)
            }
        }
    }

    /// Processes a caller-provided packet batch in order.
    pub fn process_packets<I, P>(&mut self, packets: I) -> Vec<EngineResult>
    where
        I: IntoIterator<Item = P>,
        P: AsRef<[u8]>,
    {
        packets
            .into_iter()
            .map(|packet| self.process(packet.as_ref()))
            .collect()
    }

    /// Reads one bounded batch from a packet source and processes it in order.
    pub fn process_source<S>(&mut self, source: &mut S) -> Result<Vec<EngineResult>, S::Error>
    where
        S: PacketSource,
    {
        Ok(self.process_packets(source.recv_packets()?))
    }

    /// Returns engine counters.
    #[must_use]
    pub fn counters(&self) -> Counters {
        self.counters
    }
}

impl Default for Engine {
    fn default() -> Self {
        Self::new(Policy::default())
    }
}

/// Engine processing result.
#[derive(Clone, Debug, PartialEq)]
pub enum EngineResult {
    /// Packet parsed and passed policy.
    Accepted {
        /// Parsed packet.
        packet: ParsedPacket,
    },
    /// Packet parsed but failed policy.
    Rejected {
        /// Parsed packet.
        packet: ParsedPacket,
        /// Rejection reason.
        reason: PolicyRejection,
    },
    /// Packet failed the codec boundary.
    ParseError(ParseError),
}

/// Runtime counters.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Counters {
    /// Accepted packet count.
    pub accepted: u64,
    /// Policy-rejected packet count.
    pub rejected: u64,
    /// Codec-malformed packet count.
    pub malformed: u64,
}

/// Policy options applied after parsing.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Policy {
    /// Allow semantic packets represented as unsupported.
    pub allow_unsupported: bool,
    /// Allow semantic packets represented as malformed.
    pub allow_malformed_semantics: bool,
    /// Reject NMEA sentences when a present checksum does not match.
    pub reject_invalid_nmea_checksum: bool,
    /// Maximum allowed path component count including destination.
    pub max_path_components: usize,
}

impl Policy {
    /// Strict policy: reject malformed semantics, unsupported formats, and long paths.
    #[must_use]
    pub fn strict() -> Self {
        Self::default()
    }

    /// Permissive policy: accept unsupported and malformed semantic packets.
    #[must_use]
    pub fn permissive() -> Self {
        Self {
            allow_unsupported: true,
            allow_malformed_semantics: true,
            reject_invalid_nmea_checksum: false,
            max_path_components: 9,
        }
    }

    /// Evaluates a parsed packet and semantic view.
    #[must_use]
    pub fn evaluate(&self, packet: &ParsedPacket, semantic: &AprsData<'_>) -> PolicyDecision {
        if packet.path_components.len() > self.max_path_components {
            return PolicyDecision::Reject(PolicyRejection::PathTooLong);
        }

        if self.reject_invalid_nmea_checksum
            && matches!(
                semantic,
                AprsData::Nmea(nmea) if nmea.checksum().is_some_and(|checksum| !checksum.valid)
            )
        {
            return PolicyDecision::Reject(PolicyRejection::InvalidNmeaChecksum);
        }

        match semantic {
            AprsData::Malformed { .. } if !self.allow_malformed_semantics => {
                PolicyDecision::Reject(PolicyRejection::MalformedSemantics)
            }
            AprsData::Unsupported { .. } if !self.allow_unsupported => {
                PolicyDecision::Reject(PolicyRejection::UnsupportedSemantics)
            }
            _ => PolicyDecision::Accept,
        }
    }
}

impl Default for Policy {
    fn default() -> Self {
        Self {
            allow_unsupported: false,
            allow_malformed_semantics: false,
            reject_invalid_nmea_checksum: false,
            max_path_components: 9,
        }
    }
}

/// Policy decision.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PolicyDecision {
    /// Packet is accepted.
    Accept,
    /// Packet is rejected with a reason.
    Reject(PolicyRejection),
}

/// Policy rejection reason.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PolicyRejection {
    /// Path contains too many components.
    PathTooLong,
    /// Semantic payload is malformed.
    MalformedSemantics,
    /// Semantic payload is unsupported.
    UnsupportedSemantics,
    /// NMEA sentence has a present checksum that does not match.
    InvalidNmeaChecksum,
}

impl PolicyRejection {
    /// Returns a stable policy rejection code for logs and external systems.
    #[must_use]
    pub fn code(self) -> &'static str {
        match self {
            Self::PathTooLong => "policy.path_too_long",
            Self::MalformedSemantics => "policy.malformed_semantics",
            Self::UnsupportedSemantics => "policy.unsupported_semantics",
            Self::InvalidNmeaChecksum => "policy.nmea_checksum_mismatch",
        }
    }
}

/// Semantic APRS information-field data.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AprsData<'a> {
    /// Status report.
    Status {
        /// Status text bytes.
        text: &'a [u8],
    },
    /// Uncompressed position report.
    Position(Position<'a>),
    /// Timestamped uncompressed position report.
    TimestampedPosition(TimestampedPosition<'a>),
    /// Compressed position report.
    CompressedPosition(CompressedPosition<'a>),
    /// Message, bulletin, or announcement.
    Message(Message<'a>),
    /// Object report.
    Object(Object<'a>),
    /// Item report.
    Item(Item<'a>),
    /// Weather report without position.
    Weather(Weather<'a>),
    /// Telemetry report.
    Telemetry(Telemetry<'a>),
    /// Telemetry metadata carried as an APRS message.
    TelemetryMetadata(TelemetryMetadata<'a>),
    /// Query packet.
    Query(Query<'a>),
    /// Station capabilities packet.
    Capability(Capability<'a>),
    /// NMEA sentence packet.
    Nmea(Nmea<'a>),
    /// Mic-E packet.
    MicE(MicE<'a>),
    /// Maidenhead locator packet.
    Maidenhead(Maidenhead<'a>),
    /// User-defined data packet.
    UserDefined(UserDefined<'a>),
    /// Third-party traffic packet.
    ThirdParty(ThirdParty<'a>),
    /// Data format is validly framed but not implemented yet.
    Unsupported {
        /// Original data type identifier byte.
        identifier: u8,
        /// Remaining information-field bytes.
        information: &'a [u8],
    },
    /// Data type is known, but its information bytes are malformed.
    Malformed {
        /// Original data type identifier byte.
        identifier: u8,
        /// Remaining information-field bytes.
        information: &'a [u8],
    },
}

impl AprsData<'_> {
    /// Returns a stable semantic kind name for diagnostics.
    #[must_use]
    pub fn kind_name(&self) -> &'static str {
        match self {
            Self::Status { .. } => "status",
            Self::Position(_) => "position",
            Self::TimestampedPosition(_) => "timestamped_position",
            Self::CompressedPosition(_) => "compressed_position",
            Self::Message(_) => "message",
            Self::Object(_) => "object",
            Self::Item(_) => "item",
            Self::Weather(_) => "weather",
            Self::Telemetry(_) => "telemetry",
            Self::TelemetryMetadata(_) => "telemetry_metadata",
            Self::Query(_) => "query",
            Self::Capability(_) => "capability",
            Self::Nmea(_) => "nmea",
            Self::MicE(_) => "mic_e",
            Self::Maidenhead(_) => "maidenhead",
            Self::UserDefined(_) => "user_defined",
            Self::ThirdParty(_) => "third_party",
            Self::Unsupported { .. } => "unsupported",
            Self::Malformed { .. } => "malformed",
        }
    }
}

fn summary_coordinates(data: AprsData<'_>) -> Option<Coordinates> {
    match data {
        AprsData::Position(position) => position.coordinates(),
        AprsData::TimestampedPosition(position) => position.position.coordinates(),
        AprsData::CompressedPosition(position) => position.coordinates(),
        AprsData::MicE(mic_e) => mic_e.coordinates(),
        _ => None,
    }
}

fn summary_nmea_checksum(data: AprsData<'_>) -> Option<NmeaChecksum> {
    match data {
        AprsData::Nmea(nmea) => nmea.checksum(),
        _ => None,
    }
}

fn summary_telemetry_sequence(data: AprsData<'_>) -> Option<u16> {
    match data {
        AprsData::Telemetry(telemetry) => telemetry.sequence_number(),
        _ => None,
    }
}

fn summary_mic_e_speed_course(data: AprsData<'_>) -> Option<MicESpeedCourse> {
    match data {
        AprsData::MicE(mic_e) => mic_e.speed_course(),
        _ => None,
    }
}

/// Uncompressed APRS position fields.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Position<'a> {
    /// Whether the data type identifier indicates APRS messaging support.
    pub messaging: bool,
    /// Latitude bytes in APRS `DDMM.mmN/S` form.
    pub latitude: &'a [u8],
    /// Symbol table identifier byte.
    pub symbol_table: u8,
    /// Longitude bytes in APRS `DDDMM.mmE/W` form.
    pub longitude: &'a [u8],
    /// Symbol code byte.
    pub symbol_code: u8,
    /// Optional comment bytes after the symbol code.
    pub comment: &'a [u8],
}

impl Position<'_> {
    /// Returns decimal latitude and longitude if both coordinate fields decode.
    #[must_use]
    pub fn coordinates(&self) -> Option<Coordinates> {
        Some(Coordinates {
            latitude: decode_latitude(self.latitude)?,
            longitude: decode_longitude(self.longitude)?,
        })
    }
}

/// Decimal coordinates in signed degrees.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Coordinates {
    /// Latitude in signed decimal degrees.
    pub latitude: f64,
    /// Longitude in signed decimal degrees.
    pub longitude: f64,
}

/// Timestamped uncompressed APRS position fields.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TimestampedPosition<'a> {
    /// Whether the data type identifier indicates APRS messaging support.
    pub messaging: bool,
    /// Seven-byte timestamp field.
    pub timestamp: &'a [u8],
    /// Position fields after the timestamp.
    pub position: Position<'a>,
}

/// Compressed APRS position fields.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CompressedPosition<'a> {
    /// Whether the data type identifier indicates APRS messaging support.
    pub messaging: bool,
    /// Symbol table identifier byte.
    pub symbol_table: u8,
    /// Four-byte compressed latitude.
    pub compressed_latitude: &'a [u8],
    /// Four-byte compressed longitude.
    pub compressed_longitude: &'a [u8],
    /// Symbol code byte.
    pub symbol_code: u8,
    /// Two-byte compressed extension field.
    pub extension: &'a [u8],
    /// Compression type byte.
    pub compression_type: u8,
    /// Optional comment bytes after the compression type byte.
    pub comment: &'a [u8],
}

impl CompressedPosition<'_> {
    /// Returns decoded compressed-position coordinates.
    #[must_use]
    pub fn coordinates(&self) -> Option<Coordinates> {
        let y = decode_base91(self.compressed_latitude)?;
        let x = decode_base91(self.compressed_longitude)?;

        Some(Coordinates {
            latitude: 90.0 - (y as f64 / 380_926.0),
            longitude: -180.0 + (x as f64 / 190_463.0),
        })
    }
}

/// APRS message fields.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Message<'a> {
    /// Nine-byte addressee field.
    pub addressee: &'a [u8],
    /// Classified message subtype.
    pub kind: MessageKind,
    /// Message text bytes before an optional message ID.
    pub text: &'a [u8],
    /// Optional message ID bytes after `{`.
    pub id: Option<&'a [u8]>,
}

/// APRS message subtype.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MessageKind {
    /// Regular addressed message.
    Message,
    /// Message acknowledgement.
    Ack,
    /// Message rejection.
    Reject,
    /// Bulletin.
    Bulletin,
    /// Announcement.
    Announcement,
}

/// APRS object report fields.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Object<'a> {
    /// Nine-byte object name.
    pub name: &'a [u8],
    /// Whether the object is live (`*`) rather than killed (`_`).
    pub live: bool,
    /// Seven-byte object timestamp.
    pub timestamp: &'a [u8],
    /// Remaining object body bytes.
    pub body: &'a [u8],
}

impl Object<'_> {
    /// Returns object coordinates when the object body starts with a supported
    /// APRS position encoding.
    #[must_use]
    pub fn coordinates(&self) -> Option<Coordinates> {
        coordinates_from_position_body(self.body)
    }
}

/// APRS item report fields.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Item<'a> {
    /// Item name bytes.
    pub name: &'a [u8],
    /// Whether the item is live (`!`) rather than killed (`_`).
    pub live: bool,
    /// Remaining item body bytes.
    pub body: &'a [u8],
}

impl Item<'_> {
    /// Returns item coordinates when the item body starts with a supported APRS
    /// position encoding.
    #[must_use]
    pub fn coordinates(&self) -> Option<Coordinates> {
        coordinates_from_position_body(self.body)
    }
}

/// APRS weather report bytes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Weather<'a> {
    /// Weather report bytes after the `_` data type identifier.
    pub report: &'a [u8],
}

impl Weather<'_> {
    /// Extracts common numeric weather fields when present.
    #[must_use]
    pub fn fields(&self) -> WeatherFields<'_> {
        WeatherFields {
            timestamp: self
                .report
                .get(..6)
                .filter(|value| value.iter().all(u8::is_ascii_digit)),
            wind_direction_degrees: parse_tagged_u16(self.report, b'c', 3),
            wind_speed_mph: parse_tagged_u16(self.report, b's', 3),
            wind_gust_mph: parse_tagged_u16(self.report, b'g', 3),
            temperature_fahrenheit: parse_tagged_i16(self.report, b't', 3),
            rain_last_hour_hundredths_inch: parse_tagged_u16(self.report, b'r', 3),
            rain_last_24_hours_hundredths_inch: parse_tagged_u16(self.report, b'p', 3),
            rain_since_midnight_hundredths_inch: parse_tagged_u16(self.report, b'P', 3),
            humidity_percent: parse_tagged_u16(self.report, b'h', 2).map(|value| {
                if value == 0 {
                    100
                } else {
                    value
                }
            }),
            pressure_tenths_hpa: parse_tagged_u16(self.report, b'b', 5),
            luminosity_watts_per_square_meter: parse_tagged_u16(self.report, b'L', 3),
            luminosity_1000_plus_watts_per_square_meter: parse_tagged_u16(self.report, b'l', 3)
                .map(|value| value + 1000),
            snow_last_24_hours_inches: parse_tagged_u16(self.report, b'S', 3),
            raw_rain_counter: parse_tagged_u16(self.report, b'#', 3),
        }
    }
}

/// Extracted numeric weather fields.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct WeatherFields<'a> {
    /// Optional six-byte timestamp prefix.
    pub timestamp: Option<&'a [u8]>,
    /// Wind direction in degrees.
    pub wind_direction_degrees: Option<u16>,
    /// Sustained wind speed in miles per hour.
    pub wind_speed_mph: Option<u16>,
    /// Wind gust speed in miles per hour.
    pub wind_gust_mph: Option<u16>,
    /// Temperature in degrees Fahrenheit.
    pub temperature_fahrenheit: Option<i16>,
    /// Rain in the last hour, in hundredths of an inch.
    pub rain_last_hour_hundredths_inch: Option<u16>,
    /// Rain in the last 24 hours, in hundredths of an inch.
    pub rain_last_24_hours_hundredths_inch: Option<u16>,
    /// Rain since midnight, in hundredths of an inch.
    pub rain_since_midnight_hundredths_inch: Option<u16>,
    /// Relative humidity percent.
    pub humidity_percent: Option<u16>,
    /// Barometric pressure in tenths of hPa.
    pub pressure_tenths_hpa: Option<u16>,
    /// Luminosity in watts per square meter from `Lnnn`.
    pub luminosity_watts_per_square_meter: Option<u16>,
    /// Luminosity in watts per square meter from `lnnn`, representing 1000+.
    pub luminosity_1000_plus_watts_per_square_meter: Option<u16>,
    /// Snowfall in the last 24 hours, in inches.
    pub snow_last_24_hours_inches: Option<u16>,
    /// Raw rain counter value from `#nnn`.
    pub raw_rain_counter: Option<u16>,
}

/// APRS telemetry report fields.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Telemetry<'a> {
    /// Telemetry sequence bytes.
    pub sequence: &'a [u8],
    /// Five analog telemetry value fields.
    pub analog: [&'a [u8]; 5],
    /// Optional eight-bit digital telemetry field.
    pub digital: Option<&'a [u8]>,
}

impl Telemetry<'_> {
    /// Returns the numeric telemetry sequence number.
    #[must_use]
    pub fn sequence_number(&self) -> Option<u16> {
        parse_u16(self.sequence)
    }

    /// Returns the five numeric analog telemetry values.
    #[must_use]
    pub fn analog_values(&self) -> Option<[u16; 5]> {
        Some([
            parse_u16(self.analog[0])?,
            parse_u16(self.analog[1])?,
            parse_u16(self.analog[2])?,
            parse_u16(self.analog[3])?,
            parse_u16(self.analog[4])?,
        ])
    }

    /// Returns eight digital telemetry bits.
    #[must_use]
    pub fn digital_bits(&self) -> Option<[bool; 8]> {
        let digital = self.digital?;
        if digital.len() != 8 {
            return None;
        }

        let mut bits = [false; 8];
        for (index, byte) in digital.iter().enumerate() {
            bits[index] = match byte {
                b'0' => false,
                b'1' => true,
                _ => return None,
            };
        }

        Some(bits)
    }
}

/// APRS telemetry metadata packet carried in an APRS message.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TelemetryMetadata<'a> {
    /// Nine-byte telemetry metadata addressee field.
    pub addressee: &'a [u8],
    /// Classified telemetry metadata subtype.
    pub kind: TelemetryMetadataKind,
    /// Metadata body bytes after the message separator.
    pub body: &'a [u8],
}

impl<'a> TelemetryMetadata<'a> {
    /// Returns comma-separated metadata fields without lossy conversion.
    #[must_use]
    pub fn fields(&self) -> Vec<&'a [u8]> {
        self.body.split(|byte| *byte == b',').collect()
    }
}

/// APRS telemetry metadata subtype.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TelemetryMetadataKind {
    /// `PARM.` parameter-name metadata.
    ParameterNames,
    /// `UNIT.` unit metadata.
    Units,
    /// `EQNS.` calibration/equation metadata.
    Equations,
    /// `BITS.` bit-sense/project metadata.
    BitSense,
}

/// APRS query packet bytes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Query<'a> {
    /// Query bytes after the `?` data type identifier.
    pub query: &'a [u8],
}

/// APRS station capabilities packet bytes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Capability<'a> {
    /// Capability body bytes after the `<` data type identifier.
    pub body: &'a [u8],
}

/// APRS NMEA packet bytes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Nmea<'a> {
    /// NMEA sentence bytes after the `$` data type identifier.
    pub sentence: &'a [u8],
}

impl Nmea<'_> {
    /// Returns the NMEA talker ID from the sentence address field.
    #[must_use]
    pub fn talker_id(&self) -> Option<&[u8]> {
        let address = self.address_field()?;
        (address.len() >= 2).then_some(&address[..2])
    }

    /// Returns the NMEA sentence formatter ID from the sentence address field.
    #[must_use]
    pub fn sentence_id(&self) -> Option<&[u8]> {
        let address = self.address_field()?;
        (address.len() >= 5).then_some(&address[2..5])
    }

    /// Returns data fields after the NMEA address field without the checksum.
    #[must_use]
    pub fn data_fields(&self) -> Vec<&[u8]> {
        let body = self.body_without_checksum();
        let mut fields = body.split(|byte| *byte == b',');
        let _address = fields.next();
        fields.collect()
    }

    /// Returns checksum validation details when the sentence has `*HH` syntax.
    #[must_use]
    pub fn checksum(&self) -> Option<NmeaChecksum> {
        let separator = self.sentence.iter().rposition(|byte| *byte == b'*')?;
        let checksum = self.sentence.get(separator + 1..separator + 3)?;
        if checksum.len() != 2 || self.sentence.get(separator + 3).is_some() {
            return None;
        }

        let expected = parse_hex_byte(checksum)?;
        let calculated = self.sentence[..separator]
            .iter()
            .fold(0u8, |accumulator, byte| accumulator ^ byte);

        Some(NmeaChecksum {
            expected,
            calculated,
            valid: expected == calculated,
        })
    }

    fn address_field(&self) -> Option<&[u8]> {
        let body = self.body_without_checksum();
        let end = body
            .iter()
            .position(|byte| *byte == b',')
            .unwrap_or(body.len());
        let address = &body[..end];
        (address.len() >= 5 && address.iter().all(u8::is_ascii_alphanumeric)).then_some(address)
    }

    fn body_without_checksum(&self) -> &[u8] {
        match self.sentence.iter().rposition(|byte| *byte == b'*') {
            Some(separator) => &self.sentence[..separator],
            None => self.sentence,
        }
    }
}

/// NMEA checksum validation details.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct NmeaChecksum {
    /// Checksum value supplied by the packet.
    pub expected: u8,
    /// Checksum calculated over bytes before `*`.
    pub calculated: u8,
    /// Whether supplied and calculated checksums match.
    pub valid: bool,
}

/// APRS Mic-E packet bytes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct MicE<'a> {
    /// Original Mic-E data type identifier byte.
    pub identifier: u8,
    /// Destination address bytes that carry Mic-E latitude/status data.
    pub destination: &'a [u8],
    /// Mic-E body bytes.
    pub body: &'a [u8],
    /// Destination-derived Mic-E status bits when the destination permits decoding.
    pub status: Option<MicEStatus>,
    /// Destination-derived six latitude digit nibbles when decodable.
    pub latitude_digits: Option<[u8; 6]>,
}

impl MicE<'_> {
    /// Returns decoded Mic-E coordinates when destination and body bytes permit it.
    #[must_use]
    pub fn coordinates(&self) -> Option<Coordinates> {
        Some(Coordinates {
            latitude: decode_mic_e_latitude(self.destination)?,
            longitude: decode_mic_e_longitude(self.destination, self.body)?,
        })
    }

    /// Returns decoded Mic-E speed and course when body bytes permit it.
    #[must_use]
    pub fn speed_course(&self) -> Option<MicESpeedCourse> {
        decode_mic_e_speed_course(self.body)
    }

    /// Returns the Mic-E destination-derived message code when decodable.
    #[must_use]
    pub fn message_code(&self) -> Option<MicEMessageCode> {
        decode_mic_e_message_code(self.destination)
    }
}

/// Mic-E destination-derived status bits.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MicEStatus {
    /// Standard/custom status bit tuple from the first three destination bytes.
    Custom([bool; 3]),
}

/// Mic-E destination-derived message code.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MicEMessageCode {
    /// Standard Mic-E message code.
    Standard(MicEStandardMessage),
    /// Custom Mic-E message code number from 0 through 6.
    Custom(u8),
    /// Emergency message code.
    Emergency,
}

/// Standard Mic-E message code.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MicEStandardMessage {
    /// M0: Off Duty.
    OffDuty,
    /// M1: En Route.
    EnRoute,
    /// M2: In Service.
    InService,
    /// M3: Returning.
    Returning,
    /// M4: Committed.
    Committed,
    /// M5: Special.
    Special,
    /// M6: Priority.
    Priority,
}

/// Mic-E speed/course extension.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct MicESpeedCourse {
    /// Speed in knots.
    pub speed_knots: u16,
    /// Course in degrees as encoded by Mic-E.
    pub course_degrees: u16,
}

/// APRS Maidenhead locator packet bytes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Maidenhead<'a> {
    /// Six-byte Maidenhead locator.
    pub locator: &'a [u8],
    /// Remaining comment bytes.
    pub comment: &'a [u8],
}

/// APRS user-defined packet fields.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct UserDefined<'a> {
    /// One-byte user ID.
    pub user_id: u8,
    /// One-byte user-defined packet type.
    pub packet_type: u8,
    /// User-defined body bytes.
    pub body: &'a [u8],
}

/// APRS third-party traffic packet bytes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ThirdParty<'a> {
    /// Encapsulated third-party traffic bytes.
    pub body: &'a [u8],
}

impl ThirdParty<'_> {
    /// Explicitly parses the encapsulated packet through the same codec boundary.
    pub fn nested_packet(&self) -> Result<ParsedPacket, ParseError> {
        parse_packet(self.body)
    }
}

/// APRS data type identifier from the first payload byte.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DataTypeIdentifier {
    /// `!`: position without timestamp, no APRS messaging.
    PositionNoTimestamp,
    /// `=`: position without timestamp, APRS messaging supported.
    PositionNoTimestampMessaging,
    /// `/`: position with timestamp, no APRS messaging.
    PositionWithTimestamp,
    /// `@`: position with timestamp, APRS messaging supported.
    PositionWithTimestampMessaging,
    /// `>`: status.
    Status,
    /// `?`: query.
    Query,
    /// `<`: station capabilities.
    Capability,
    /// `:`: message, bulletin, or announcement.
    Message,
    /// `;`: object.
    Object,
    /// `)`: item.
    Item,
    /// `_`: weather report without position.
    Weather,
    /// `T`: telemetry.
    Telemetry,
    /// `$`: NMEA sentence.
    Nmea,
    /// ``` ` ```: current Mic-E data.
    MicECurrent,
    /// `'`: old Mic-E data.
    MicEOld,
    /// `[`: Maidenhead locator.
    Maidenhead,
    /// `{`: user-defined data.
    UserDefined,
    /// `}`: third-party traffic.
    ThirdParty,
    /// Any currently unclassified identifier byte.
    Unknown(u8),
}

impl DataTypeIdentifier {
    fn from_byte(byte: u8) -> Self {
        match byte {
            b'!' => Self::PositionNoTimestamp,
            b'=' => Self::PositionNoTimestampMessaging,
            b'/' => Self::PositionWithTimestamp,
            b'@' => Self::PositionWithTimestampMessaging,
            b'>' => Self::Status,
            b'?' => Self::Query,
            b'<' => Self::Capability,
            b':' => Self::Message,
            b';' => Self::Object,
            b')' => Self::Item,
            b'_' => Self::Weather,
            b'T' => Self::Telemetry,
            b'$' => Self::Nmea,
            b'`' => Self::MicECurrent,
            b'\'' => Self::MicEOld,
            b'[' => Self::Maidenhead,
            b'{' => Self::UserDefined,
            b'}' => Self::ThirdParty,
            other => Self::Unknown(other),
        }
    }

    fn as_byte(self) -> u8 {
        match self {
            Self::PositionNoTimestamp => b'!',
            Self::PositionNoTimestampMessaging => b'=',
            Self::PositionWithTimestamp => b'/',
            Self::PositionWithTimestampMessaging => b'@',
            Self::Status => b'>',
            Self::Query => b'?',
            Self::Capability => b'<',
            Self::Message => b':',
            Self::Object => b';',
            Self::Item => b')',
            Self::Weather => b'_',
            Self::Telemetry => b'T',
            Self::Nmea => b'$',
            Self::MicECurrent => b'`',
            Self::MicEOld => b'\'',
            Self::Maidenhead => b'[',
            Self::UserDefined => b'{',
            Self::ThirdParty => b'}',
            Self::Unknown(value) => value,
        }
    }

    /// Returns a stable data type identifier name for diagnostics.
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::PositionNoTimestamp => "position_no_timestamp",
            Self::PositionNoTimestampMessaging => "position_no_timestamp_messaging",
            Self::PositionWithTimestamp => "position_with_timestamp",
            Self::PositionWithTimestampMessaging => "position_with_timestamp_messaging",
            Self::Status => "status",
            Self::Query => "query",
            Self::Capability => "capability",
            Self::Message => "message",
            Self::Object => "object",
            Self::Item => "item",
            Self::Weather => "weather",
            Self::Telemetry => "telemetry",
            Self::Nmea => "nmea",
            Self::MicECurrent => "mic_e_current",
            Self::MicEOld => "mic_e_old",
            Self::Maidenhead => "maidenhead",
            Self::UserDefined => "user_defined",
            Self::ThirdParty => "third_party",
            Self::Unknown(_) => "unknown",
        }
    }
}

fn parse_aprs_data<'a>(
    identifier: DataTypeIdentifier,
    information: &'a [u8],
    destination: &'a [u8],
) -> AprsData<'a> {
    match identifier {
        DataTypeIdentifier::Status => AprsData::Status { text: information },
        DataTypeIdentifier::PositionNoTimestamp => parse_position(false, b'!', information),
        DataTypeIdentifier::PositionNoTimestampMessaging => parse_position(true, b'=', information),
        DataTypeIdentifier::PositionWithTimestamp => {
            parse_timestamped_position(false, b'/', information)
        }
        DataTypeIdentifier::PositionWithTimestampMessaging => {
            parse_timestamped_position(true, b'@', information)
        }
        DataTypeIdentifier::Message => parse_message(information),
        DataTypeIdentifier::Object => parse_object(information),
        DataTypeIdentifier::Item => parse_item(information),
        DataTypeIdentifier::Weather => AprsData::Weather(Weather {
            report: information,
        }),
        DataTypeIdentifier::Telemetry => parse_telemetry(information),
        DataTypeIdentifier::Query => AprsData::Query(Query { query: information }),
        DataTypeIdentifier::Capability => AprsData::Capability(Capability { body: information }),
        DataTypeIdentifier::Nmea => AprsData::Nmea(Nmea {
            sentence: information,
        }),
        DataTypeIdentifier::MicECurrent | DataTypeIdentifier::MicEOld => {
            parse_mic_e(identifier, information, destination)
        }
        DataTypeIdentifier::Maidenhead => parse_maidenhead(information),
        DataTypeIdentifier::UserDefined => parse_user_defined(information),
        DataTypeIdentifier::ThirdParty => AprsData::ThirdParty(ThirdParty { body: information }),
        other => AprsData::Unsupported {
            identifier: other.as_byte(),
            information,
        },
    }
}

fn parse_mic_e<'a>(
    identifier: DataTypeIdentifier,
    information: &'a [u8],
    destination: &'a [u8],
) -> AprsData<'a> {
    if information.len() < 3 {
        return AprsData::Malformed {
            identifier: identifier.as_byte(),
            information,
        };
    }

    AprsData::MicE(MicE {
        identifier: identifier.as_byte(),
        destination,
        body: information,
        status: decode_mic_e_status(destination),
        latitude_digits: decode_mic_e_latitude_digits(destination),
    })
}

fn parse_position(messaging: bool, identifier: u8, information: &[u8]) -> AprsData<'_> {
    if is_compressed_position(information) {
        return parse_compressed_position(messaging, identifier, information);
    }

    if information.len() < 19 {
        return AprsData::Malformed {
            identifier,
            information,
        };
    }

    let latitude = &information[..8];
    let symbol_table = information[8];
    let longitude = &information[9..18];
    let symbol_code = information[18];
    let comment = &information[19..];

    if !is_latitude(latitude)
        || !is_symbol_table_identifier(symbol_table)
        || !is_longitude(longitude)
        || !is_printable_ascii(symbol_code)
    {
        return AprsData::Malformed {
            identifier,
            information,
        };
    }

    AprsData::Position(Position {
        messaging,
        latitude,
        symbol_table,
        longitude,
        symbol_code,
        comment,
    })
}

fn coordinates_from_position_body(body: &[u8]) -> Option<Coordinates> {
    if is_compressed_position(body) {
        let AprsData::CompressedPosition(position) = parse_compressed_position(false, b'!', body)
        else {
            return None;
        };
        return position.coordinates();
    }

    let AprsData::Position(position) = parse_position(false, b'!', body) else {
        return None;
    };
    position.coordinates()
}

fn parse_timestamped_position(messaging: bool, identifier: u8, information: &[u8]) -> AprsData<'_> {
    if information.len() < 8 {
        return AprsData::Malformed {
            identifier,
            information,
        };
    }

    let timestamp = &information[..7];
    if !is_timestamp(timestamp) {
        return AprsData::Malformed {
            identifier,
            information,
        };
    }

    match parse_position(messaging, identifier, &information[7..]) {
        AprsData::Position(position) => AprsData::TimestampedPosition(TimestampedPosition {
            messaging,
            timestamp,
            position,
        }),
        AprsData::CompressedPosition(position) => AprsData::CompressedPosition(position),
        _ => AprsData::Malformed {
            identifier,
            information,
        },
    }
}

fn parse_compressed_position(messaging: bool, identifier: u8, information: &[u8]) -> AprsData<'_> {
    if information.len() < 13 {
        return AprsData::Malformed {
            identifier,
            information,
        };
    }

    let symbol_table = information[0];
    let compressed_latitude = &information[1..5];
    let compressed_longitude = &information[5..9];
    let symbol_code = information[9];
    let extension = &information[10..12];
    let compression_type = information[12];
    let comment = &information[13..];

    if !is_symbol_table_identifier(symbol_table)
        || !compressed_latitude.iter().all(|byte| is_base91(*byte))
        || !compressed_longitude.iter().all(|byte| is_base91(*byte))
        || !is_printable_ascii(symbol_code)
        || !extension.iter().all(|byte| is_base91(*byte))
        || !is_base91(compression_type)
    {
        return AprsData::Malformed {
            identifier,
            information,
        };
    }

    AprsData::CompressedPosition(CompressedPosition {
        messaging,
        symbol_table,
        compressed_latitude,
        compressed_longitude,
        symbol_code,
        extension,
        compression_type,
        comment,
    })
}

fn parse_object(information: &[u8]) -> AprsData<'_> {
    if information.len() < 17
        || !matches!(information[9], b'*' | b'_')
        || !is_timestamp(&information[10..17])
    {
        return AprsData::Malformed {
            identifier: b';',
            information,
        };
    }

    AprsData::Object(Object {
        name: &information[..9],
        live: information[9] == b'*',
        timestamp: &information[10..17],
        body: &information[17..],
    })
}

fn parse_item(information: &[u8]) -> AprsData<'_> {
    let Some(separator) = information
        .iter()
        .position(|byte| matches!(*byte, b'!' | b'_'))
    else {
        return AprsData::Malformed {
            identifier: b')',
            information,
        };
    };

    if separator == 0 || separator > 9 {
        return AprsData::Malformed {
            identifier: b')',
            information,
        };
    }

    AprsData::Item(Item {
        name: &information[..separator],
        live: information[separator] == b'!',
        body: &information[separator + 1..],
    })
}

fn parse_message(information: &[u8]) -> AprsData<'_> {
    if information.len() < 10 || information[9] != b':' {
        return AprsData::Malformed {
            identifier: b':',
            information,
        };
    }

    let addressee = &information[..9];
    let body = &information[10..];
    if let Some(kind) = classify_telemetry_metadata_kind(addressee) {
        return AprsData::TelemetryMetadata(TelemetryMetadata {
            addressee,
            kind,
            body,
        });
    }

    let (text, id) = match body.iter().position(|byte| *byte == b'{') {
        Some(separator) => (&body[..separator], Some(&body[separator + 1..])),
        None => (body, None),
    };
    let kind = classify_message_kind(addressee, text);

    AprsData::Message(Message {
        addressee,
        kind,
        text,
        id,
    })
}

fn parse_telemetry(information: &[u8]) -> AprsData<'_> {
    if !information.starts_with(b"#") {
        return AprsData::Malformed {
            identifier: b'T',
            information,
        };
    }

    let fields: Vec<&[u8]> = information[1..].split(|byte| *byte == b',').collect();
    if fields.len() < 6 || fields[..6].iter().any(|field| field.is_empty()) {
        return AprsData::Malformed {
            identifier: b'T',
            information,
        };
    }

    AprsData::Telemetry(Telemetry {
        sequence: fields[0],
        analog: [fields[1], fields[2], fields[3], fields[4], fields[5]],
        digital: fields.get(6).copied().filter(|field| !field.is_empty()),
    })
}

fn parse_maidenhead(information: &[u8]) -> AprsData<'_> {
    if information.len() < 6 || !is_maidenhead_locator(&information[..6]) {
        return AprsData::Malformed {
            identifier: b'[',
            information,
        };
    }

    AprsData::Maidenhead(Maidenhead {
        locator: &information[..6],
        comment: &information[6..],
    })
}

fn parse_user_defined(information: &[u8]) -> AprsData<'_> {
    if information.len() < 2 {
        return AprsData::Malformed {
            identifier: b'{',
            information,
        };
    }

    AprsData::UserDefined(UserDefined {
        user_id: information[0],
        packet_type: information[1],
        body: &information[2..],
    })
}

fn classify_telemetry_metadata_kind(addressee: &[u8]) -> Option<TelemetryMetadataKind> {
    match addressee.get(..5)? {
        b"PARM." => Some(TelemetryMetadataKind::ParameterNames),
        b"UNIT." => Some(TelemetryMetadataKind::Units),
        b"EQNS." => Some(TelemetryMetadataKind::Equations),
        b"BITS." => Some(TelemetryMetadataKind::BitSense),
        _ => None,
    }
}

fn classify_message_kind(addressee: &[u8], text: &[u8]) -> MessageKind {
    if text.starts_with(b"ack") {
        MessageKind::Ack
    } else if text.starts_with(b"rej") {
        MessageKind::Reject
    } else if addressee.starts_with(b"BLN") && addressee.get(3).is_some_and(u8::is_ascii_digit) {
        MessageKind::Bulletin
    } else if addressee.starts_with(b"BLN") && addressee.get(3).is_some_and(u8::is_ascii_uppercase)
    {
        MessageKind::Announcement
    } else {
        MessageKind::Message
    }
}

fn is_latitude(value: &[u8]) -> bool {
    if !(value.len() == 8
        && value[0].is_ascii_digit()
        && value[1].is_ascii_digit()
        && value[2].is_ascii_digit()
        && value[3].is_ascii_digit()
        && value[4] == b'.'
        && value[5].is_ascii_digit()
        && value[6].is_ascii_digit()
        && matches!(value[7], b'N' | b'S'))
    {
        return false;
    }

    coordinate_in_range(&value[..2], &value[2..7], 90)
}

fn is_longitude(value: &[u8]) -> bool {
    if !(value.len() == 9
        && value[0].is_ascii_digit()
        && value[1].is_ascii_digit()
        && value[2].is_ascii_digit()
        && value[3].is_ascii_digit()
        && value[4].is_ascii_digit()
        && value[5] == b'.'
        && value[6].is_ascii_digit()
        && value[7].is_ascii_digit()
        && matches!(value[8], b'E' | b'W'))
    {
        return false;
    }

    coordinate_in_range(&value[..3], &value[3..8], 180)
}

fn coordinate_in_range(degrees: &[u8], minutes: &[u8], max_degrees: u16) -> bool {
    let Some(degrees) = parse_u16(degrees) else {
        return false;
    };
    let Some(minutes) = parse_fixed_minutes(minutes) else {
        return false;
    };

    degrees < max_degrees || (degrees == max_degrees && minutes == 0.0)
}

fn is_symbol_table_identifier(value: u8) -> bool {
    matches!(value, b'/' | b'\\') || value.is_ascii_alphanumeric()
}

fn is_printable_ascii(value: u8) -> bool {
    (0x20..=0x7e).contains(&value)
}

fn is_base91(value: u8) -> bool {
    (b'!'..=b'{').contains(&value)
}

fn is_compressed_position(information: &[u8]) -> bool {
    information
        .first()
        .is_some_and(|byte| !byte.is_ascii_digit() && is_symbol_table_identifier(*byte))
        && information
            .get(1..13)
            .is_some_and(|bytes| bytes.iter().all(|byte| is_base91(*byte)))
}

fn is_timestamp(value: &[u8]) -> bool {
    value.len() == 7
        && value[..6].iter().all(u8::is_ascii_digit)
        && matches!(value[6], b'z' | b'/' | b'h')
}

fn is_maidenhead_locator(value: &[u8]) -> bool {
    value.len() == 6
        && is_ascii_alpha_range(value[0], b'A', b'R')
        && is_ascii_alpha_range(value[1], b'A', b'R')
        && value[2].is_ascii_digit()
        && value[3].is_ascii_digit()
        && is_ascii_alpha_range(value[4], b'A', b'X')
        && is_ascii_alpha_range(value[5], b'A', b'X')
}

fn is_ascii_alpha_range(value: u8, start: u8, end: u8) -> bool {
    let uppercase = value.to_ascii_uppercase();
    (start..=end).contains(&uppercase)
}

fn decode_latitude(value: &[u8]) -> Option<f64> {
    if !is_latitude(value) {
        return None;
    }

    let degrees = parse_u16(&value[..2])? as f64;
    let minutes = parse_fixed_minutes(&value[2..7])?;
    let sign = match value[7] {
        b'N' => 1.0,
        b'S' => -1.0,
        _ => return None,
    };

    Some(sign * (degrees + minutes / 60.0))
}

fn decode_longitude(value: &[u8]) -> Option<f64> {
    if !is_longitude(value) {
        return None;
    }

    let degrees = parse_u16(&value[..3])? as f64;
    let minutes = parse_fixed_minutes(&value[3..8])?;
    let sign = match value[8] {
        b'E' => 1.0,
        b'W' => -1.0,
        _ => return None,
    };

    Some(sign * (degrees + minutes / 60.0))
}

fn parse_fixed_minutes(value: &[u8]) -> Option<f64> {
    if value.len() != 5 || value[2] != b'.' || !value[..2].iter().all(u8::is_ascii_digit) {
        return None;
    }

    let whole = parse_u16(&value[..2])? as f64;
    let fraction = parse_u16(&value[3..])? as f64 / 100.0;
    Some(whole + fraction)
}

fn decode_base91(value: &[u8]) -> Option<u32> {
    if value.len() != 4 || !value.iter().all(|byte| is_base91(*byte)) {
        return None;
    }

    let mut decoded = 0u32;
    for byte in value {
        decoded = decoded * 91 + u32::from(byte - b'!');
    }

    Some(decoded)
}

fn parse_u16(value: &[u8]) -> Option<u16> {
    if value.is_empty() || !value.iter().all(u8::is_ascii_digit) {
        return None;
    }

    let mut parsed = 0u16;
    for digit in value {
        parsed = parsed.checked_mul(10)?;
        parsed = parsed.checked_add(u16::from(digit - b'0'))?;
    }

    Some(parsed)
}

fn parse_i16(value: &[u8]) -> Option<i16> {
    if value.is_empty() {
        return None;
    }

    let (sign, digits) = match value[0] {
        b'-' => (-1, &value[1..]),
        b'+' => (1, &value[1..]),
        _ => (1, value),
    };

    let unsigned = parse_u16(digits)?;
    i16::try_from(unsigned).ok()?.checked_mul(sign)
}

fn parse_hex_byte(value: &[u8]) -> Option<u8> {
    if value.len() != 2 {
        return None;
    }

    Some(hex_value(value[0])? * 16 + hex_value(value[1])?)
}

fn hex_value(value: u8) -> Option<u8> {
    match value {
        b'0'..=b'9' => Some(value - b'0'),
        b'A'..=b'F' => Some(value - b'A' + 10),
        b'a'..=b'f' => Some(value - b'a' + 10),
        _ => None,
    }
}

fn parse_tagged_u16(report: &[u8], tag: u8, width: usize) -> Option<u16> {
    parse_tagged(report, tag, width).and_then(parse_u16)
}

fn parse_tagged_i16(report: &[u8], tag: u8, width: usize) -> Option<i16> {
    parse_tagged(report, tag, width).and_then(parse_i16)
}

fn parse_tagged(report: &[u8], tag: u8, width: usize) -> Option<&[u8]> {
    let start = report.iter().position(|byte| *byte == tag)? + 1;
    report.get(start..start + width)
}

fn decode_mic_e_status(destination: &[u8]) -> Option<MicEStatus> {
    if destination.len() != 6 {
        return None;
    }

    let bytes = destination.get(..3)?;
    Some(MicEStatus::Custom([
        mic_e_status_bit(bytes[0])?,
        mic_e_status_bit(bytes[1])?,
        mic_e_status_bit(bytes[2])?,
    ]))
}

fn decode_mic_e_message_code(destination: &[u8]) -> Option<MicEMessageCode> {
    if destination.len() != 6 {
        return None;
    }

    let mut bits = [MicEMessageBit::Zero; 3];
    for (index, byte) in destination[..3].iter().copied().enumerate() {
        bits[index] = mic_e_message_bit(byte)?;
    }

    let code = message_code_number([
        !matches!(bits[0], MicEMessageBit::Zero),
        !matches!(bits[1], MicEMessageBit::Zero),
        !matches!(bits[2], MicEMessageBit::Zero),
    ]);

    if code == 7 {
        return Some(MicEMessageCode::Emergency);
    }

    let has_standard = bits
        .iter()
        .any(|bit| matches!(bit, MicEMessageBit::StandardOne));
    let has_custom = bits
        .iter()
        .any(|bit| matches!(bit, MicEMessageBit::CustomOne));

    if has_standard && !has_custom {
        return standard_mic_e_message(code).map(MicEMessageCode::Standard);
    }

    if has_custom && !has_standard {
        return Some(MicEMessageCode::Custom(code));
    }

    None
}

#[derive(Clone, Copy)]
enum MicEMessageBit {
    Zero,
    StandardOne,
    CustomOne,
}

fn mic_e_message_bit(byte: u8) -> Option<MicEMessageBit> {
    match byte {
        b'0'..=b'9' | b'L' => Some(MicEMessageBit::Zero),
        b'A'..=b'K' => Some(MicEMessageBit::StandardOne),
        b'P'..=b'Z' => Some(MicEMessageBit::CustomOne),
        _ => None,
    }
}

fn message_code_number(bits: [bool; 3]) -> u8 {
    match bits {
        [true, true, true] => 0,
        [true, true, false] => 1,
        [true, false, true] => 2,
        [true, false, false] => 3,
        [false, true, true] => 4,
        [false, true, false] => 5,
        [false, false, true] => 6,
        [false, false, false] => 7,
    }
}

fn standard_mic_e_message(code: u8) -> Option<MicEStandardMessage> {
    match code {
        0 => Some(MicEStandardMessage::OffDuty),
        1 => Some(MicEStandardMessage::EnRoute),
        2 => Some(MicEStandardMessage::InService),
        3 => Some(MicEStandardMessage::Returning),
        4 => Some(MicEStandardMessage::Committed),
        5 => Some(MicEStandardMessage::Special),
        6 => Some(MicEStandardMessage::Priority),
        _ => None,
    }
}

fn mic_e_status_bit(byte: u8) -> Option<bool> {
    match byte {
        b'0'..=b'9' | b'L' => Some(false),
        b'A'..=b'K' | b'P'..=b'Z' => Some(true),
        _ => None,
    }
}

fn decode_mic_e_latitude_digits(destination: &[u8]) -> Option<[u8; 6]> {
    if destination.len() != 6 {
        return None;
    }

    let mut digits = [0u8; 6];
    for (index, byte) in destination.iter().copied().enumerate() {
        digits[index] = mic_e_latitude_digit(byte)?;
    }

    Some(digits)
}

fn mic_e_latitude_digit(byte: u8) -> Option<u8> {
    match byte {
        b'0'..=b'9' => Some(byte - b'0'),
        b'A'..=b'J' => Some(byte - b'A'),
        b'P'..=b'Y' => Some(byte - b'P'),
        b'K' | b'L' | b'Z' => Some(0),
        _ => None,
    }
}

fn decode_mic_e_latitude(destination: &[u8]) -> Option<f64> {
    let digits = decode_mic_e_latitude_digits(destination)?;
    let degrees = u16::from(digits[0]) * 10 + u16::from(digits[1]);
    let minutes = u16::from(digits[2]) * 10 + u16::from(digits[3]);
    let hundredths = u16::from(digits[4]) * 10 + u16::from(digits[5]);
    if degrees > 90 || minutes > 59 {
        return None;
    }

    let sign = if mic_e_north(destination[3])? {
        1.0
    } else {
        -1.0
    };
    Some(sign * (f64::from(degrees) + (f64::from(minutes) + f64::from(hundredths) / 100.0) / 60.0))
}

fn decode_mic_e_longitude(destination: &[u8], body: &[u8]) -> Option<f64> {
    if destination.len() != 6 || body.len() < 3 {
        return None;
    }

    let mut degrees = i16::from(mic_e_body_value(body[0])?);
    if mic_e_longitude_offset(destination[4])? {
        degrees += 100;
    }
    if (180..=189).contains(&degrees) {
        degrees -= 80;
    } else if (190..=199).contains(&degrees) {
        degrees -= 190;
    }

    let minutes = mic_e_body_value(body[1])?;
    let hundredths = mic_e_body_value(body[2])?;
    if !(0..=179).contains(&degrees) || minutes > 59 || hundredths > 99 {
        return None;
    }

    let sign = if mic_e_west(destination[5])? {
        -1.0
    } else {
        1.0
    };
    Some(sign * (f64::from(degrees) + (f64::from(minutes) + f64::from(hundredths) / 100.0) / 60.0))
}

fn decode_mic_e_speed_course(body: &[u8]) -> Option<MicESpeedCourse> {
    if body.len() < 6 {
        return None;
    }

    let speed_tens = u16::from(mic_e_body_value(body[3])?);
    let speed_units_course_hundreds = u16::from(mic_e_body_value(body[4])?);
    let course_remainder = u16::from(mic_e_body_value(body[5])?);
    let mut speed_knots = speed_tens * 10 + speed_units_course_hundreds / 10;
    if speed_knots >= 800 {
        speed_knots -= 800;
    }

    Some(MicESpeedCourse {
        speed_knots,
        course_degrees: (speed_units_course_hundreds % 10) * 100 + course_remainder,
    })
}

fn mic_e_body_value(byte: u8) -> Option<u8> {
    let value = byte.checked_sub(28)?;
    (value <= 99).then_some(value)
}

fn mic_e_north(byte: u8) -> Option<bool> {
    match byte {
        b'0'..=b'9' | b'A'..=b'L' => Some(false),
        b'P'..=b'Z' => Some(true),
        _ => None,
    }
}

fn mic_e_longitude_offset(byte: u8) -> Option<bool> {
    match byte {
        b'0'..=b'9' | b'A'..=b'L' => Some(false),
        b'P'..=b'Z' => Some(true),
        _ => None,
    }
}

fn mic_e_west(byte: u8) -> Option<bool> {
    match byte {
        b'0'..=b'9' | b'A'..=b'L' => Some(false),
        b'P'..=b'Z' => Some(true),
        _ => None,
    }
}

/// Fail-closed packet parse errors.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
    /// No bytes were supplied.
    Empty,
    /// Packet exceeds [`MAX_PACKET_LEN`].
    Oversized,
    /// Packet does not contain the required APRS `>` and `:` separators.
    MissingSeparator,
    /// Packet contains an empty source, path, or payload segment.
    EmptySegment,
    /// Packet source or path contains bytes outside the conservative address set.
    InvalidAddress,
}

impl ParseError {
    /// Returns a stable parse error code for logs and external systems.
    #[must_use]
    pub fn code(&self) -> &'static str {
        match self {
            Self::Empty => "parse.empty",
            Self::Oversized => "parse.oversized",
            Self::MissingSeparator => "parse.missing_separator",
            Self::EmptySegment => "parse.empty_segment",
            Self::InvalidAddress => "parse.invalid_address",
        }
    }
}

/// Parses an APRS packet from untrusted bytes.
///
/// This parser intentionally validates only the minimal frame shape for the
/// skeleton: `source>path:payload`. Payload bytes are opaque and may be invalid
/// UTF-8.
pub fn parse_packet(input: &[u8]) -> Result<ParsedPacket, ParseError> {
    parse_packet_with_options(input, ParseOptions::default())
}

/// Parses an APRS packet from untrusted bytes with explicit codec options.
pub fn parse_packet_with_options(
    input: &[u8],
    options: ParseOptions,
) -> Result<ParsedPacket, ParseError> {
    if input.is_empty() {
        return Err(ParseError::Empty);
    }

    if input.len() > options.max_packet_len {
        return Err(ParseError::Oversized);
    }

    let source_end = input
        .iter()
        .position(|byte| *byte == b'>')
        .ok_or(ParseError::MissingSeparator)?;
    let payload_separator = input[source_end + 1..]
        .iter()
        .position(|byte| *byte == b':')
        .map(|offset| source_end + 1 + offset)
        .ok_or(ParseError::MissingSeparator)?;

    let path_start = source_end + 1;
    let path_end = payload_separator;
    let payload_start = payload_separator + 1;

    if source_end == 0 || path_start == path_end || payload_start == input.len() {
        return Err(ParseError::EmptySegment);
    }

    let Some(path_components) = path_component_ranges(input, path_start, path_end) else {
        return Err(ParseError::InvalidAddress);
    };

    if !is_ax25_like_source(&input[..source_end])
        || !path_components
            .iter()
            .all(|(start, end)| is_ax25_like_path_component(&input[*start..*end]))
    {
        return Err(ParseError::InvalidAddress);
    }

    Ok(ParsedPacket {
        raw: RawPacket {
            bytes: input.to_vec(),
        },
        source_end,
        path_start,
        path_end,
        path_components,
        payload_start,
    })
}

fn path_component_ranges(
    input: &[u8],
    path_start: usize,
    path_end: usize,
) -> Option<Vec<(usize, usize)>> {
    let mut components = Vec::new();
    let mut component_start = path_start;

    for (offset, byte) in input[path_start..path_end].iter().enumerate() {
        if *byte == b',' {
            let index = path_start + offset;
            if component_start == index {
                return None;
            }
            components.push((component_start, index));
            component_start = index + 1;
        }
    }

    if component_start == path_end {
        return None;
    }

    components.push((component_start, path_end));
    Some(components)
}

fn is_ax25_like_source(source: &[u8]) -> bool {
    is_ax25_like_address(source, false)
}

fn is_ax25_like_path_component(component: &[u8]) -> bool {
    is_ax25_like_address(component, true)
}

fn is_ax25_like_address(address: &[u8], allow_repeated_marker: bool) -> bool {
    let address = if allow_repeated_marker {
        address.strip_suffix(b"*").unwrap_or(address)
    } else {
        address
    };

    if address.is_empty() || address.contains(&b'*') {
        return false;
    }

    let (callsign, ssid) = match address.iter().position(|byte| *byte == b'-') {
        Some(separator) => (&address[..separator], Some(&address[separator + 1..])),
        None => (address, None),
    };

    is_ax25_like_callsign(callsign) && ssid.map_or(true, is_ax25_like_ssid)
}

fn is_ax25_like_callsign(callsign: &[u8]) -> bool {
    (1..=6).contains(&callsign.len())
        && callsign
            .iter()
            .all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit())
}

fn is_ax25_like_ssid(ssid: &[u8]) -> bool {
    if ssid.is_empty() || ssid.len() > 2 || !ssid.iter().all(u8::is_ascii_digit) {
        return false;
    }

    let mut value = 0u8;
    for digit in ssid {
        value = value * 10 + (digit - b'0');
    }

    value <= 15
}