use core::fmt;
use crate::{sentences::GnssType, SentenceType};
#[derive(Debug, PartialEq)]
pub enum Error<'a> {
Utf8Decoding,
ASCII,
ChecksumMismatch { calculated: u8, found: u8 },
WrongSentenceHeader {
expected: SentenceType,
found: SentenceType,
},
UnknownGnssType(&'a str),
ParsingError(nom::Err<nom::error::Error<&'a str>>),
SentenceLength(usize),
ParameterLength {
max_length: usize,
parameter_length: usize,
},
Unsupported(SentenceType),
Unknown(&'a str),
EmptyNavConfig,
UnknownTalkerId { expected: &'a str, found: &'a str },
DisabledSentence,
}
impl<'a> From<nom::Err<nom::error::Error<&'a str>>> for Error<'a> {
fn from(error: nom::Err<nom::error::Error<&'a str>>) -> Self {
Self::ParsingError(error)
}
}
impl fmt::Display for Error<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Utf8Decoding => {
write!(f, "The provided input was not a valid UTF-8 string")
}
Error::ASCII => write!(f, "Provided input includes non-ASCII characters"),
Error::ChecksumMismatch { calculated, found } => write!(
f,
"Checksum Mismatch(calculated = {}, found = {})",
calculated, found
),
Error::WrongSentenceHeader { expected, found } => write!(
f,
"Wrong Sentence Header (expected = '{}', found = '{}')",
expected, found
),
Error::UnknownGnssType(found) => write!(
f,
"Unknown GNSS type (expected one of '{:?}', found = '{}')",
GnssType::ALL_TYPES,
found
),
Error::ParsingError(e) => write!(f, "Parse error: {}", e),
Error::SentenceLength(size) => write!(
f,
"The sentence was too long to be parsed, current limit is {} characters",
size
),
Error::ParameterLength {
max_length,
parameter_length: _,
} => write!(
f,
"Parameter was too long to fit into string, max length is {}",
max_length
),
Error::Unsupported(sentence) => {
write!(f, "Unsupported NMEA sentence '{}'", sentence)
}
Error::Unknown(sentence) => {
write!(f, "Unknown for the crate NMEA sentence '{}'", sentence)
}
Error::EmptyNavConfig => write!(
f,
"The provided navigation configuration was empty and thus invalid"
),
Error::UnknownTalkerId { expected, found } => write!(
f,
"Unknown Talker ID (expected = '{}', found = '{}')",
expected, found
),
Error::DisabledSentence => {
write!(f, "Sentence is parsable but it's feature is disabled",)
}
}
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for Error<'_> {}