summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-07-06 22:42:45 +0200
committerHampusM <hampus@hampusmat.com>2026-07-06 22:42:45 +0200
commitdadf2f2e6036dd8045ecb07f61e0d089469779f9 (patch)
treee4147394eb5595aa0c965824f76972c317a8bec5
parentddba6a25923bfc8f3204d88c4b1ae3dfd26c05e6 (diff)
feat(engine-ecs): add Error::from_system_defined_win32_error fn
-rw-r--r--engine-ecs/Cargo.toml6
-rw-r--r--engine-ecs/src/error.rs38
2 files changed, 43 insertions, 1 deletions
diff --git a/engine-ecs/Cargo.toml b/engine-ecs/Cargo.toml
index 001cf54..e9ce85c 100644
--- a/engine-ecs/Cargo.toml
+++ b/engine-ecs/Cargo.toml
@@ -21,7 +21,11 @@ vizoxide = { version = "1.0.5", optional = true }
[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.61.2"
-features = ["Win32_System_Threading"]
+features = [
+ "Win32_System_Threading",
+ "Win32_System_Diagnostics_Debug",
+ "Win32_Foundation"
+]
[target.'cfg(target_os = "linux")'.dependencies.libc]
version = "0.2.186"
diff --git a/engine-ecs/src/error.rs b/engine-ecs/src/error.rs
index 2654dde..5ce5632 100644
--- a/engine-ecs/src/error.rs
+++ b/engine-ecs/src/error.rs
@@ -44,6 +44,44 @@ impl Error
{
Self { inner: self.inner.context(context) }
}
+
+ /// Retrieves the message text of a system-defined Windows Win32 error code and
+ /// returns it as a `Error`. If this fails (for example if the error code is invalid),
+ /// the returned `Error` contains the error code number.
+ #[cfg(windows)]
+ pub fn from_system_defined_win32_error(error: u32) -> Self
+ {
+ use std::ffi::CStr;
+ use std::ptr::null;
+
+ use windows_sys::Win32::System::Diagnostics::Debug::{
+ FormatMessageA,
+ FORMAT_MESSAGE_FROM_SYSTEM,
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ };
+
+ let mut buffer = vec![0u8; 256];
+
+ let len = unsafe {
+ FormatMessageA(
+ FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+ null(),
+ error,
+ 0,
+ buffer.as_mut_ptr(),
+ (buffer.len() - 1) as u32,
+ null(),
+ )
+ } as usize;
+
+ if len == 0 {
+ return Self::message(format!("Win32 error {error}"));
+ }
+
+ let buf = unsafe { CStr::from_bytes_with_nul_unchecked(&buffer[..len + 1]) };
+
+ Self::message(buf.to_string_lossy().trim().to_owned())
+ }
}
impl Debug for Error