use alloc::borrow::Cow;
use core::fmt::{Debug, Display};
#[derive(Debug)]
pub enum Error {
Io {
msg: Cow<'static, str>,
},
Mmap {
msg: Cow<'static, str>,
},
Relocation {
msg: Cow<'static, str>,
},
ParseDynamic {
msg: Cow<'static, str>,
},
ParseEhdr {
msg: Cow<'static, str>,
},
ParsePhdr {
msg: Cow<'static, str>,
},
Custom {
msg: Cow<'static, str>,
},
Tls {
msg: Cow<'static, str>,
},
}
impl Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::Io { msg } => write!(f, "I/O error: {msg}"),
Error::Mmap { msg } => write!(f, "Memory mapping error: {msg}"),
Error::Relocation { msg, .. } => write!(f, "Relocation error: {msg}"),
Error::ParseDynamic { msg } => write!(f, "Dynamic section parsing error: {msg}"),
Error::ParseEhdr { msg } => write!(f, "ELF header parsing error: {msg}"),
Error::ParsePhdr { msg, .. } => write!(f, "Program header parsing error: {msg}"),
Error::Custom { msg } => write!(f, "Custom error: {msg}"),
Error::Tls { msg } => write!(f, "TLS error: {msg}"),
}
}
}
impl core::error::Error for Error {}
#[cold]
#[inline(never)]
#[allow(unused)]
pub(crate) fn io_error(msg: impl Into<Cow<'static, str>>) -> Error {
Error::Io { msg: msg.into() }
}
#[cold]
#[inline(never)]
pub(crate) fn relocate_error(msg: impl Into<Cow<'static, str>>) -> Error {
Error::Relocation { msg: msg.into() }
}
#[cold]
#[inline(never)]
pub(crate) fn parse_dynamic_error(msg: impl Into<Cow<'static, str>>) -> Error {
Error::ParseDynamic { msg: msg.into() }
}
#[cold]
#[inline(never)]
pub(crate) fn parse_ehdr_error(msg: impl Into<Cow<'static, str>>) -> Error {
Error::ParseEhdr { msg: msg.into() }
}
#[cold]
#[inline(never)]
#[allow(unused)]
pub fn custom_error(msg: impl Into<Cow<'static, str>>) -> Error {
Error::Custom { msg: msg.into() }
}
#[cold]
#[inline(never)]
pub(crate) fn tls_error(msg: impl Into<Cow<'static, str>>) -> Error {
Error::Tls { msg: msg.into() }
}