| 1 | const testing = @import("std").testing; |
| 2 | |
| 3 | const high_bit = 1 << @typeInfo(usize).int.bits - 1; |
| 4 | |
| 5 | pub const Status = enum(usize) { |
| 6 | /// The operation completed successfully. |
| 7 | success = 0, |
| 8 | |
| 9 | /// The image failed to load. |
| 10 | load_error = high_bit | 1, |
| 11 | |
| 12 | /// A parameter was incorrect. |
| 13 | invalid_parameter = high_bit | 2, |
| 14 | |
| 15 | /// The operation is not supported. |
| 16 | unsupported = high_bit | 3, |
| 17 | |
| 18 | /// The buffer was not the proper size for the request. |
| 19 | bad_buffer_size = high_bit | 4, |
| 20 | |
| 21 | /// The buffer is not large enough to hold the requested data. The required buffer size is returned in the appropriate parameter when this error occurs. |
| 22 | buffer_too_small = high_bit | 5, |
| 23 | |
| 24 | /// There is no data pending upon return. |
| 25 | not_ready = high_bit | 6, |
| 26 | |
| 27 | /// The physical device reported an error while attempting the operation. |
| 28 | device_error = high_bit | 7, |
| 29 | |
| 30 | /// The device cannot be written to. |
| 31 | write_protected = high_bit | 8, |
| 32 | |
| 33 | /// A resource has run out. |
| 34 | out_of_resources = high_bit | 9, |
| 35 | |
| 36 | /// An inconstancy was detected on the file system causing the operating to fail. |
| 37 | volume_corrupted = high_bit | 10, |
| 38 | |
| 39 | /// There is no more space on the file system. |
| 40 | volume_full = high_bit | 11, |
| 41 | |
| 42 | /// The device does not contain any medium to perform the operation. |
| 43 | no_media = high_bit | 12, |
| 44 | |
| 45 | /// The medium in the device has changed since the last access. |
| 46 | media_changed = high_bit | 13, |
| 47 | |
| 48 | /// The item was not found. |
| 49 | not_found = high_bit | 14, |
| 50 | |
| 51 | /// Access was denied. |
| 52 | access_denied = high_bit | 15, |
| 53 | |
| 54 | /// The server was not found or did not respond to the request. |
| 55 | no_response = high_bit | 16, |
| 56 | |
| 57 | /// A mapping to a device does not exist. |
| 58 | no_mapping = high_bit | 17, |
| 59 | |
| 60 | /// The timeout time expired. |
| 61 | timeout = high_bit | 18, |
| 62 | |
| 63 | /// The protocol has not been started. |
| 64 | not_started = high_bit | 19, |
| 65 | |
| 66 | /// The protocol has already been started. |
| 67 | already_started = high_bit | 20, |
| 68 | |
| 69 | /// The operation was aborted. |
| 70 | aborted = high_bit | 21, |
| 71 | |
| 72 | /// An ICMP error occurred during the network operation. |
| 73 | icmp_error = high_bit | 22, |
| 74 | |
| 75 | /// A TFTP error occurred during the network operation. |
| 76 | tftp_error = high_bit | 23, |
| 77 | |
| 78 | /// A protocol error occurred during the network operation. |
| 79 | protocol_error = high_bit | 24, |
| 80 | |
| 81 | /// The function encountered an internal version that was incompatible with a version requested by the caller. |
| 82 | incompatible_version = high_bit | 25, |
| 83 | |
| 84 | /// The function was not performed due to a security violation. |
| 85 | security_violation = high_bit | 26, |
| 86 | |
| 87 | /// A CRC error was detected. |
| 88 | crc_error = high_bit | 27, |
| 89 | |
| 90 | /// Beginning or end of media was reached |
| 91 | end_of_media = high_bit | 28, |
| 92 | |
| 93 | /// The end of the file was reached. |
| 94 | end_of_file = high_bit | 31, |
| 95 | |
| 96 | /// The language specified was invalid. |
| 97 | invalid_language = high_bit | 32, |
| 98 | |
| 99 | /// The security status of the data is unknown or compromised and the data must be updated or replaced to restore a valid security status. |
| 100 | compromised_data = high_bit | 33, |
| 101 | |
| 102 | /// There is an address conflict address allocation |
| 103 | ip_address_conflict = high_bit | 34, |
| 104 | |
| 105 | /// A HTTP error occurred during the network operation. |
| 106 | http_error = high_bit | 35, |
| 107 | |
| 108 | network_unreachable = high_bit | 100, |
| 109 | |
| 110 | host_unreachable = high_bit | 101, |
| 111 | |
| 112 | protocol_unreachable = high_bit | 102, |
| 113 | |
| 114 | port_unreachable = high_bit | 103, |
| 115 | |
| 116 | connection_fin = high_bit | 104, |
| 117 | |
| 118 | connection_reset = high_bit | 105, |
| 119 | |
| 120 | connection_refused = high_bit | 106, |
| 121 | |
| 122 | /// The string contained one or more characters that the device could not render and were skipped. |
| 123 | warn_unknown_glyph = 1, |
| 124 | |
| 125 | /// The handle was closed, but the file was not deleted. |
| 126 | warn_delete_failure = 2, |
| 127 | |
| 128 | /// The handle was closed, but the data to the file was not flushed properly. |
| 129 | warn_write_failure = 3, |
| 130 | |
| 131 | /// The resulting buffer was too small, and the data was truncated to the buffer size. |
| 132 | warn_buffer_too_small = 4, |
| 133 | |
| 134 | /// The data has not been updated within the timeframe set by localpolicy for this type of data. |
| 135 | warn_stale_data = 5, |
| 136 | |
| 137 | /// The resulting buffer contains UEFI-compliant file system. |
| 138 | warn_file_system = 6, |
| 139 | |
| 140 | /// The operation will be processed across a system reset. |
| 141 | warn_reset_required = 7, |
| 142 | |
| 143 | _, |
| 144 | |
| 145 | pub const Error = 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 | }; |
| 187 | |
| 188 | pub fn err(self: Status) Error!void { |
| 189 | switch (self) { |
| 190 | .load_error => return error.LoadError, |
| 191 | .invalid_parameter => return error.InvalidParameter, |
| 192 | .unsupported => return error.Unsupported, |
| 193 | .bad_buffer_size => return error.BadBufferSize, |
| 194 | .buffer_too_small => return error.BufferTooSmall, |
| 195 | .not_ready => return error.NotReady, |
| 196 | .device_error => return error.DeviceError, |
| 197 | .write_protected => return error.WriteProtected, |
| 198 | .out_of_resources => return error.OutOfResources, |
| 199 | .volume_corrupted => return error.VolumeCorrupted, |
| 200 | .volume_full => return error.VolumeFull, |
| 201 | .no_media => return error.NoMedia, |
| 202 | .media_changed => return error.MediaChanged, |
| 203 | .not_found => return error.NotFound, |
| 204 | .access_denied => return error.AccessDenied, |
| 205 | .no_response => return error.NoResponse, |
| 206 | .no_mapping => return error.NoMapping, |
| 207 | .timeout => return error.Timeout, |
| 208 | .not_started => return error.NotStarted, |
| 209 | .already_started => return error.AlreadyStarted, |
| 210 | .aborted => return error.Aborted, |
| 211 | .icmp_error => return error.IcmpError, |
| 212 | .tftp_error => return error.TftpError, |
| 213 | .protocol_error => return error.ProtocolError, |
| 214 | .incompatible_version => return error.IncompatibleVersion, |
| 215 | .security_violation => return error.SecurityViolation, |
| 216 | .crc_error => return error.CrcError, |
| 217 | .end_of_media => return error.EndOfMedia, |
| 218 | .end_of_file => return error.EndOfFile, |
| 219 | .invalid_language => return error.InvalidLanguage, |
| 220 | .compromised_data => return error.CompromisedData, |
| 221 | .ip_address_conflict => return error.IpAddressConflict, |
| 222 | .http_error => return error.HttpError, |
| 223 | .network_unreachable => return error.NetworkUnreachable, |
| 224 | .host_unreachable => return error.HostUnreachable, |
| 225 | .protocol_unreachable => return error.ProtocolUnreachable, |
| 226 | .port_unreachable => return error.PortUnreachable, |
| 227 | .connection_fin => return error.ConnectionFin, |
| 228 | .connection_reset => return error.ConnectionReset, |
| 229 | .connection_refused => return error.ConnectionRefused, |
| 230 | // success, warn_*, _ |
| 231 | else => {}, |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | pub fn fromError(e: Error) Status { |
| 236 | return switch (e) { |
| 237 | Error.Aborted => .aborted, |
| 238 | Error.AccessDenied => .access_denied, |
| 239 | Error.AlreadyStarted => .already_started, |
| 240 | Error.BadBufferSize => .bad_buffer_size, |
| 241 | Error.BufferTooSmall => .buffer_too_small, |
| 242 | Error.CompromisedData => .compromised_data, |
| 243 | Error.ConnectionFin => .connection_fin, |
| 244 | Error.ConnectionRefused => .connection_refused, |
| 245 | Error.ConnectionReset => .connection_reset, |
| 246 | Error.CrcError => .crc_error, |
| 247 | Error.DeviceError => .device_error, |
| 248 | Error.EndOfFile => .end_of_file, |
| 249 | Error.EndOfMedia => .end_of_media, |
| 250 | Error.HostUnreachable => .host_unreachable, |
| 251 | Error.HttpError => .http_error, |
| 252 | Error.IcmpError => .icmp_error, |
| 253 | Error.IncompatibleVersion => .incompatible_version, |
| 254 | Error.InvalidLanguage => .invalid_language, |
| 255 | Error.InvalidParameter => .invalid_parameter, |
| 256 | Error.IpAddressConflict => .ip_address_conflict, |
| 257 | Error.LoadError => .load_error, |
| 258 | Error.MediaChanged => .media_changed, |
| 259 | Error.NetworkUnreachable => .network_unreachable, |
| 260 | Error.NoMapping => .no_mapping, |
| 261 | Error.NoMedia => .no_media, |
| 262 | Error.NoResponse => .no_response, |
| 263 | Error.NotFound => .not_found, |
| 264 | Error.NotReady => .not_ready, |
| 265 | Error.NotStarted => .not_started, |
| 266 | Error.OutOfResources => .out_of_resources, |
| 267 | Error.PortUnreachable => .port_unreachable, |
| 268 | Error.ProtocolError => .protocol_error, |
| 269 | Error.ProtocolUnreachable => .protocol_unreachable, |
| 270 | Error.SecurityViolation => .security_violation, |
| 271 | Error.TftpError => .tftp_error, |
| 272 | Error.Timeout => .timeout, |
| 273 | Error.Unsupported => .unsupported, |
| 274 | Error.VolumeCorrupted => .volume_corrupted, |
| 275 | Error.VolumeFull => .volume_full, |
| 276 | Error.WriteProtected => .write_protected, |
| 277 | }; |
| 278 | } |
| 279 | }; |
| 280 | |
| 281 | test "status" { |
| 282 | var st: Status = .device_error; |
| 283 | try testing.expectError(error.DeviceError, st.err()); |
| 284 | try testing.expectEqual(st, Status.fromError(st.err())); |
| 285 | |
| 286 | st = .success; |
| 287 | try st.err(); |
| 288 | |
| 289 | st = .warn_unknown_glyph; |
| 290 | try st.err(); |
| 291 | } |