pub struct Nmea0183ParserBuilder { /* private fields */ }Expand description
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
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
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)Implementations§
Source§impl Nmea0183ParserBuilder
impl Nmea0183ParserBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new NMEA 0183 parser builder with default settings.
The default settings are:
- Checksum mode:
ChecksumMode::Required - Line ending mode:
LineEndingMode::Required
Sourcepub fn checksum_mode(self, mode: ChecksumMode) -> Self
pub fn checksum_mode(self, mode: ChecksumMode) -> Self
Sets the checksum mode for the parser.
§Arguments
mode- The desired checksum mode:ChecksumMode::Required: Checksum must be present and validChecksumMode::Optional: Checksum may be absent or must be valid if present
Sourcepub fn line_ending_mode(self, mode: LineEndingMode) -> Self
pub fn line_ending_mode(self, mode: LineEndingMode) -> Self
Sets the line ending mode for the parser.
§Arguments
mode- The desired line ending mode:LineEndingMode::Required: Message must end with\r\nLineEndingMode::Forbidden: Message must not end with\r\n
Sourcepub fn build<'a, I, O, F, E>(
self,
content_parser: F,
) -> impl FnMut(I) -> IResult<I, O, E>
pub fn build<'a, I, O, F, E>( self, content_parser: F, ) -> impl FnMut(I) -> IResult<I, O, E>
Builds the NMEA 0183-style parser with the configured settings.
This method takes a user-provided parser function that will handle the content of the message after the framing has been processed.
The returned parser will:
- Validate that the input is ASCII-only
- Expect the message to start with
$ - Extract the message content (everything before
*CCor\r\n) - Parse and validate the checksum using the provided checksum parser
- Call the user-provided parser on the message content
§Arguments
content_parser- User-provided parser for the message content.
§Returns
A parser function that takes an input and returns a result containing the parsed content or an error if the input does not conform to the expected NMEA 0183 format.