Commit 628a7f85de

fifty-six <ybham6@gmail.com>
2022-01-16 08:35:22
std/os/uefi: Add conversion from Status to EfiError
Allows handling uefi function errors in a more zig-style way with try and catch, using `try f().err()` when a `Status` is returned.
1 parent d276a11
Changed files (1)
lib
std
os
lib/std/os/uefi/status.zig
@@ -1,3 +1,5 @@
+const testing = @import("std").testing;
+
 const high_bit = 1 << @typeInfo(usize).Int.bits - 1;
 
 pub const Status = enum(usize) {
@@ -139,4 +141,71 @@ pub const Status = enum(usize) {
     WarnResetRequired = 7,
 
     _,
+
+    pub const EfiError = error{
+        LoadError,
+        InvalidParameter,
+        Unsupported,
+        BadBufferSize,
+        BufferTooSmall,
+        NotReady,
+        DeviceError,
+        WriteProtected,
+        OutOfResources,
+        VolumeCorrupted,
+        VolumeFull,
+        NoMedia,
+        MediaChanged,
+        NotFound,
+        AccessDenied,
+        NoResponse,
+        NoMapping,
+        Timeout,
+        NotStarted,
+        AlreadyStarted,
+        Aborted,
+        IcmpError,
+        TftpError,
+        ProtocolError,
+        IncompatibleVersion,
+        SecurityViolation,
+        CrcError,
+        EndOfMedia,
+        EndOfFile,
+        InvalidLanguage,
+        CompromisedData,
+        IpAddressConflict,
+        HttpError,
+        NetworkUnreachable,
+        HostUnreachable,
+        ProtocolUnreachable,
+        PortUnreachable,
+        ConnectionFin,
+        ConnectionReset,
+        ConnectionRefused,
+        WarnUnknownGlyph,
+        WarnDeleteFailure,
+        WarnWriteFailure,
+        WarnBufferTooSmall,
+        WarnStaleData,
+        WarnFileSystem,
+        WarnResetRequired,
+    };
+
+    pub fn err(self: Status) EfiError!void {
+        inline for (@typeInfo(EfiError).ErrorSet.?) |efi_err| {
+            if (self == @field(Status, efi_err.name)) {
+                return @field(EfiError, efi_err.name);
+            }
+        }
+        // self is .Success
+    }
 };
+
+test "status" {
+    var st: Status = .DeviceError;
+    try testing.expectError(error.DeviceError, st.err());
+
+    st = .Success;
+    try st.err();
+}