interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / uefi / src / result / error.rs

73 lines · 1977 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Module for UEFI-specific error encodings. See [`Error`].
4
5use super::Status;
6use core::error;
7use core::fmt::{self, Debug, Display, Formatter};
8
9/// An UEFI-related error with optionally additional payload data. The error
10/// kind is encoded in the `status` field (see [`Status`]). Additional payload
11/// may be inside the `data` field.
12#[derive(Clone, Debug, PartialEq, Eq)]
13pub struct Error<Data: Debug = ()> {
14 status: Status,
15 data: Data,
16}
17
18impl<Data: Debug> Error<Data> {
19 /// Create an `Error`.
20 ///
21 /// # Panics
22 ///
23 /// Panics if `status` is [`Status::SUCCESS`].
24 pub const fn new(status: Status, data: Data) -> Self {
25 assert!(!matches!(status, Status::SUCCESS));
26 Self { status, data }
27 }
28
29 /// Get error `Status`.
30 pub const fn status(&self) -> Status {
31 self.status
32 }
33
34 /// Get error data.
35 pub const fn data(&self) -> &Data {
36 &self.data
37 }
38
39 /// Split this error into its inner status and error data
40 pub fn split(self) -> (Status, Data) {
41 (self.status, self.data)
42 }
43}
44
45// Errors without error data can be autogenerated from statuses
46
47impl From<Status> for Error<()> {
48 fn from(status: Status) -> Self {
49 Self::new(status, ())
50 }
51}
52
53impl<Data: Debug> Display for Error<Data> {
54 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
55 write!(f, "UEFI Error {}: {:?}", self.status(), self.data())
56 }
57}
58
59impl<Data: Debug> Error<Data> {
60 /// Transforms the generic payload of an error to `()`. This is useful if
61 /// you want
62 /// - to retain the erroneous status code,
63 /// - do not care about the payload, and
64 /// - refrain from generic type complexity in a higher API level.
65 pub const fn to_err_without_payload(&self) -> Error<()> {
66 Error {
67 status: self.status,
68 data: (),
69 }
70 }
71}
72
73impl<Data: Debug> error::Error for Error<Data> {}