Skip to content

Commit

Permalink
Remember to implement std::error::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
squidpickles committed Sep 17, 2022
1 parent 8517f88 commit dfa1475
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 68 deletions.
19 changes: 17 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,29 @@ mod err {
/// Custom `Result` to prepopulate `Error` type
pub type Result<T> = lib::std::result::Result<T, Error>;
/// A general error in parsing an AIS message
#[derive(Debug)]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Error {
//#[error("invalid NMEA sentence: '{msg}'")]
Nmea { msg: String },
//#[error("checksum mismatch; expected: {expected:#X}, received: {found:#X}")]
Checksum { expected: u8, found: u8 },
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let msg = match self {
Self::Nmea { msg } => format!("Error parsing NMEA content: {}", msg),
Self::Checksum { expected, found } => {
format!("Checksum error; expected 0x{expected:x}, found 0x{found:x}")
}
};
f.write_str(&msg)
}
}

impl From<&str> for Error {
fn from(err: &str) -> Self {
Self::Nmea { msg: err.into() }
Expand Down Expand Up @@ -63,7 +78,7 @@ mod err {
/// Custom `Result` to prepopulate `Error` type
pub type Result<T> = lib::std::result::Result<T, Error>;
/// A general error in parsing an AIS message
#[derive(Debug)]
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Error {
//#[error("invalid NMEA sentence: '{msg}'")]
Nmea { msg: &'static str },
Expand Down
66 changes: 0 additions & 66 deletions src/errors_nostd.rs

This file was deleted.

0 comments on commit dfa1475

Please sign in to comment.