nmea0183_parser/
error.rs

1//! # Error Types
2//!
3//! This module defines the error types used throughout the NMEA parsing library.
4
5use nom::error::{ErrorKind, FromExternalError, ParseError};
6use std::fmt::Debug;
7
8/// Holds the result of parsing functions.
9///
10/// It depends on the input type `I`, the output type `O`, and the error type `E`
11/// (by default `nom::error::Error<I>`).
12///
13/// The `Ok` side is a pair containing the remainder of the input (the part of the data that
14/// was not parsed) and the produced value. The `Err` side contains an instance of `nom::Err`.
15///
16/// Outside of the parsing code, you can use the [nom::Finish::finish] method to convert
17/// it to a more common result type.
18pub type IResult<I, O, E = nom::error::Error<I>> = nom::IResult<I, O, Error<I, E>>;
19
20/// Represents all possible errors that can occur during NMEA message parsing.
21///
22/// This enum covers various failure modes including input validation,
23/// checksum verification, and parsing errors.
24#[derive(Debug, PartialEq)]
25pub enum Error<I, E> {
26    /// The provided input contains non-ASCII characters.
27    ///
28    /// NMEA messages must be ASCII-only for proper parsing and checksum calculation.
29    NonAscii,
30
31    /// The checksum of the sentence was corrupt or incorrect.
32    ///
33    /// Contains both the expected checksum (calculated from the message content)
34    /// and the actual checksum found in the message.
35    ChecksumMismatch {
36        /// The checksum calculated from the message content
37        expected: u8,
38        /// The checksum found in the message
39        found: u8,
40    },
41
42    /// The sentence could not be parsed because its format was invalid.
43    ///
44    /// This wraps nom's standard parsing errors and provides context about
45    /// what went wrong during parsing.
46    ParsingError(E),
47
48    /// The message type is not recognized by the parser.
49    ///
50    /// This variant is used when a valid NMEA sentence is encountered, but the
51    /// parser does not implement handling for this specific message type.
52    /// The message type that caused the error is provided for reference.
53    UnrecognizedMessage(I),
54
55    /// A field in the NMEA sentence was invalid.
56    ///
57    /// This error occurs when a specific field in the NMEA sentence does not
58    /// conform to the expected format, type, or value range.
59    ///
60    /// Contains the input that caused the error.
61    InvalidField(I),
62
63    /// An unknown error occurred.
64    ///
65    /// This is a catch-all for unexpected error conditions.
66    Unknown,
67}
68
69impl<I, E> ParseError<I> for Error<I, E>
70where
71    E: ParseError<I>,
72{
73    fn from_error_kind(input: I, kind: ErrorKind) -> Self {
74        Error::ParsingError(E::from_error_kind(input, kind))
75    }
76
77    fn append(_: I, _: ErrorKind, other: Self) -> Self {
78        other
79    }
80}
81
82impl<I, E, EX> FromExternalError<I, EX> for Error<I, E>
83where
84    E: FromExternalError<I, EX>,
85{
86    fn from_external_error(input: I, kind: ErrorKind, e: EX) -> Self {
87        Error::ParsingError(E::from_external_error(input, kind, e))
88    }
89}