use alloc::{
boxed::Box,
string::{String, ToString},
};
use core::{any::Any, fmt::Display};
#[derive(Debug)]
pub enum Error {
LoaderError { err: elf_loader::Error },
FindLibError { msg: String },
FindSymbolError { msg: String },
IteratorPhdrError { err: Box<dyn Any> },
ParseLdCacheError { msg: String },
InvalidPath,
Unsupported,
#[cfg(feature = "std")]
IO(std::io::Error),
#[cfg(not(feature = "std"))]
IO(String),
}
impl Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::LoaderError { err } => write!(f, "{err}"),
Error::FindLibError { msg } => write!(f, "{msg}"),
Error::FindSymbolError { msg } => write!(f, "{msg}"),
Error::IteratorPhdrError { err } => write!(f, "iterator phdr error: {err:?}"),
Error::ParseLdCacheError { msg } => write!(f, "{msg}"),
Error::InvalidPath => write!(f, "invalid path"),
Error::Unsupported => write!(f, "unsupported"),
#[cfg(feature = "std")]
Error::IO(err) => write!(f, "IO error: {err}"),
#[cfg(not(feature = "std"))]
Error::IO(msg) => write!(f, "IO error: {msg}"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Error::IO(value)
}
}
impl From<elf_loader::Error> for Error {
#[cold]
fn from(value: elf_loader::Error) -> Self {
Error::LoaderError { err: value }
}
}
#[cold]
#[inline(never)]
pub(crate) fn find_lib_error(msg: impl ToString) -> Error {
Error::FindLibError {
msg: msg.to_string(),
}
}
#[cold]
#[inline(never)]
pub(crate) fn find_symbol_error(msg: impl ToString) -> Error {
Error::FindSymbolError {
msg: msg.to_string(),
}
}
#[cold]
#[inline(never)]
pub(crate) fn parse_ld_cache_error(msg: impl ToString) -> Error {
Error::ParseLdCacheError {
msg: msg.to_string(),
}
}