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

Implement core::error::Error for ParsingError #28

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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