Nmea0183ParserBuilder

Struct Nmea0183ParserBuilder 

Source
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

Source

pub fn new() -> Self

Creates a new NMEA 0183 parser builder with default settings.

The default settings are:

Source

pub fn checksum_mode(self, mode: ChecksumMode) -> Self

Sets the checksum mode for the parser.

§Arguments
Source

pub fn line_ending_mode(self, mode: LineEndingMode) -> Self

Sets the line ending mode for the parser.

§Arguments
Source

pub fn build<'a, I, O, F, E>( self, content_parser: F, ) -> impl FnMut(I) -> IResult<I, O, E>
where I: Input + AsBytes + Compare<&'a str> + FindSubstring<&'a str>, <I as Input>::Item: AsChar, F: Parser<I, Output = O, Error = Error<I, E>>, E: ParseError<I>,

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 *CC or \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.

Trait Implementations§

Source§

impl Default for Nmea0183ParserBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.