1use std::ffi::CString;
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
12pub struct WindowsError(pub(crate) std::io::Error);
14
15impl std::fmt::Debug for WindowsError {
16 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
17 std::fmt::Debug::fmt(&self.0, f)
18 }
19}
20
21#[derive(Debug)]
23#[non_exhaustive]
24pub enum Error {
25 DlOpen {
27 desc: DlDescription
29 },
30 DlOpenUnknown,
32 DlSym {
34 desc: DlDescription
36 },
37 DlSymUnknown,
39 DlClose {
41 desc: DlDescription
43 },
44 DlCloseUnknown,
46 LoadLibraryExW {
48 source: WindowsError
50 },
51 LoadLibraryExWUnknown,
53 GetModuleHandleExW {
55 source: WindowsError
57 },
58 GetModuleHandleExWUnknown,
60 GetProcAddress {
62 source: WindowsError
64 },
65 GetProcAddressUnknown,
67 FreeLibrary {
69 source: WindowsError
71 },
72 FreeLibraryUnknown,
74 IncompatibleSize,
76 CreateCString {
78 source: std::ffi::NulError
80 },
81 CreateCStringWithTrailing {
83 source: std::ffi::FromBytesWithNulError
85 },
86}
87
88impl std::error::Error for Error {
89 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
90 use Error::*;
91 match *self {
92 CreateCString { ref source } => Some(source),
93 CreateCStringWithTrailing { ref source } => Some(source),
94 LoadLibraryExW { ref source } => Some(&source.0),
95 GetProcAddress { ref source } => Some(&source.0),
96 FreeLibrary { ref source } => Some(&source.0),
97 _ => None,
98 }
99 }
100}
101
102impl std::fmt::Display for Error {
103 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
104 use Error::*;
105 match *self {
106 DlOpen { ref desc } => write!(f, "{}", desc.0.to_string_lossy()),
107 DlOpenUnknown => write!(f, "dlopen failed, but system did not report the error"),
108 DlSym { ref desc } => write!(f, "{}", desc.0.to_string_lossy()),
109 DlSymUnknown => write!(f, "dlsym failed, but system did not report the error"),
110 DlClose { ref desc } => write!(f, "{}", desc.0.to_string_lossy()),
111 DlCloseUnknown => write!(f, "dlclose failed, but system did not report the error"),
112 LoadLibraryExW { .. } => write!(f, "LoadLibraryExW failed"),
113 LoadLibraryExWUnknown =>
114 write!(f, "LoadLibraryExW failed, but system did not report the error"),
115 GetModuleHandleExW { .. } => write!(f, "GetModuleHandleExW failed"),
116 GetModuleHandleExWUnknown =>
117 write!(f, "GetModuleHandleExWUnknown failed, but system did not report the error"),
118 GetProcAddress { .. } => write!(f, "GetProcAddress failed"),
119 GetProcAddressUnknown =>
120 write!(f, "GetProcAddress failed, but system did not report the error"),
121 FreeLibrary { .. } => write!(f, "FreeLibrary failed"),
122 FreeLibraryUnknown =>
123 write!(f, "FreeLibrary failed, but system did not report the error"),
124 CreateCString { .. } => write!(f, "could not create a C string from bytes"),
125 CreateCStringWithTrailing { .. } =>
126 write!(f, "could not create a C string from bytes with trailing null"),
127 IncompatibleSize => write!(f, "requested type cannot possibly work"),
128 }
129 }
130}