1use std::ffi::{CString, CStr};
2
3pub struct DlDescription(pub(crate) CString);
5
6impl std::fmt::Debug for DlDescription {
7 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
8 std::fmt::Debug::fmt(&self.0, f)
9 }
10}
11
12impl From<&CStr> for DlDescription {
13 fn from(value: &CStr) -> Self {
14 Self(value.into())
15 }
16}
17
18pub struct WindowsError(pub(crate) std::io::Error);
20
21impl std::fmt::Debug for WindowsError {
22 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
23 std::fmt::Debug::fmt(&self.0, f)
24 }
25}
26
27#[derive(Debug)]
29#[non_exhaustive]
30pub enum Error {
31 DlOpen {
33 desc: DlDescription
35 },
36 DlOpenUnknown,
38 DlSym {
40 desc: DlDescription
42 },
43 DlSymUnknown,
45 DlClose {
47 desc: DlDescription
49 },
50 DlCloseUnknown,
52 LoadLibraryExW {
54 source: WindowsError
56 },
57 LoadLibraryExWUnknown,
59 GetModuleHandleExW {
61 source: WindowsError
63 },
64 GetModuleHandleExWUnknown,
66 GetProcAddress {
68 source: WindowsError
70 },
71 GetProcAddressUnknown,
73 FreeLibrary {
75 source: WindowsError
77 },
78 FreeLibraryUnknown,
80 IncompatibleSize,
82 CreateCString {
84 source: std::ffi::NulError
86 },
87 CreateCStringWithTrailing {
89 source: std::ffi::FromBytesWithNulError
91 },
92}
93
94impl std::error::Error for Error {
95 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
96 use Error::*;
97 match *self {
98 CreateCString { ref source } => Some(source),
99 CreateCStringWithTrailing { ref source } => Some(source),
100 LoadLibraryExW { ref source } => Some(&source.0),
101 GetModuleHandleExW { ref source } => Some(&source.0),
102 GetProcAddress { ref source } => Some(&source.0),
103 FreeLibrary { ref source } => Some(&source.0),
104 _ => None,
105 }
106 }
107}
108
109impl std::fmt::Display for Error {
110 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
111 use Error::*;
112 match *self {
113 DlOpen { ref desc } => write!(f, "{}", desc.0.to_string_lossy()),
114 DlOpenUnknown => write!(f, "dlopen failed, but system did not report the error"),
115 DlSym { ref desc } => write!(f, "{}", desc.0.to_string_lossy()),
116 DlSymUnknown => write!(f, "dlsym failed, but system did not report the error"),
117 DlClose { ref desc } => write!(f, "{}", desc.0.to_string_lossy()),
118 DlCloseUnknown => write!(f, "dlclose failed, but system did not report the error"),
119 LoadLibraryExW { .. } => write!(f, "LoadLibraryExW failed"),
120 LoadLibraryExWUnknown =>
121 write!(f, "LoadLibraryExW failed, but system did not report the error"),
122 GetModuleHandleExW { .. } => write!(f, "GetModuleHandleExW failed"),
123 GetModuleHandleExWUnknown =>
124 write!(f, "GetModuleHandleExWUnknown failed, but system did not report the error"),
125 GetProcAddress { .. } => write!(f, "GetProcAddress failed"),
126 GetProcAddressUnknown =>
127 write!(f, "GetProcAddress failed, but system did not report the error"),
128 FreeLibrary { .. } => write!(f, "FreeLibrary failed"),
129 FreeLibraryUnknown =>
130 write!(f, "FreeLibrary failed, but system did not report the error"),
131 CreateCString { .. } => write!(f, "could not create a C string from bytes"),
132 CreateCStringWithTrailing { .. } =>
133 write!(f, "could not create a C string from bytes with trailing null"),
134 IncompatibleSize => write!(f, "requested type cannot possibly work"),
135 }
136 }
137}