use std::ffi::{CString, CStr};
pub struct DlDescription(pub(crate) CString);
impl std::fmt::Debug for DlDescription {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.0, f)
}
}
impl From<&CStr> for DlDescription {
fn from(value: &CStr) -> Self {
Self(value.into())
}
}
pub struct WindowsError(pub(crate) std::io::Error);
impl std::fmt::Debug for WindowsError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.0, f)
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
DlOpen {
desc: DlDescription
},
DlOpenUnknown,
DlSym {
desc: DlDescription
},
DlSymUnknown,
DlClose {
desc: DlDescription
},
DlCloseUnknown,
LoadLibraryExW {
source: WindowsError
},
LoadLibraryExWUnknown,
GetModuleHandleExW {
source: WindowsError
},
GetModuleHandleExWUnknown,
GetProcAddress {
source: WindowsError
},
GetProcAddressUnknown,
FreeLibrary {
source: WindowsError
},
FreeLibraryUnknown,
IncompatibleSize,
CreateCString {
source: std::ffi::NulError
},
CreateCStringWithTrailing {
source: std::ffi::FromBytesWithNulError
},
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use Error::*;
match *self {
CreateCString { ref source } => Some(source),
CreateCStringWithTrailing { ref source } => Some(source),
LoadLibraryExW { ref source } => Some(&source.0),
GetProcAddress { ref source } => Some(&source.0),
FreeLibrary { ref source } => Some(&source.0),
_ => None,
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
use Error::*;
match *self {
DlOpen { ref desc } => write!(f, "{}", desc.0.to_string_lossy()),
DlOpenUnknown => write!(f, "dlopen failed, but system did not report the error"),
DlSym { ref desc } => write!(f, "{}", desc.0.to_string_lossy()),
DlSymUnknown => write!(f, "dlsym failed, but system did not report the error"),
DlClose { ref desc } => write!(f, "{}", desc.0.to_string_lossy()),
DlCloseUnknown => write!(f, "dlclose failed, but system did not report the error"),
LoadLibraryExW { .. } => write!(f, "LoadLibraryExW failed"),
LoadLibraryExWUnknown =>
write!(f, "LoadLibraryExW failed, but system did not report the error"),
GetModuleHandleExW { .. } => write!(f, "GetModuleHandleExW failed"),
GetModuleHandleExWUnknown =>
write!(f, "GetModuleHandleExWUnknown failed, but system did not report the error"),
GetProcAddress { .. } => write!(f, "GetProcAddress failed"),
GetProcAddressUnknown =>
write!(f, "GetProcAddress failed, but system did not report the error"),
FreeLibrary { .. } => write!(f, "FreeLibrary failed"),
FreeLibraryUnknown =>
write!(f, "FreeLibrary failed, but system did not report the error"),
CreateCString { .. } => write!(f, "could not create a C string from bytes"),
CreateCStringWithTrailing { .. } =>
write!(f, "could not create a C string from bytes with trailing null"),
IncompatibleSize => write!(f, "requested type cannot possibly work"),
}
}
}