commit 628a7f85deb500624fb75f83014d3bc1a1c03cf4 (tree)
parent d276a1189de15f85523a0e6844d0fd316306838a
Author: fifty-six <ybham6@gmail.com>
Date: Sun, 16 Jan 2022 02:35:22 -0500
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.
Diffstat:
1 file changed, 69 insertions(+), 0 deletions(-)
diff --git 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();
+}