From 628a7f85deb500624fb75f83014d3bc1a1c03cf4 Mon Sep 17 00:00:00 2001 From: fifty-six Date: Sun, 16 Jan 2022 02:35:22 -0500 Subject: [PATCH] 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. --- lib/std/os/uefi/status.zig | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/lib/std/os/uefi/status.zig b/lib/std/os/uefi/status.zig index 09bc5030eb..e6f2024f7d 100644 --- a/lib/std/os/uefi/status.zig +++ b/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(); +}