1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
pub struct SystemCategory;
#[cfg(not(windows))]
use crate::posix::to_error;
#[cfg(not(windows))]
pub use crate::posix::get_last_error;
#[cfg(windows)]
extern "system" {
fn GetLastError() -> u32;
fn FormatMessageW(dwFlags: u32, lpSource: *const u8, dwMessageId: u32, dwLanguageId: u32, lpBuffer: *mut u16, nSize: u32, Arguments: *mut i8) -> u32;
}
#[cfg(windows)]
#[inline]
pub fn get_last_error() -> i32 {
unsafe {
GetLastError() as i32
}
}
#[cfg(windows)]
pub fn to_error<'a>(code: i32) -> alloc::borrow::Cow<'a, str> {
const FORMAT_MESSAGE_ARGUMENT_ARRAY: u32 = 0x00002000;
const FORMAT_MESSAGE_FROM_SYSTEM: u32 = 0x00001000;
const FORMAT_MESSAGE_IGNORE_INSERTS: u32 = 0x00000200;
const BUF_SIZE: usize = 512;
const FMT_FLAGS: u32 = FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY;
let mut buff: [u16; BUF_SIZE] = [0; BUF_SIZE];
let num_chars: u32 = unsafe { FormatMessageW(FMT_FLAGS,
core::ptr::null(), code as u32,
0, buff.as_mut_ptr(),
BUF_SIZE as u32, core::ptr::null_mut()) };
if num_chars == 0 {
match get_last_error() {
122 => alloc::string::String::from_utf16_lossy(&buff).into(),
_ => alloc::borrow::Cow::Borrowed(crate::UNKNOWN_ERROR),
}
} else {
alloc::string::String::from_utf16_lossy(&buff[..num_chars as usize-2]).into()
}
}
impl crate::Category for SystemCategory {
const NAME: &'static str = "OS error";
#[inline]
fn message<'a>(code: i32) -> alloc::borrow::Cow<'a, str> {
to_error(code)
}
}