authorgravatar for ybham6@gmail.comfifty-six <ybham6@gmail.com> 2022-01-16 02:35:22-05:00
committergravatar for ybham6@gmail.comfifty-six <ybham6@gmail.com> 2022-01-16 02:35:22-05:00
log628a7f85deb500624fb75f83014d3bc1a1c03cf4
tree7cc35d6dfbcd20e8d25e5476126924edb9445079
parentd276a1189de15f85523a0e6844d0fd316306838a

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 files changed, 69 insertions(+), 0 deletions(-)

lib/std/os/uefi/status.zig+69
...@@ -1,3 +1,5 @@...@@ -1,3 +1,5 @@
1const testing = @import("std").testing;
2
1const high_bit = 1 << @typeInfo(usize).Int.bits - 1;3const high_bit = 1 << @typeInfo(usize).Int.bits - 1;
24
3pub const Status = enum(usize) {5pub const Status = enum(usize) {
...@@ -139,4 +141,71 @@ pub const Status = enum(usize) {...@@ -139,4 +141,71 @@ pub const Status = enum(usize) {
139 WarnResetRequired = 7,141 WarnResetRequired = 7,
140142
141 _,143 _,
144
145 pub const EfiError = error{
146 LoadError,
147 InvalidParameter,
148 Unsupported,
149 BadBufferSize,
150 BufferTooSmall,
151 NotReady,
152 DeviceError,
153 WriteProtected,
154 OutOfResources,
155 VolumeCorrupted,
156 VolumeFull,
157 NoMedia,
158 MediaChanged,
159 NotFound,
160 AccessDenied,
161 NoResponse,
162 NoMapping,
163 Timeout,
164 NotStarted,
165 AlreadyStarted,
166 Aborted,
167 IcmpError,
168 TftpError,
169 ProtocolError,
170 IncompatibleVersion,
171 SecurityViolation,
172 CrcError,
173 EndOfMedia,
174 EndOfFile,
175 InvalidLanguage,
176 CompromisedData,
177 IpAddressConflict,
178 HttpError,
179 NetworkUnreachable,
180 HostUnreachable,
181 ProtocolUnreachable,
182 PortUnreachable,
183 ConnectionFin,
184 ConnectionReset,
185 ConnectionRefused,
186 WarnUnknownGlyph,
187 WarnDeleteFailure,
188 WarnWriteFailure,
189 WarnBufferTooSmall,
190 WarnStaleData,
191 WarnFileSystem,
192 WarnResetRequired,
193 };
194
195 pub fn err(self: Status) EfiError!void {
196 inline for (@typeInfo(EfiError).ErrorSet.?) |efi_err| {
197 if (self == @field(Status, efi_err.name)) {
198 return @field(EfiError, efi_err.name);
199 }
200 }
201 // self is .Success
202 }
142};203};
204
205test "status" {
206 var st: Status = .DeviceError;
207 try testing.expectError(error.DeviceError, st.err());
208
209 st = .Success;
210 try st.err();
211}