Skip to content

Commit

Permalink
Implement core::error::Error for ParsingError
Browse files Browse the repository at this point in the history
  • Loading branch information
jlxip committed Jan 11, 2023
1 parent da1e25f commit bddf58a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ name = "elf"
default = ["std", "to_str"]
std = []
to_str = []
nightly = []
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
//! ```

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(all(feature = "nightly", not(feature = "std")), feature(error_in_core))]

pub mod abi;

Expand Down
24 changes: 23 additions & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,34 @@ impl std::error::Error for ParseError {
ParseError::Utf8Error(ref err) => Some(err),
ParseError::TryFromSliceError(ref err) => Some(err),
ParseError::TryFromIntError(ref err) => Some(err),
#[cfg(feature = "std")]
ParseError::IOError(ref err) => Some(err),
}
}
}

#[cfg(all(feature = "nightly", not(feature = "std")))]
impl core::error::Error for ParseError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match *self {
ParseError::BadMagic(_) => None,
ParseError::UnsupportedElfClass(_) => None,
ParseError::UnsupportedElfEndianness(_) => None,
ParseError::UnsupportedVersion(_) => None,
ParseError::BadOffset(_) => None,
ParseError::StringTableMissingNul(_) => None,
ParseError::BadEntsize(_) => None,
ParseError::UnexpectedSectionType(_) => None,
ParseError::UnexpectedSegmentType(_) => None,
ParseError::UnexpectedAlignment(_) => None,
ParseError::SliceReadError(_) => None,
ParseError::IntegerOverflow => None,
ParseError::Utf8Error(ref err) => Some(err),
ParseError::TryFromSliceError(ref err) => Some(err),
ParseError::TryFromIntError(ref err) => Some(err),
}
}
}

impl core::fmt::Display for ParseError {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match *self {
Expand Down

0 comments on commit bddf58a

Please sign in to comment.