Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor parsers #51

Merged
merged 14 commits into from
Sep 14, 2022
34 changes: 18 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
[package]
name = "nmea"
version = "0.3.1"
version = "0.4.0"

authors = [
"Felix Obenhuber <felix@obenhuber.de>",
"Evgeniy A. Dushistov <dushistov@mail.ru>",
"Henrik Böving <hargonix@gmail.com>",
"Lachezar Lechev <elpiel93@gmail.com>",
"AeroRust <aerospace.rust@gmail.com>"
"Felix Obenhuber <felix@obenhuber.de>",
"Evgeniy A. Dushistov <dushistov@mail.ru>",
"Henrik Böving <hargonix@gmail.com>",
"Lachezar Lechev <elpiel93@gmail.com>",
"AeroRust <aerospace.rust@gmail.com>",
]
description = "Simple NMEA 0183 parser"
license = "Apache-2.0"
keywords = ["NMEA", "gps", "glonass", "coordinate", "position"]

categories = ["parser-implementations", "no-std", "embedded"]
repository = "https://github.com/AeroRust/nmea"
keywords = ["NMEA", "gps", "glonass", "coordinate", "position"]
description = "Simple NMEA 0183 parser"
license = "MIT OR Apache-2.0"
documentation = "https://docs.rs/nmea"
repository = "https://github.com/AeroRust/nmea"
readme = "README.md"

edition = "2021"
Expand All @@ -24,20 +26,20 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
nom = { version = "7.1.1", default-features = false }
chrono = { version = "0.4.19", default-features = false }
arrayvec = { version = "0.7.2", default-features = false }
arrayvec = {version = "0.7.2", default-features = false}
chrono = {version = "0.4.19", default-features = false}
heapless = "0.7.15"
nom = {version = "7.1.1", default-features = false}

# we include num-traits only when `std` is not enabled
# because of `fract()` and `trunc()` methods
num-traits = { version = "0.2", default-features = false }
num-traits = {version = "0.2", default-features = false}

[dev-dependencies]
quickcheck = { version = "1.0.3", default-features = false }
approx = "0.5.1"
pretty_assertions = "1"
doc-comment = "0.3"
pretty_assertions = "1"
quickcheck = {version = "1.0.3", default-features = false}

[features]
default = ["std"]
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
[![Version](https://img.shields.io/crates/v/nmea.svg)](https://crates.io/crates/nmea)
[![Build Status](https://github.com/AeroRust/nmea/workflows/CI/badge.svg)](https://github.com/AeroRust/nmea/actions?query=workflow%3ACI+branch%3Amaster)
[![codecov](https://codecov.io/gh/AeroRust/nmea/branch/master/graph/badge.svg)](https://codecov.io/gh/AeroRust/nmea)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/AeroRust/nmea/blob/master/LICENSE.txt)
[![License Apache-2](https://img.shields.io/crates/l/nmea.svg)](./LICENSE-APACHE)

[Complete documentation can be found on www.docs.rs/nmea][doc]

NMEA 0183 sentence parser for Rust.
NMEA 0183 sentence parser for Rust.

Supported sentences:
- BWC
- BOD (untested, not supported by `Nmea::parse()`)
- BWC (not supported by `Nmea::parse()`)
- GBS (untested, not supported by `Nmea::parse()`)
- GGA
- GLL
- GNS
Expand Down Expand Up @@ -76,4 +78,4 @@ This project is licensed under the [Apache-2.0](./LICENSE.txt).

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the project by you, shall be licensed as Apache-2.0,
without any additional terms or conditions.
without any additional terms or conditions.
90 changes: 90 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use core::fmt;

use crate::{sentences::GnssType, SentenceType};

#[derive(Debug, PartialEq)]
pub enum Error<'a> {
/// The provided input was not a proper UTF-8 string
Utf8Decoding,
/// The provided string message contains other apart from ASCII.
ASCII,
/// The checksum of the sentence was corrupt or wrong
ChecksumMismatch { calculated: u8, found: u8 },
/// For some reason a sentence was passed to the wrong sentence specific parser, this error
/// should never happen. First slice is the expected header, second is the found one
WrongSentenceHeader {
expected: SentenceType,
found: SentenceType,
},
/// An unknown [`GnssType`] was found in the NMEA message.
UnknownGnssType(&'a str),
/// The sentence could not be parsed because its format was invalid.
ParsingError(nom::Err<nom::error::Error<&'a str>>),
/// The sentence was too long to be parsed, our current limit is `SENTENCE_MAX_LEN` characters.
SentenceLength(usize),
/// The sentence is recognized but it is not supported by the crate.
Unsupported(SentenceType),
/// The sentence type is unknown for this crate.
Unknown(&'a str),
/// The provided navigation configuration was empty and thus invalid
EmptyNavConfig,
/// Invalid sentence number field in nmea sentence of type GSV
InvalidGsvSentenceNum,
}

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<'a> fmt::Display for Error<'a> {
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::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::InvalidGsvSentenceNum => write!(
f,
"Invalid senetence number field in nmea sentence of type GSV"
),
}
}
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<'a> std::error::Error for Error<'a> {}
Loading