nmea_parser/error.rs
1/*
2Copyright 2020 Timo Saarinen
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use core::fmt;
18use core::num::{ParseIntError, ParseFloatError};
19use alloc::string::String;
20
21/// Parse error returned by `NmeaParser::parse_sentence()`. `String` data type is used instead of
22/// `static &str` because the error messages are expected to contain context-specific details.
23#[derive(Clone, Debug, PartialEq)]
24pub enum ParseError {
25 /// Unsupported (or unimplemented) sentence type
26 UnsupportedSentenceType(String),
27
28 /// NMEA checksum doesn't match
29 CorruptedSentence(String),
30
31 /// The sentence format isn't what expected
32 InvalidSentence(String),
33}
34
35impl From<String> for ParseError {
36 fn from(s: String) -> Self {
37 ParseError::InvalidSentence(s)
38 }
39}
40
41impl From<ParseIntError> for ParseError {
42 fn from(e: ParseIntError) -> Self {
43 ParseError::InvalidSentence(format!("{}", e))
44 }
45}
46
47impl From<ParseFloatError> for ParseError {
48 fn from(e: ParseFloatError) -> Self {
49 ParseError::InvalidSentence(format!("{}", e))
50 }
51}
52
53impl fmt::Display for ParseError {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 match self {
56 ParseError::UnsupportedSentenceType(s) => {
57 write!(f, "Unsupported NMEA sentence type: {}", s)
58 }
59 ParseError::CorruptedSentence(s) => write!(f, "Corrupted NMEA sentence: {}", s),
60 ParseError::InvalidSentence(s) => write!(f, "Invalid NMEA sentence: {}", s),
61 }
62 }
63}