1use super::*;
2use core::num::NonZeroI32;
3
4#[expect(unused_imports)]
5use core::mem::size_of;
6
7#[derive(Clone)]
29pub struct Error {
30 code: NonZeroI32,
35
36 info: ErrorInfo,
38}
39
40const S_EMPTY_ERROR: NonZeroI32 = const_nonzero_i32(u32::from_be_bytes(*b"S_OK") as i32);
43
44const fn const_nonzero_i32(i: i32) -> NonZeroI32 {
48 if let Some(nz) = NonZeroI32::new(i) {
49 nz
50 } else {
51 panic!();
52 }
53}
54
55fn nonzero_hresult(hr: HRESULT) -> NonZeroI32 {
56 if let Some(nz) = NonZeroI32::new(hr.0) {
57 nz
58 } else {
59 S_EMPTY_ERROR
60 }
61}
62
63impl Error {
64 pub const fn empty() -> Self {
66 Self {
67 code: S_EMPTY_ERROR,
68 info: ErrorInfo::empty(),
69 }
70 }
71
72 pub fn new<T: AsRef<str>>(code: HRESULT, message: T) -> Self {
75 let message: &str = message.as_ref();
76 if message.is_empty() {
77 Self::from_hresult(code)
78 } else {
79 ErrorInfo::originate_error(code, message);
80 code.into()
81 }
82 }
83
84 pub fn from_hresult(code: HRESULT) -> Self {
86 Self {
87 code: nonzero_hresult(code),
88 info: ErrorInfo::empty(),
89 }
90 }
91
92 pub fn from_thread() -> Self {
94 Self::from_hresult(HRESULT::from_thread())
95 }
96
97 pub const fn code(&self) -> HRESULT {
99 if self.code.get() == S_EMPTY_ERROR.get() {
100 HRESULT(0)
101 } else {
102 HRESULT(self.code.get())
103 }
104 }
105
106 pub fn message(&self) -> String {
108 if let Some(message) = self.info.message() {
109 return message;
110 }
111
112 self.code().message()
114 }
115
116 #[cfg(windows)]
118 pub fn as_ptr(&self) -> *mut core::ffi::c_void {
119 self.info.as_ptr()
120 }
121}
122
123impl core::error::Error for Error {}
124
125impl From<Error> for HRESULT {
126 fn from(error: Error) -> Self {
127 let code = error.code();
128 error.info.into_thread();
129 code
130 }
131}
132
133impl From<HRESULT> for Error {
134 fn from(code: HRESULT) -> Self {
135 Self {
136 code: nonzero_hresult(code),
137 info: ErrorInfo::from_thread(),
138 }
139 }
140}
141
142#[cfg(feature = "std")]
143impl From<Error> for std::io::Error {
144 fn from(from: Error) -> Self {
145 if let Some(win32) = WIN32_ERROR::from_error(&from) {
153 Self::from_raw_os_error(win32.0 as i32)
154 } else {
155 Self::from_raw_os_error(from.code().0)
156 }
157 }
158}
159
160#[cfg(feature = "std")]
161impl From<std::io::Error> for Error {
162 fn from(from: std::io::Error) -> Self {
163 match from.raw_os_error() {
164 Some(status) => WIN32_ERROR(status as u32).into(),
165 None => HRESULT(E_UNEXPECTED).into(),
166 }
167 }
168}
169
170impl From<alloc::string::FromUtf16Error> for Error {
171 fn from(_: alloc::string::FromUtf16Error) -> Self {
172 WIN32_ERROR(ERROR_NO_UNICODE_TRANSLATION as u32).into()
173 }
174}
175
176impl From<alloc::string::FromUtf8Error> for Error {
177 fn from(_: alloc::string::FromUtf8Error) -> Self {
178 WIN32_ERROR(ERROR_NO_UNICODE_TRANSLATION as u32).into()
179 }
180}
181
182impl From<core::num::TryFromIntError> for Error {
183 fn from(_: core::num::TryFromIntError) -> Self {
184 WIN32_ERROR(ERROR_INVALID_DATA as u32).into()
185 }
186}
187
188impl core::fmt::Debug for Error {
189 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
190 let mut debug = fmt.debug_struct("Error");
191 debug
192 .field("code", &self.code())
193 .field("message", &self.message())
194 .finish()
195 }
196}
197
198impl core::fmt::Display for Error {
199 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
200 let message = self.message();
201 if message.is_empty() {
202 core::write!(fmt, "{}", self.code())
203 } else {
204 core::write!(fmt, "{} ({})", message, self.code())
205 }
206 }
207}
208
209impl core::hash::Hash for Error {
210 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
211 self.code.hash(state);
212 }
214}
215
216impl PartialEq for Error {
218 fn eq(&self, other: &Self) -> bool {
219 self.code == other.code
220 }
221}
222
223impl Eq for Error {}
224
225impl PartialOrd for Error {
226 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
227 Some(self.cmp(other))
228 }
229}
230
231impl Ord for Error {
232 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
233 self.code.cmp(&other.code)
234 }
235}
236
237use error_info::*;
238
239#[cfg(all(windows, not(windows_slim_errors)))]
240mod error_info {
241 use super::*;
242 use crate::com::ComPtr;
243 use alloc::vec::Vec;
244
245 #[derive(Clone, Default)]
253 pub(crate) struct ErrorInfo {
254 pub(super) ptr: Option<ComPtr>,
255 }
256
257 impl ErrorInfo {
258 pub(crate) const fn empty() -> Self {
259 Self { ptr: None }
260 }
261
262 pub(crate) fn from_thread() -> Self {
263 let mut ptr: *mut core::ffi::c_void = core::ptr::null_mut();
264 unsafe { GetErrorInfo(0, &mut ptr) };
265 Self {
266 ptr: core::ptr::NonNull::new(ptr).map(ComPtr),
267 }
268 }
269
270 pub(crate) fn into_thread(self) {
271 if let Some(ptr) = self.ptr {
272 unsafe {
273 SetErrorInfo(0, ptr.as_raw());
274 }
275 }
276 }
277
278 pub(crate) fn originate_error(code: HRESULT, message: &str) {
279 let message: Vec<_> = message.encode_utf16().collect();
280 unsafe {
281 RoOriginateErrorW(code.0, message.len() as u32, message.as_ptr());
282 }
283 }
284
285 pub(crate) fn message(&self) -> Option<String> {
286 use crate::bstr::BasicString;
287
288 let ptr = self.ptr.as_ref()?;
289
290 let mut message = BasicString::default();
291
292 if let Some(info) = ptr.cast(&IID_IRestrictedErrorInfo) {
294 let mut fallback = BasicString::default();
295 let mut code = 0;
296
297 unsafe {
298 com_call!(
299 IRestrictedErrorInfo_Vtbl,
300 info.GetErrorDetails(
301 &mut fallback as *mut _ as _,
302 &mut code,
303 &mut message as *mut _ as _,
304 &mut BasicString::default() as *mut _ as _
305 )
306 );
307 }
308
309 if message.is_empty() {
310 message = fallback;
311 };
312 }
313
314 if message.is_empty() {
316 unsafe {
317 com_call!(
318 IErrorInfo_Vtbl,
319 ptr.GetDescription(&mut message as *mut _ as _)
320 );
321 }
322 }
323
324 Some(String::from_utf16_lossy(wide_trim_end(&message)))
325 }
326
327 pub(crate) fn as_ptr(&self) -> *mut core::ffi::c_void {
328 if let Some(info) = self.ptr.as_ref() {
329 info.as_raw()
330 } else {
331 core::ptr::null_mut()
332 }
333 }
334 }
335
336 unsafe impl Send for ErrorInfo {}
337 unsafe impl Sync for ErrorInfo {}
338}
339
340#[cfg(not(all(windows, not(windows_slim_errors))))]
341mod error_info {
342 use super::*;
343
344 #[derive(Clone, Default)]
347 pub(crate) struct EmptyErrorInfo;
348
349 pub(crate) use self::EmptyErrorInfo as ErrorInfo;
350
351 impl EmptyErrorInfo {
352 pub(crate) const fn empty() -> Self {
353 Self
354 }
355
356 pub(crate) fn from_thread() -> Self {
357 Self
358 }
359
360 pub(crate) fn into_thread(self) {}
361
362 pub(crate) fn originate_error(_code: HRESULT, _message: &str) {}
363
364 pub(crate) fn message(&self) -> Option<String> {
365 None
366 }
367
368 #[cfg(windows)]
369 pub(crate) fn as_ptr(&self) -> *mut core::ffi::c_void {
370 core::ptr::null_mut()
371 }
372 }
373}