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 / mod.rs

101 lines · 3299 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Facilities for dealing with UEFI operation results.
4
5use core::fmt::Debug;
6use core::result;
7
8/// The error type that we use, essentially a status code + optional additional data
9mod error;
10pub use error::Error;
11
12/// Definition of UEFI's standard status codes
13mod status;
14pub use status::{Status, StatusExt};
15
16/// Return type of most UEFI functions. Both success and error payloads are optional.
17///
18/// Almost all UEFI operations provide a status code as an output which
19/// indicates either success, a warning, or an error. This type alias maps
20/// [`Status::SUCCESS`] to the `Ok` variant (with optional `Output` data), and
21/// maps both warning and error statuses to the `Err` variant of type [`Error`],
22/// which may carry optional inner `ErrData`.
23///
24/// Warnings are treated as errors by default because they generally indicate
25/// an abnormal situation.
26///
27/// Some convenience methods are provided by the [`ResultExt`] trait.
28pub type Result<Output = (), ErrData = ()> = result::Result<Output, Error<ErrData>>;
29
30/// Extension trait which provides some convenience methods for [`Result`].
31pub trait ResultExt<Output, ErrData: Debug> {
32 /// Extract the UEFI status from this result
33 fn status(&self) -> Status;
34
35 /// Transform the ErrData value to ()
36 fn discard_errdata(self) -> Result<Output>;
37
38 /// Calls `op` if the result contains a warning, otherwise returns
39 /// the result unchanged.
40 ///
41 /// By default warning statuses are treated as errors (i.e. stored in the
42 /// `Err` variant) because they generally indicate an abnormal
43 /// situation. In rare cases though it may be helpful to handle a
44 /// warning. This method is similar to [`Result::or_else`], except that
45 /// `op` is called only when the status is a warning.
46 ///
47 /// # Example
48 ///
49 /// ```
50 /// use uefi::{Result, ResultExt, Status};
51 ///
52 /// # use uefi::StatusExt;
53 /// # fn x() -> uefi::Result {
54 /// # let some_result = Status::WARN_RESET_REQUIRED.to_result();
55 /// // Treat a specific warning as success, propagate others as errors.
56 /// some_result.handle_warning(|err| {
57 /// if err.status() == Status::WARN_RESET_REQUIRED {
58 /// Ok(())
59 /// } else {
60 /// Err(err)
61 /// }
62 /// })?;
63 /// # Status::SUCCESS.to_result()
64 /// # }
65 /// ```
66 fn handle_warning<O>(self, op: O) -> Result<Output, ErrData>
67 where
68 O: FnOnce(Error<ErrData>) -> Result<Output, ErrData>;
69}
70
71impl<Output, ErrData: Debug> ResultExt<Output, ErrData> for Result<Output, ErrData> {
72 fn status(&self) -> Status {
73 match self {
74 Ok(_) => Status::SUCCESS,
75 Err(e) => e.status(),
76 }
77 }
78
79 fn discard_errdata(self) -> Result<Output> {
80 match self {
81 Ok(o) => Ok(o),
82 Err(e) => Err(e.status().into()),
83 }
84 }
85
86 fn handle_warning<O>(self, op: O) -> Self
87 where
88 O: FnOnce(Error<ErrData>) -> Self,
89 {
90 match self {
91 Ok(output) => Ok(output),
92 Err(err) => {
93 if err.status().is_warning() {
94 op(err)
95 } else {
96 Err(err)
97 }
98 }
99 }
100 }
101}