serial-mcp 0.9.3

MCP server for serial port communication. 25 tools (list/open/read/write/transact/capture_boot/get_status/reconfigure/list_profiles/open_profile/save_profile/delete_profile/configure/rollback_profile/get_log/clear_log/export_log/reconnect/compute_checksum/...), resources, prompts, task cancellation, stdio + streamable-HTTP transports.
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
//! Frame-content parsers.
//!
//! The parser factory (`build_parser`) and the AT command, JSON lines, shell
//! prompt, raw, NMEA-0183, and Modbus ASCII parsers. Parser-only helpers
//! (trailing-newline/leading-marker stripping, checksum validation) live here
//! beside their sole consumers.

use crate::checksums::{lrc, xor_checksum};

use super::config::{ParserConfig, ParserType};
use super::decoder::{FrameDecodeError, FrameParser, ParsedFrame};

// ---- Parser implementations ------------------------------------------------

pub(crate) fn build_parser(config: &ParserConfig) -> Result<Box<dyn FrameParser>, String> {
    match config.parser_type {
        ParserType::AtCommand => Ok(Box::new(AtCommandParser)),
        ParserType::JsonLines => Ok(Box::new(JsonLinesParser)),
        ParserType::ShellPrompt => {
            let custom = config
                .custom_prompt
                .as_deref()
                .map(|s| {
                    regex::bytes::Regex::new(s).map_err(|e| format!("Invalid prompt regex: {e}"))
                })
                .transpose()?;
            Ok(Box::new(ShellPromptParser { custom }))
        }
        ParserType::Raw => Ok(Box::new(RawParser)),
        ParserType::Nmea => Ok(Box::new(NmeaParser {
            validate: config.validate,
        })),
        ParserType::ModbusAscii => Ok(Box::new(ModbusAsciiParser {
            validate: config.validate,
        })),
    }
}

// AT command parser

struct AtCommandParser;

impl FrameParser for AtCommandParser {
    fn parse(&self, data: &[u8]) -> Result<ParsedFrame, FrameDecodeError> {
        let text = String::from_utf8_lossy(data);
        let trimmed = text.trim();
        if trimmed.is_empty() {
            return Ok(ParsedFrame::Raw);
        }
        if let Some(cmd) = trimmed.strip_prefix('+') {
            if let Some(colon) = cmd.find(':') {
                let cmd_name = cmd[..colon].to_string();
                let fields: Vec<String> = cmd[colon + 1..]
                    .split(',')
                    .map(|s| s.trim().to_string())
                    .collect();
                return Ok(ParsedFrame::AtCommand {
                    response_type: "response".into(),
                    command: Some(cmd_name),
                    status: None,
                    fields,
                });
            }
        }
        if trimmed == "OK" {
            return Ok(ParsedFrame::AtCommand {
                response_type: "status".into(),
                command: None,
                status: Some("OK".into()),
                fields: vec![],
            });
        }
        if trimmed == "ERROR" {
            return Ok(ParsedFrame::AtCommand {
                response_type: "error".into(),
                command: None,
                status: Some("ERROR".into()),
                fields: vec![],
            });
        }
        if trimmed.starts_with("+CME ERROR") || trimmed.starts_with("+CMS ERROR") {
            return Ok(ParsedFrame::AtCommand {
                response_type: "error".into(),
                command: None,
                status: Some(trimmed.to_string()),
                fields: vec![],
            });
        }
        Ok(ParsedFrame::AtCommand {
            response_type: "data".into(),
            command: None,
            status: None,
            fields: vec![trimmed.to_string()],
        })
    }
}

// JSON lines parser

struct JsonLinesParser;

impl FrameParser for JsonLinesParser {
    fn parse(&self, data: &[u8]) -> Result<ParsedFrame, FrameDecodeError> {
        match serde_json::from_slice::<serde_json::Value>(data) {
            Ok(val) if val.is_object() => Ok(ParsedFrame::Json(val)),
            _ => Ok(ParsedFrame::Raw),
        }
    }
}

// Shell prompt parser

struct ShellPromptParser {
    custom: Option<regex::bytes::Regex>,
}

impl FrameParser for ShellPromptParser {
    fn parse(&self, data: &[u8]) -> Result<ParsedFrame, FrameDecodeError> {
        let text = String::from_utf8_lossy(data);
        let trimmed = text.trim_end();
        if trimmed.is_empty() {
            return Ok(ParsedFrame::Raw);
        }
        if let Some(ref re) = self.custom {
            if re.is_match(data) {
                return Ok(ParsedFrame::ShellPrompt {
                    prompt: trimmed.to_string(),
                    prompt_type: "custom".into(),
                });
            }
        }
        if trimmed.ends_with("$ ") || trimmed.ends_with("$") {
            return Ok(ParsedFrame::ShellPrompt {
                prompt: trimmed.to_string(),
                prompt_type: "user".into(),
            });
        }
        if trimmed.ends_with("# ") || trimmed.ends_with("#") {
            return Ok(ParsedFrame::ShellPrompt {
                prompt: trimmed.to_string(),
                prompt_type: "root".into(),
            });
        }
        if trimmed.ends_with("> ") || trimmed.ends_with(">") {
            return Ok(ParsedFrame::ShellPrompt {
                prompt: trimmed.to_string(),
                prompt_type: "generic".into(),
            });
        }
        if let Some(at_pos) = trimmed.rfind('@') {
            if let Some(colon_pos) = trimmed[at_pos..].find(':') {
                let _user = &trimmed[..at_pos];
                let _host = &trimmed[at_pos + 1..at_pos + colon_pos];
                let suffix = &trimmed[at_pos + colon_pos + 1..];
                if suffix == "$ " || suffix == "$" || suffix == "# " || suffix == "#" {
                    return Ok(ParsedFrame::ShellPrompt {
                        prompt: trimmed.to_string(),
                        prompt_type: if suffix.starts_with('#') {
                            "root".to_string()
                        } else {
                            "user".to_string()
                        },
                    });
                }
            }
        }
        Ok(ParsedFrame::Raw)
    }
}

// Raw parser (passthrough)

struct RawParser;

impl FrameParser for RawParser {
    fn parse(&self, _data: &[u8]) -> Result<ParsedFrame, FrameDecodeError> {
        Ok(ParsedFrame::Raw)
    }
}

/// Strip a trailing `\r\n` or bare `\n` from `content` in place (defensive:
/// handles frames where the end marker was NOT stripped by the framing layer).
fn strip_trailing_newline(content: &mut Vec<u8>) {
    if content.ends_with(b"\r\n") {
        content.truncate(content.len() - 2);
    } else if content.ends_with(b"\n") {
        content.truncate(content.len() - 1);
    }
}

/// Strip a leading byte if it matches any of `markers` (defensive: handles
/// frames where the start marker was NOT stripped by the framing layer).
fn strip_leading_if_any(content: &mut Vec<u8>, markers: &[u8]) {
    if let Some(&first) = content.first() {
        if markers.contains(&first) {
            content.remove(0);
        }
    }
}

/// Shared checksum-validate ladder for NMEA XOR and Modbus ASCII LRC.
///
/// Returns `Ok(Some(true))` if computed == received, `Ok(Some(false))` if
/// validate is false and they differ, or `Err(ChecksumMismatch)` if validate is
/// true and they differ. `received` is `None` when the received checksum is
/// unparseable or too short (guaranteed mismatch). The error carries the 1-byte
/// `computed_byte` as `expected` and the raw received bytes as `received` to
/// preserve the [`FrameDecodeError`] shape.
fn check_checksum(
    validate: bool,
    computed_byte: u8,
    received: Option<u8>,
    received_raw: Vec<u8>,
) -> Result<Option<bool>, FrameDecodeError> {
    match received {
        Some(r) if r == computed_byte => Ok(Some(true)),
        Some(_) if validate => Err(FrameDecodeError::ChecksumMismatch {
            expected: vec![computed_byte],
            received: received_raw,
        }),
        Some(_) => Ok(Some(false)),
        None if validate => Err(FrameDecodeError::ChecksumMismatch {
            expected: vec![computed_byte],
            received: received_raw,
        }),
        None => Ok(Some(false)),
    }
}

// NMEA-0183 parser

struct NmeaParser {
    validate: bool,
}

impl FrameParser for NmeaParser {
    fn parse(&self, data: &[u8]) -> Result<ParsedFrame, FrameDecodeError> {
        // 1. Strip optional leading $ or ! and trailing \r\n.
        //    Defensive: when include_markers is true, data includes the start/end
        //    markers. The nmea0183 preset uses include_markers=false, but bare
        //    callers might set true. Handle both.
        let mut content = data.to_vec();
        strip_leading_if_any(&mut content, b"$!");
        strip_trailing_newline(&mut content);

        // 2. Validate that this looks like a NMEA sentence. The preset strips
        //    $/! via StartEnd framing, so the data may start directly with the
        //    talker ID (e.g. "GPGLL,..."). If include_markers were true, $/!
        //    was stripped by step 1 above. In either case, a valid NMEA
        //    sentence always contains at least one comma (separating address
        //    from fields). Non-NMEA frames (e.g. "hello world", "AT+CGMI")
        //    have no comma and return Raw — the parser is opt-in; bare callers
        //    mixing data types see Raw for non-matching frames.
        if !content.contains(&b',') {
            return Ok(ParsedFrame::Raw);
        }

        // 3. Split at '*': content before = sentence body, after = checksum hex.
        let (body, checksum_hex) = if let Some(star_pos) = content.iter().position(|&b| b == b'*') {
            let body = &content[..star_pos];
            let checksum_hex = &content[star_pos + 1..];
            (body.to_vec(), Some(checksum_hex.to_vec()))
        } else {
            (content, None)
        };

        // 4. Evaluate checksum validity. Yields None (no checksum present),
        // Some(true) (present + correct), Some(false) (present + incorrect),
        // or Err (present + incorrect + validate=true). Body parsing always
        // runs afterward so malformed-checksum frames carry the full parsed
        // body with checksum_valid: Some(false) when validate is false.
        let checksum_valid = match &checksum_hex {
            Some(hex) if hex.len() >= 2 => {
                let computed_byte = xor_checksum(&body);
                match std::str::from_utf8(&hex[..2])
                    .ok()
                    .and_then(|s| u8::from_str_radix(s, 16).ok())
                {
                    Some(received_val) => {
                        // Valid hex — compare against computed checksum.
                        check_checksum(
                            self.validate,
                            computed_byte,
                            Some(received_val),
                            vec![received_val],
                        )?
                    }
                    None => {
                        // Invalid hex chars — guaranteed mismatch (None).
                        check_checksum(self.validate, computed_byte, None, hex.clone())?
                    }
                }
            }
            Some(hex) => {
                // Checksum present but too short (<2 hex chars). Guaranteed mismatch.
                let computed_byte = xor_checksum(&body);
                check_checksum(self.validate, computed_byte, None, hex.clone())?
            }
            None => None,
        };

        // 5. Parse the sentence body: split into address + comma fields.
        // NMEA spec is ASCII; invalid UTF-8 is non-NMEA input → Raw
        // (mirrors ModbusAsciiParser).
        let body_str = match std::str::from_utf8(&body) {
            Ok(s) => s,
            Err(_) => return Ok(ParsedFrame::Raw),
        };
        if !body_str.is_ascii() {
            return Ok(ParsedFrame::Raw);
        }

        let (address_part, fields_part) = match body_str.find(',') {
            Some(comma_pos) => (
                body_str[..comma_pos].to_string(),
                body_str[comma_pos + 1..].to_string(),
            ),
            None => (body_str.to_string(), String::new()),
        };

        let (talker_id, sentence_type) = if address_part.len() >= 2 && address_part.starts_with('P')
        {
            // Proprietary NMEA: talker = "P", type = rest of address
            let tid = "P".to_string();
            let stype = address_part[1..].to_string();
            (tid, stype)
        } else if address_part.len() >= 2 {
            // Standard NMEA: talker = first 2, type = rest
            let tid = address_part[..2].to_string();
            let stype = address_part[2..].to_string();
            (tid, stype)
        } else {
            // < 2 chars: use whole as talker, no type
            (address_part, String::new())
        };

        let fields: Vec<String> = if fields_part.is_empty() {
            vec![]
        } else {
            fields_part.split(',').map(|s| s.to_string()).collect()
        };

        Ok(ParsedFrame::Nmea {
            talker_id,
            sentence_type,
            fields,
            checksum_valid,
        })
    }
}

// Modbus ASCII parser

struct ModbusAsciiParser {
    validate: bool,
}

impl FrameParser for ModbusAsciiParser {
    fn parse(&self, data: &[u8]) -> Result<ParsedFrame, FrameDecodeError> {
        // 1. Strip optional leading ':' and trailing \r\n (defensive — the
        //    preset uses include_markers=false so data is the body between
        //    ':' and \r\n; a bare caller with include_markers=true would
        //    include them).
        let mut content = data.to_vec();
        strip_leading_if_any(&mut content, b":");
        strip_trailing_newline(&mut content);

        // 2. Modbus ASCII body is all hex chars (0-9, A-F, a-f). Validate:
        //    if any non-hex byte is present, return Ok(ParsedFrame::Raw) —
        //    non-Modbus frames routed through this parser see Raw (mirror
        //    Nmea's non-NMEA → Raw behavior). The body MUST have an even
        //    number of hex chars (each decoded byte = 2 hex chars). Odd
        //    length → Raw (malformed).
        let body_str = match std::str::from_utf8(&content) {
            Ok(s) => s,
            Err(_) => return Ok(ParsedFrame::Raw),
        };
        if body_str.is_empty() || body_str.bytes().any(|b| !b.is_ascii_hexdigit()) {
            return Ok(ParsedFrame::Raw);
        }
        if body_str.len() % 2 != 0 {
            return Ok(ParsedFrame::Raw);
        }

        // 3. Hex-decode the body into bytes.
        let decoded: Vec<u8> = (0..body_str.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&body_str[i..i + 2], 16).unwrap_or(0))
            .collect();
        // decoded = [address, function_code, data..., lrc]

        // 4. Split off the LRC (last byte). Need at least 3 bytes
        //    (address + function + lrc) for a minimal frame; fewer → Raw.
        if decoded.len() < 3 {
            return Ok(ParsedFrame::Raw);
        }
        let lrc_received = decoded[decoded.len() - 1];
        let pdu = &decoded[..decoded.len() - 1]; // address + function + data
        let address = pdu[0];
        let function_code = pdu[1];
        let data = pdu[2..].to_vec();

        // 5. Validate LRC over the PDU (address + function + data — NOT the LRC byte).
        let computed_val = lrc(pdu);
        let checksum_valid = check_checksum(
            self.validate,
            computed_val,
            Some(lrc_received),
            vec![lrc_received],
        )?;

        // 6. Return ParsedFrame::ModbusAscii.
        Ok(ParsedFrame::ModbusAscii {
            address,
            function_code,
            data,
            checksum_valid,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::framing::config::{LineEnding, RxFramingConfig, RxFramingMode};
    use crate::framing::decoder::FrameDecoder;

    // ── Parser tests ────────────────────────────────────────────────────

    #[test]
    fn at_parser_ok() {
        let p = AtCommandParser;
        let result = p.parse(b"OK").unwrap();
        assert!(matches!(
            result,
            ParsedFrame::AtCommand {
                response_type,
                status: Some(s),
                ..
            } if response_type == "status" && s == "OK"
        ));
    }

    #[test]
    fn at_parser_error() {
        let p = AtCommandParser;
        let result = p.parse(b"ERROR").unwrap();
        assert!(matches!(
            result,
            ParsedFrame::AtCommand {
                response_type,
                status: Some(s),
                ..
            } if response_type == "error" && s == "ERROR"
        ));
    }

    #[test]
    fn at_parser_command_response() {
        let p = AtCommandParser;
        let result = p.parse(b"+CGREG: 0,1").unwrap();
        assert!(matches!(
            result,
            ParsedFrame::AtCommand {
                response_type,
                command: Some(ref c),
                ..
            } if response_type == "response" && c == "CGREG"
        ));
    }

    #[test]
    fn json_parser_valid() {
        let p = JsonLinesParser;
        let result = p.parse(b"{\"key\":\"value\"}").unwrap();
        assert!(matches!(result, ParsedFrame::Json(_)));
    }

    #[test]
    fn json_parser_invalid() {
        let p = JsonLinesParser;
        let result = p.parse(b"not json").unwrap();
        assert!(matches!(result, ParsedFrame::Raw));
    }

    #[test]
    fn json_parser_non_object_is_raw() {
        let p = JsonLinesParser;
        assert!(matches!(p.parse(b"[1,2,3]").unwrap(), ParsedFrame::Raw));
        assert!(matches!(p.parse(b"42").unwrap(), ParsedFrame::Raw));
        assert!(matches!(p.parse(b"\"hi\"").unwrap(), ParsedFrame::Raw));
        assert!(matches!(
            p.parse(b"{\"k\":1}").unwrap(),
            ParsedFrame::Json(_)
        ));
    }

    #[test]
    fn shell_prompt_user() {
        let p = ShellPromptParser { custom: None };
        let result = p.parse(b"$ ").unwrap();
        assert!(
            matches!(result, ParsedFrame::ShellPrompt { prompt_type, .. } if prompt_type == "user")
        );
    }

    #[test]
    fn shell_prompt_root() {
        let p = ShellPromptParser { custom: None };
        let result = p.parse(b"# ").unwrap();
        assert!(
            matches!(result, ParsedFrame::ShellPrompt { prompt_type, .. } if prompt_type == "root")
        );
    }

    #[test]
    fn shell_prompt_host() {
        let p = ShellPromptParser { custom: None };
        let result = p.parse(b"root@host:~# ").unwrap();
        assert!(
            matches!(result, ParsedFrame::ShellPrompt { prompt_type, .. } if prompt_type == "root")
        );
    }

    #[test]
    fn at_parser_empty_input() {
        let p = AtCommandParser;
        let result = p.parse(b"").unwrap();
        assert!(matches!(result, ParsedFrame::Raw));
    }

    #[test]
    fn at_parser_cme_error() {
        let p = AtCommandParser;
        let result = p.parse(b"+CME ERROR: 100").unwrap();
        assert!(matches!(
            result,
            ParsedFrame::AtCommand {
                response_type,
                command: Some(ref c),
                ..
            } if response_type == "response" && c == "CME ERROR"
        ));
    }

    #[test]
    fn at_parser_cms_error() {
        let p = AtCommandParser;
        let result = p.parse(b"+CMS ERROR: 500").unwrap();
        assert!(matches!(
            result,
            ParsedFrame::AtCommand {
                response_type,
                command: Some(ref c),
                ..
            } if response_type == "response" && c == "CMS ERROR"
        ));
    }

    #[test]
    fn json_parser_empty_input() {
        let p = JsonLinesParser;
        let result = p.parse(b"").unwrap();
        assert!(matches!(result, ParsedFrame::Raw));
    }

    #[test]
    fn shell_prompt_empty_input() {
        let p = ShellPromptParser { custom: None };
        let result = p.parse(b"").unwrap();
        assert!(matches!(result, ParsedFrame::Raw));
    }

    #[test]
    fn shell_prompt_custom_regex_invalid() {
        let config = RxFramingConfig {
            mode: RxFramingMode::Line {
                ending: LineEnding::Auto,
            },
            ..Default::default()
        };
        let parser = ParserConfig {
            parser_type: ParserType::ShellPrompt,
            custom_prompt: Some("[invalid".to_string()),
            validate: false,
        };
        match FrameDecoder::new(&config, Some(&parser)) {
            Ok(_) => panic!("invalid regex should be rejected"),
            Err(err) => assert!(err.contains("Invalid prompt regex"), "got: {err}"),
        }
    }

    #[test]
    fn shell_prompt_custom_regex_match() {
        let p = ShellPromptParser {
            custom: Some(regex::bytes::Regex::new("^>>> $").unwrap()),
        };
        let result = p.parse(b">>> ").unwrap();
        assert!(
            matches!(result, ParsedFrame::ShellPrompt { prompt_type, .. } if prompt_type == "custom")
        );
    }

    #[test]
    fn raw_parser_passthrough() {
        let p = RawParser;
        let result = p.parse(b"anything").unwrap();
        assert!(matches!(result, ParsedFrame::Raw));
    }

    // ── NMEA parser tests ──────────────────────────────────────────────

    /// Helper: build a NMEA sentence body with a computed XOR checksum.
    fn nmea_checksum_body(body: &[u8]) -> String {
        format!("{:02X}", xor_checksum(body))
    }

    #[test]
    fn nmea_parser_valid_gga_sentence() {
        let body = b"GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,";
        let cs_hex = nmea_checksum_body(body);
        let sentence =
            format!("GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*{cs_hex}");
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence.as_bytes()).unwrap();
        match result {
            ParsedFrame::Nmea {
                talker_id,
                sentence_type,
                fields,
                checksum_valid,
            } => {
                assert_eq!(talker_id, "GP");
                assert_eq!(sentence_type, "GGA");
                assert_eq!(
                    fields,
                    vec![
                        "123519",
                        "4807.038",
                        "N",
                        "01131.000",
                        "E",
                        "1",
                        "08",
                        "0.9",
                        "545.4",
                        "M",
                        "46.9",
                        "M",
                        "",
                        ""
                    ]
                );
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_valid_gll_sentence() {
        // Known-good sentence from checksums::tests::xor_checksum_known_nmea_sentence
        // Body: "GPGLL,3751.65,N,12226.54,W" → XOR checksum 0x7E
        let sentence = b"GPGLL,3751.65,N,12226.54,W*7E";
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence).unwrap();
        match result {
            ParsedFrame::Nmea {
                talker_id,
                sentence_type,
                fields,
                checksum_valid,
            } => {
                assert_eq!(talker_id, "GP");
                assert_eq!(sentence_type, "GLL");
                assert_eq!(fields, vec!["3751.65", "N", "12226.54", "W"]);
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_ais_sentence_starts_with_bang() {
        // Simple AIS sentence body.
        let body = b"AIVDM,1,1,,B,15M67FC000G?ufbE`H9P<In,0";
        let cs_hex = nmea_checksum_body(body);
        let sentence = format!("AIVDM,1,1,,B,15M67FC000G?ufbE`H9P<In,0*{cs_hex}");
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence.as_bytes()).unwrap();
        match result {
            ParsedFrame::Nmea {
                talker_id,
                sentence_type,
                checksum_valid,
                ..
            } => {
                assert_eq!(talker_id, "AI");
                assert_eq!(sentence_type, "VDM");
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_bad_checksum_returns_error_when_validate_true() {
        // GLL sentence with correct checksum 0x7E, but we pass *00 (wrong).
        let sentence = b"GPGLL,3751.65,N,12226.54,W*00";
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence);
        match result {
            Err(FrameDecodeError::ChecksumMismatch { expected, received }) => {
                assert_eq!(expected, vec![0x7E]);
                assert_eq!(received, vec![0x00]);
            }
            other => panic!("expected ChecksumMismatch error, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_bad_checksum_returns_some_false_when_validate_false() {
        let sentence = b"GPGLL,3751.65,N,12226.54,W*00";
        let p = NmeaParser { validate: false };
        let result = p.parse(sentence).unwrap();
        match result {
            ParsedFrame::Nmea { checksum_valid, .. } => {
                assert_eq!(checksum_valid, Some(false));
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_no_checksum_accepted() {
        let sentence = b"GPGLL,3751.65,N,12226.54,W";
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence).unwrap();
        match result {
            ParsedFrame::Nmea { checksum_valid, .. } => {
                assert_eq!(checksum_valid, None);
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
        // Also test with validate: false
        let p2 = NmeaParser { validate: false };
        let result2 = p2.parse(sentence).unwrap();
        match result2 {
            ParsedFrame::Nmea { checksum_valid, .. } => {
                assert_eq!(checksum_valid, None);
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_non_nmea_frame_returns_raw() {
        let p = NmeaParser { validate: true };
        let result = p.parse(b"hello world").unwrap();
        assert!(matches!(result, ParsedFrame::Raw));

        let result2 = p.parse(b"AT+CGMI").unwrap();
        assert!(matches!(result2, ParsedFrame::Raw));
    }

    #[test]
    fn nmea_parser_invalid_utf8_body_returns_raw() {
        // NMEA spec is ASCII; invalid UTF-8 is non-NMEA → Raw (mirrors Modbus).
        // The NmeaParser now uses from_utf8 (not from_utf8_lossy) for the body.
        let parser = build_parser(&ParserConfig {
            parser_type: ParserType::Nmea,
            custom_prompt: None,
            validate: false,
        })
        .unwrap();
        // Body with a comma (passes the NMEA-shape check) but invalid UTF-8.
        let body: &[u8] = b"GPGLL,\xFF,";
        let result = parser.parse(body).unwrap();
        assert!(
            matches!(result, ParsedFrame::Raw),
            "invalid UTF-8 → Raw, got {result:?}"
        );
    }

    #[test]
    fn nmea_parser_strips_leading_start_char_if_present() {
        // Simulate include_markers: true — the $ is included in the data.
        let body = b"GPGLL,3751.65,N,12226.54,W";
        let cs_hex = nmea_checksum_body(body);
        let sentence = format!("$GPGLL,3751.65,N,12226.54,W*{cs_hex}");
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence.as_bytes()).unwrap();
        match result {
            ParsedFrame::Nmea {
                talker_id,
                sentence_type,
                checksum_valid,
                ..
            } => {
                assert_eq!(talker_id, "GP");
                assert_eq!(sentence_type, "GLL");
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_strips_trailing_crlf_if_present() {
        // Simulate include_markers: true — the trailing \r\n is included.
        let body = b"GPGLL,3751.65,N,12226.54,W";
        let cs_hex = nmea_checksum_body(body);
        let sentence = format!("GPGLL,3751.65,N,12226.54,W*{cs_hex}\r\n");
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence.as_bytes()).unwrap();
        match result {
            ParsedFrame::Nmea {
                talker_id,
                sentence_type,
                checksum_valid,
                ..
            } => {
                assert_eq!(talker_id, "GP");
                assert_eq!(sentence_type, "GLL");
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_proprietary_sentence() {
        // $PGRMZ proprietary sentence without the $.
        let body = b"PGRMZ,2010,f,3";
        let cs_hex = nmea_checksum_body(body);
        let sentence = format!("PGRMZ,2010,f,3*{cs_hex}");
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence.as_bytes()).unwrap();
        match result {
            ParsedFrame::Nmea {
                talker_id,
                sentence_type,
                fields,
                checksum_valid,
            } => {
                // Proprietary $PGRMZ: talker = "P", sentence_type = rest = "GRMZ"
                assert_eq!(talker_id, "P");
                assert_eq!(sentence_type, "GRMZ");
                assert_eq!(fields, vec!["2010", "f", "3"]);
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_non_ascii_body_returns_raw() {
        // Two branches: valid-UTF-8-but-non-ASCII and invalid-UTF-8.
        //
        // Case 1: valid UTF-8 but non-ASCII → is_ascii fails, returns Raw.
        // b"a\xC3\xA9,x" = "aé,x" where é is U+00E9 (2-byte UTF-8, non-ASCII).
        // This passes the contains(',') guard and from_utf8 but hits the ASCII
        // guard — byte index 2 is not a char boundary, so slicing panicked
        // before the ASCII guard was added.
        let p = NmeaParser { validate: true };
        let body1 = b"a\xC3\xA9,x";
        let result1 = p.parse(body1).unwrap();
        assert!(
            matches!(result1, ParsedFrame::Raw),
            "valid-UTF-8 non-ASCII body should return Raw, got {result1:?}"
        );

        // validate: false variant — same behavior.
        let p2 = NmeaParser { validate: false };
        let result1b = p2.parse(body1).unwrap();
        assert!(
            matches!(result1b, ParsedFrame::Raw),
            "valid-UTF-8 non-ASCII body (validate=false) should return Raw, got {result1b:?}"
        );

        // Case 2: invalid UTF-8 → from_utf8 returns Err → Raw.
        // b"\xFFx,y" — 0xFF is a lone byte >0x7F and not a valid UTF-8
        // lead byte. from_utf8 returns Err immediately.
        let body2 = b"\xFFx,y";
        let result2 = p.parse(body2).unwrap();
        assert!(
            matches!(result2, ParsedFrame::Raw),
            "invalid-UTF-8 body should return Raw, got {result2:?}"
        );

        // Sanity: a valid NMEA sentence with the same parser DOES parse to Nmea.
        let sentence = b"GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,";
        let cs_hex = nmea_checksum_body(sentence);
        let valid_sentence =
            format!("GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*{cs_hex}");
        let result_valid = p.parse(valid_sentence.as_bytes()).unwrap();
        assert!(
            matches!(result_valid, ParsedFrame::Nmea { .. }),
            "valid ASCII sentence should parse to Nmea, got {result_valid:?}"
        );
    }

    #[test]
    fn nmea_parser_proprietary_p_prefix_split() {
        // $PGRMM proprietary sentence: talker = "P", sentence_type = rest = "GRMM"
        let body = b"PGRMM,W";
        let cs_hex = nmea_checksum_body(body);
        let sentence = format!("PGRMM,W*{cs_hex}");
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence.as_bytes()).unwrap();
        match result {
            ParsedFrame::Nmea {
                talker_id,
                sentence_type,
                fields,
                checksum_valid,
            } => {
                assert_eq!(talker_id, "P", "proprietary talker should be 'P'");
                assert_eq!(
                    sentence_type, "GRMM",
                    "proprietary sentence_type should be rest after P"
                );
                assert_eq!(fields, vec!["W"]);
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_standard_5char_address_unchanged() {
        // GPGGA standard sentence (5-char address): confirms standard split
        // is not accidentally swallowed by the P-branch.
        let body = b"GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,";
        let cs_hex = nmea_checksum_body(body);
        let sentence =
            format!("GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*{cs_hex}");
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence.as_bytes()).unwrap();
        match result {
            ParsedFrame::Nmea {
                talker_id,
                sentence_type,
                fields,
                checksum_valid,
            } => {
                assert_eq!(talker_id, "GP", "standard talker should be first 2 chars");
                assert_eq!(
                    sentence_type, "GGA",
                    "standard sentence_type should be rest"
                );
                assert_eq!(
                    fields,
                    vec![
                        "123519",
                        "4807.038",
                        "N",
                        "01131.000",
                        "E",
                        "1",
                        "08",
                        "0.9",
                        "545.4",
                        "M",
                        "46.9",
                        "M",
                        "",
                        ""
                    ]
                );
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_invalid_hex_checksum_validate_false_returns_full_body() {
        // Sentence with non-hex checksum chars (*GG), validate: false →
        // full body parse + checksum_valid: Some(false).
        let sentence = b"GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*GG";
        let p = NmeaParser { validate: false };
        let result = p.parse(sentence).unwrap();
        match result {
            ParsedFrame::Nmea {
                talker_id,
                sentence_type,
                fields,
                checksum_valid,
            } => {
                assert_eq!(talker_id, "GP");
                assert_eq!(sentence_type, "GGA");
                assert_eq!(
                    fields,
                    vec![
                        "123519",
                        "4807.038",
                        "N",
                        "01131.000",
                        "E",
                        "1",
                        "08",
                        "0.9",
                        "545.4",
                        "M",
                        "46.9",
                        "M",
                        "",
                        ""
                    ]
                );
                assert_eq!(checksum_valid, Some(false));
            }
            other => panic!("expected Nmea frame with full body, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_too_short_checksum_validate_false_returns_full_body() {
        // Sentence with 1-hex-char checksum (*7), validate: false →
        // full body parse + checksum_valid: Some(false).
        let sentence = b"GPGLL,3751.65,N,12226.54,W*7";
        let p = NmeaParser { validate: false };
        let result = p.parse(sentence).unwrap();
        match result {
            ParsedFrame::Nmea {
                talker_id,
                sentence_type,
                fields,
                checksum_valid,
            } => {
                assert_eq!(talker_id, "GP");
                assert_eq!(sentence_type, "GLL");
                assert_eq!(fields, vec!["3751.65", "N", "12226.54", "W"]);
                assert_eq!(checksum_valid, Some(false));
            }
            other => panic!("expected Nmea frame with full body, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_invalid_hex_checksum_validate_true_returns_err() {
        // Sentence with non-hex checksum chars (*GG), validate: true →
        // Err(ChecksumMismatch). The received vec contains the raw hex bytes.
        let sentence = b"GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*GG";
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence);
        match result {
            Err(FrameDecodeError::ChecksumMismatch { expected, received }) => {
                assert_eq!(expected.len(), 1);
                assert_eq!(received, b"GG".to_vec());
            }
            other => panic!("expected ChecksumMismatch, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_too_short_checksum_validate_true_returns_err() {
        // Sentence with 1-hex-char checksum (*7), validate: true →
        // Err(ChecksumMismatch). The received vec contains the raw hex bytes.
        let sentence = b"GPGLL,3751.65,N,12226.54,W*7";
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence);
        match result {
            Err(FrameDecodeError::ChecksumMismatch { expected, received }) => {
                assert_eq!(expected.len(), 1);
                assert_eq!(received, b"7".to_vec());
            }
            other => panic!("expected ChecksumMismatch, got {other:?}"),
        }
    }

    // ── Modbus ASCII parser unit tests ───────────────────────────────────

    // Helper: compute LRC over a PDU byte slice and return the 1-byte hex string.
    fn modbus_lrc(pdu: &[u8]) -> String {
        format!("{:02X}", lrc(pdu))
    }

    // Helper: build a Modbus ASCII hex body from PDU and append LRC.
    fn modbus_body(hex_pdu: &str) -> String {
        let pdu_bytes: Vec<u8> = (0..hex_pdu.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&hex_pdu[i..i + 2], 16).unwrap())
            .collect();
        let lrc_hex = modbus_lrc(&pdu_bytes);
        format!("{hex_pdu}{lrc_hex}")
    }

    #[test]
    fn modbus_ascii_parser_valid_read_holding_registers() {
        // :010300000001FB\r\n — address=1, fc=3, data=[0,0,0,1], LRC=FB
        let body = modbus_body("010300000001");
        let p = ModbusAsciiParser { validate: true };
        let result = p.parse(body.as_bytes()).unwrap();
        match result {
            ParsedFrame::ModbusAscii {
                address,
                function_code,
                data,
                checksum_valid,
            } => {
                assert_eq!(address, 1);
                assert_eq!(function_code, 3);
                assert_eq!(data, vec![0, 0, 0, 1]);
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected ModbusAscii frame, got {other:?}"),
        }
    }

    #[test]
    fn modbus_ascii_parser_valid_write_single_register() {
        // Address 0x01, FC 0x06, register 0x000A, value 0x0001
        // PDU = [0x01, 0x06, 0x00, 0x0A, 0x00, 0x01]
        // LRC = compute in test
        let pdu = [0x01u8, 0x06, 0x00, 0x0A, 0x00, 0x01];
        let lrc_hex = modbus_lrc(&pdu);
        let body = format!("0106000A0001{lrc_hex}");
        let p = ModbusAsciiParser { validate: true };
        let result = p.parse(body.as_bytes()).unwrap();
        match result {
            ParsedFrame::ModbusAscii {
                address,
                function_code,
                data,
                checksum_valid,
            } => {
                assert_eq!(address, 1);
                assert_eq!(function_code, 6);
                assert_eq!(data, vec![0x00, 0x0A, 0x00, 0x01]);
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected ModbusAscii frame, got {other:?}"),
        }
    }

    #[test]
    fn modbus_ascii_parser_exception_response() {
        // Exception response: address 0x01, FC 0x83 (0x03 | 0x80), exception code 0x02
        let pdu = [0x01u8, 0x83, 0x02];
        let lrc_hex = modbus_lrc(&pdu);
        let body = format!("018302{lrc_hex}");
        let p = ModbusAsciiParser { validate: true };
        let result = p.parse(body.as_bytes()).unwrap();
        match result {
            ParsedFrame::ModbusAscii {
                address,
                function_code,
                data,
                checksum_valid,
            } => {
                assert_eq!(address, 1);
                assert_eq!(function_code, 131); // 0x83
                assert_eq!(data, vec![2]);
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected ModbusAscii frame, got {other:?}"),
        }
    }

    #[test]
    fn modbus_ascii_parser_broadcast_address_zero() {
        let pdu = [0x00u8, 0x03, 0x00, 0x00, 0x00, 0x01];
        let lrc_hex = modbus_lrc(&pdu);
        let body = format!("000300000001{lrc_hex}");
        let p = ModbusAsciiParser { validate: true };
        let result = p.parse(body.as_bytes()).unwrap();
        match result {
            ParsedFrame::ModbusAscii {
                address,
                function_code,
                ..
            } => {
                assert_eq!(address, 0);
                assert_eq!(function_code, 3);
            }
            other => panic!("expected ModbusAscii frame, got {other:?}"),
        }
    }

    #[test]
    fn modbus_ascii_parser_bad_lrc_returns_error_when_validate_true() {
        // Correct LRC is 0xFB, corrupt to 0x00
        let body = b"01030000000100";
        let p = ModbusAsciiParser { validate: true };
        let result = p.parse(body);
        match result {
            Err(FrameDecodeError::ChecksumMismatch { expected, received }) => {
                assert_eq!(expected, vec![0xFB]);
                assert_eq!(received, vec![0x00]);
            }
            other => panic!("expected ChecksumMismatch error, got {other:?}"),
        }
    }

    #[test]
    fn modbus_ascii_parser_bad_lrc_returns_some_false_when_validate_false() {
        let body = b"01030000000100";
        let p = ModbusAsciiParser { validate: false };
        let result = p.parse(body).unwrap();
        match result {
            ParsedFrame::ModbusAscii {
                address: 1,
                function_code: 3,
                checksum_valid: Some(false),
                ..
            } => {}
            other => panic!("expected ModbusAscii with checksum_valid: false, got {other:?}"),
        }
    }

    #[test]
    fn modbus_ascii_parser_lowercase_hex_accepted() {
        let body = b"010300000001fb";
        let p = ModbusAsciiParser { validate: true };
        let result = p.parse(body).unwrap();
        match result {
            ParsedFrame::ModbusAscii {
                address: 1,
                function_code: 3,
                checksum_valid: Some(true),
                ..
            } => {}
            other => panic!("expected valid ModbusAscii, got {other:?}"),
        }
    }

    #[test]
    fn modbus_ascii_parser_non_hex_returns_raw() {
        let p = ModbusAsciiParser { validate: true };
        let result = p.parse(b"hello world").unwrap();
        assert!(matches!(result, ParsedFrame::Raw));
    }

    #[test]
    fn modbus_ascii_parser_odd_length_returns_raw() {
        let p = ModbusAsciiParser { validate: true };
        let result = p.parse(b"010").unwrap();
        assert!(matches!(result, ParsedFrame::Raw));
    }

    #[test]
    fn modbus_ascii_parser_too_short_returns_raw() {
        // "01" → 1 decoded byte, < 3
        let p = ModbusAsciiParser { validate: true };
        let result = p.parse(b"01").unwrap();
        assert!(matches!(result, ParsedFrame::Raw));
    }

    #[test]
    fn modbus_ascii_parser_strips_leading_colon_and_trailing_crlf() {
        // Body with markers included — defensive stripping should still work.
        let pdu = [0x01u8, 0x03, 0x00, 0x00, 0x00, 0x01];
        let lrc_hex = modbus_lrc(&pdu);
        let frame = format!(":010300000001{lrc_hex}\r\n");
        let p = ModbusAsciiParser { validate: true };
        let result = p.parse(frame.as_bytes()).unwrap();
        match result {
            ParsedFrame::ModbusAscii {
                address: 1,
                function_code: 3,
                checksum_valid: Some(true),
                ..
            } => {}
            other => panic!("expected valid ModbusAscii, got {other:?}"),
        }
    }

    #[test]
    fn modbus_ascii_parser_empty_body_returns_raw() {
        let p = ModbusAsciiParser { validate: true };
        let result = p.parse(b"").unwrap();
        assert!(matches!(result, ParsedFrame::Raw));
    }

    #[test]
    fn modbus_ascii_parser_minimal_frame_3_bytes() {
        // address 0x01, function 0xFF, LRC: sum=0x01+0xFF=0x00 (wrap), LRC=0x00
        let body = b"01FF00";
        let p = ModbusAsciiParser { validate: true };
        let result = p.parse(body).unwrap();
        match result {
            ParsedFrame::ModbusAscii {
                address: 1,
                function_code: 255,
                data,
                checksum_valid: Some(true),
            } if data.is_empty() => {}
            other => panic!("expected minimal ModbusAscii frame, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_multi_fragment_ais_first_sentence() {
        use crate::checksums::xor_checksum;

        // AIS multi-fragment messages are NOT reassembled by the parser —
        // each fragment is its own frame. This test pins the first-fragment
        // parse and documents the no-reassembly behavior.
        //
        // Verify the XOR checksum of the body between ! and * (exclusive).
        let checksum_body = b"AIVDM,2,1,3,B,55?MbV02;H0000,0";
        let computed = xor_checksum(checksum_body);
        // Correct XOR is 0x22 (an earlier draft claimed 0x5C).
        assert_eq!(computed, 0x22, "AIS sentence checksum is 0x22, not 0x5C");

        let sentence = b"!AIVDM,2,1,3,B,55?MbV02;H0000,0*22";
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence).unwrap();
        match result {
            ParsedFrame::Nmea {
                talker_id,
                sentence_type,
                fields,
                checksum_valid,
            } => {
                assert_eq!(talker_id, "AI");
                assert_eq!(sentence_type, "VDM");
                assert_eq!(fields, vec!["2", "1", "3", "B", "55?MbV02;H0000", "0"]);
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
    }

    #[test]
    fn nmea_parser_preserves_whitespace_in_fields() {
        use crate::checksums::xor_checksum;

        // The parser uses split(',') without per-field trimming; this test
        // pins that whitespace inside a field is preserved. A future
        // "helpful" refactor adding .trim() per field would break this.
        let body_without_star = b"GPVTG,  48.7  ,T,,,N,,K,N";
        let cs = xor_checksum(body_without_star);
        let cs_hex = format!("{cs:02X}");
        // cs_hex = "58" for this body.
        let sentence = format!(
            "${}*{}",
            std::str::from_utf8(body_without_star).unwrap(),
            cs_hex
        );
        let p = NmeaParser { validate: true };
        let result = p.parse(sentence.as_bytes()).unwrap();
        match result {
            ParsedFrame::Nmea {
                talker_id,
                sentence_type,
                fields,
                checksum_valid,
            } => {
                assert_eq!(talker_id, "GP");
                assert_eq!(sentence_type, "VTG");
                // The first data field (fields[0]) has leading + trailing
                // spaces — they are NOT trimmed.
                assert_eq!(fields[0], "  48.7  ");
                assert_eq!(checksum_valid, Some(true));
            }
            other => panic!("expected Nmea frame, got {other:?}"),
        }
    }

    #[test]
    fn modbus_ascii_parser_non_ascii_body_returns_raw() {
        // Two branches in ModbusAsciiParser::parse that lead to Raw:
        //
        // Case 1: valid UTF-8 but non-ASCII → is_ascii_hexdigit fails.
        // b"01\xC3\xA900" = "01é00" where é is U+00E9 (valid UTF-8, non-
        // hex-digit byte 0xC3). The parser calls from_utf8 (Ok), then
        // is_ascii_hexdigit fails → Raw.
        let p = ModbusAsciiParser { validate: true };
        let body1 = b"01\xC3\xA900";
        let result1 = p.parse(body1).unwrap();
        assert!(
            matches!(result1, ParsedFrame::Raw),
            "non-ASCII valid-UTF-8 body should return Raw, got {result1:?}"
        );

        // Case 2: invalid UTF-8 → from_utf8 returns Err → Raw.
        // b"01\xFF00" — 0xFF is a lone byte >0x7F and not a valid UTF-8
        // continuation. from_utf8 returns Err immediately.
        let body2 = b"01\xFF00";
        let result2 = p.parse(body2).unwrap();
        assert!(
            matches!(result2, ParsedFrame::Raw),
            "invalid-UTF-8 body should return Raw, got {result2:?}"
        );

        // Sanity: a valid hex body with the same parser DOES parse to ModbusAscii.
        let pdu = [0x01u8, 0x03, 0x00, 0x00, 0x00, 0x01];
        let lrc_hex = {
            let cs = lrc(&pdu);
            format!("{cs:02X}")
        };
        let body_valid = format!("010300000001{lrc_hex}");
        let result_valid = p.parse(body_valid.as_bytes()).unwrap();
        assert!(
            matches!(result_valid, ParsedFrame::ModbusAscii { .. }),
            "valid hex body should parse to ModbusAscii, got {result_valid:?}"
        );
    }
}