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
//! # NMEA 0183 Message Parser
//!
//! This module provides the main parsing functionality for NMEA 0183-style messages.
//! It handles the standard NMEA 0183 format: `$HHH,D1,D2,...,Dn*CC\r\n`
//!
//! The parser is configurable to handle variations in:
//! - Checksum requirements (required or optional)
//! - Line ending requirements (CRLF required or forbidden)
use ;
use crate::;
/// Defines how the parser should handle NMEA message checksums.
///
/// NMEA 0183 messages can include an optional checksum in the format `*CC` where
/// CC is a two-digit hexadecimal value representing the XOR of all bytes in the
/// message content (excluding the '$' prefix and '*' delimiter).
/// Defines how the parser should handle CRLF line endings.
///
/// NMEA 0183 messages typically end with a carriage return and line feed (`\r\n`),
/// but some systems or applications may omit these characters.
/// Creates a configurable NMEA 0183-style parser factory.
///
/// This struct allows you to configure the NMEA 0183 framing parser with different
/// checksum and line ending modes before building the final parser.
///
/// It uses the builder pattern to allow for flexible configuration of the parser settings.
///
/// # Examples
///
/// ```rust
/// use nmea0183_parser::{IResult, Nmea0183ParserBuilder};
///
/// fn content_parser(input: &str) -> IResult<&str, Vec<&str>> {
/// Ok(("", input.split(',').collect()))
/// }
///
/// // Create a parser with required checksum and CRLF
/// let parser_factory = Nmea0183ParserBuilder::new();
/// let mut parser = parser_factory.build(content_parser);
/// ```
///
/// ## Configuration
///
/// ```rust
/// use nmea0183_parser::{ChecksumMode, IResult, LineEndingMode, Nmea0183ParserBuilder};
/// use nom::Parser;
///
/// fn content_parser(i: &str) -> IResult<&str, bool> {
/// Ok((i, true))
/// }
///
/// // Strict: checksum and CRLF both required
/// let mut strict_parser = Nmea0183ParserBuilder::new()
/// .checksum_mode(ChecksumMode::Required)
/// .line_ending_mode(LineEndingMode::Required)
/// .build(content_parser);
/// assert!(strict_parser.parse("$GPGGA,data*6A\r\n").is_ok());
/// assert!(strict_parser.parse("$GPGGA,data*6A").is_err()); // (missing CRLF)
/// assert!(strict_parser.parse("$GPGGA,data\r\n").is_err()); // (missing checksum)
///
/// // Checksum required, no CRLF allowed
/// let mut no_crlf_parser = Nmea0183ParserBuilder::new()
/// .checksum_mode(ChecksumMode::Required)
/// .line_ending_mode(LineEndingMode::Forbidden)
/// .build(content_parser);
/// assert!(no_crlf_parser.parse("$GPGGA,data*6A").is_ok());
/// assert!(no_crlf_parser.parse("$GPGGA,data*6A\r\n").is_err()); // (CRLF present)
/// assert!(no_crlf_parser.parse("$GPGGA,data").is_err()); // (missing checksum)
///
/// // Checksum optional, CRLF required
/// let mut optional_checksum_parser = Nmea0183ParserBuilder::new()
/// .checksum_mode(ChecksumMode::Optional)
/// .line_ending_mode(LineEndingMode::Required)
/// .build(content_parser);
/// assert!(optional_checksum_parser.parse("$GPGGA,data*6A\r\n").is_ok()); // (with valid checksum)
/// assert!(optional_checksum_parser.parse("$GPGGA,data\r\n").is_ok()); // (without checksum)
/// assert!(optional_checksum_parser.parse("$GPGGA,data*99\r\n").is_err()); // (invalid checksum)
/// assert!(optional_checksum_parser.parse("$GPGGA,data*6A").is_err()); // (missing CRLF)
///
/// // Lenient: checksum optional, CRLF forbidden
/// let mut lenient_parser = Nmea0183ParserBuilder::new()
/// .checksum_mode(ChecksumMode::Optional)
/// .line_ending_mode(LineEndingMode::Forbidden)
/// .build(content_parser);
/// assert!(lenient_parser.parse("$GPGGA,data*6A").is_ok()); // (with valid checksum)
/// assert!(lenient_parser.parse("$GPGGA,data").is_ok()); // (without checksum)
/// assert!(lenient_parser.parse("$GPGGA,data*99").is_err()); // (invalid checksum)
/// assert!(lenient_parser.parse("$GPGGA,data\r\n").is_err()); // (CRLF present)
/// ```
/// Creates a parser for checksum and CRLF based on configuration.
///
/// This function returns a parser that can handle the end portion of NMEA messages,
/// specifically the checksum (if present) and line ending (if present).
///
/// # Arguments
///
/// * `cc` - Checksum requirement:
/// - [`ChecksumMode::Required`]: Parser will fail if no '*CC' is present
/// - [`ChecksumMode::Optional`]: Parser accepts messages with or without '*CC',
/// but validates checksum if present
/// * `crlf` - CRLF requirement:
/// - [`LineEndingMode::Required`]: Parser will fail if message doesn't end with `\r\n`
/// - [`LineEndingMode::Forbidden`]: Parser will fail if message ends with `\r\n`
///
/// # Returns
///
/// A parser that extracts the checksum value ([`None`] if no checksum present).
///
/// # Message Format Expectations
///
/// - cc=[`ChecksumMode::Required`], crlf=[`LineEndingMode::Required`]: Expects `*CC\r\n`
/// - cc=[`ChecksumMode::Required`], crlf=[`LineEndingMode::Forbidden`]: Expects `*CC`
/// - cc=[`ChecksumMode::Optional`], crlf=[`LineEndingMode::Required`]: Expects `\r\n` or `*CC\r\n`
/// - cc=[`ChecksumMode::Optional`], crlf=[`LineEndingMode::Forbidden`]: Expects nothing or `*CC`
///
/// # Examples
///
/// ```rust,ignore
/// use nmea0183_parser::{ChecksumMode, IResult, LineEndingMode, checksum_crlf};
/// use nom::Parser;
///
/// // Required checksum, required CRLF
/// let mut parser = checksum_crlf(ChecksumMode::Required, LineEndingMode::Required);
/// let result: IResult<_, _> = parser.parse("*51\r\n");
/// assert_eq!(result, Ok(("", Some(0x51))));
///
/// // Optional checksum, forbidden CRLF
/// let mut parser = checksum_crlf(ChecksumMode::Optional, LineEndingMode::Forbidden);
/// let result1: IResult<_, _> = parser.parse("*51"); // With checksum
/// let result2: IResult<_, _> = parser.parse(""); // Without checksum
/// assert!(result1.is_ok());
/// assert!(result2.is_ok());
/// ```
/// Parses CRLF line endings based on configuration.
///
/// This function handles the parsing of carriage return and line feed characters
/// at the end of NMEA messages, with support for both required and forbidden modes.
///
/// # Arguments
///
/// * `crlf` - CRLF requirement:
/// - [`LineEndingMode::Required`]: Parser will fail if message doesn't end with `\r\n`
/// - [`LineEndingMode::Forbidden`]: Parser will fail if message ends with `\r\n`
///
/// # Returns
///
/// A parser function that validates CRLF presence according to the configuration.
///
/// # Examples
///
/// ```rust,ignore
/// use nmea0183_parser::{IResult, LineEndingMode, crlf};
/// use nom::Parser;
///
/// // CRLF required
/// let mut parser = crlf(LineEndingMode::Required);
/// let result: IResult<_, _> = parser.parse("data\r\n");
/// assert_eq!(result, Ok(("data", ())));
///
/// // CRLF forbidden
/// let mut parser = crlf(LineEndingMode::Forbidden);
/// let result: IResult<_, _> = parser.parse("data");
/// assert_eq!(result, Ok(("data", ())));
/// ```
/// Calculates the NMEA 0183 checksum for the given message content.
///
/// The NMEA 0183 checksum is calculated by performing an XOR (exclusive OR) operation
/// on all bytes in the message content. This includes everything between the '$' prefix
/// and the '*' checksum delimiter, but excludes both the '$' and '*' characters themselves.
///
/// # Algorithm
///
/// 1. Initialize checksum to 0
/// 2. For each byte in the message content:
/// - XOR the current checksum with the byte value
/// 3. The final result is an 8-bit value (0-255)
///
/// # Arguments
///
/// * `input` - The message content to calculate checksum for (without '$' prefix or '*' delimiter)
///
/// # Returns
///
/// A tuple of (input, checksum) where:
/// - `input` is returned unchanged (zero-copy)
/// - `checksum` is the calculated XOR value as a u8
///
/// # NMEA 0183 Standard
///
/// According to the NMEA 0183 standard:
/// - The checksum is represented as a two-digit hexadecimal number
/// - It appears after the '*' character at the end of the sentence
/// - Example: `$GPGGA,123456,data*41` where '41' is the hex representation of the checksum
///
/// # Performance Notes
///
/// This function uses `fold()` with XOR operation, which is:
/// - Efficient for small to medium message sizes (typical NMEA messages are < 100 bytes)
/// - Single-pass algorithm with O(n) time complexity
/// - No memory allocation (zero-copy input handling)
/// Ensures that the parser consumes all input.
///
/// This is a convenience function for the common case of wanting to ensure that
/// a parser consumes the entire input with no remainder.
///
/// # Arguments
///
/// * `f` - The parser to run
/// * `e` - Error kind to return if input is not fully consumed
///
/// # Examples
///
/// ```ignore
/// use nmea0183_parser::nmea0183::consumed;
/// use nom::{IResult, Parser, bytes::complete::take, error::ErrorKind};
///
/// // Parse all 3 bytes
/// let mut parser = consumed(take(3u8), ErrorKind::Count);
/// let result: IResult<_, _> = parser.parse("abc");
/// assert!(result.is_ok());
///
/// // This would fail because not all input is consumed
/// let result = parser.parse("abcd");
/// assert!(result.is_err());
/// ```