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

140 lines · 4150 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use super::{Error, Result};
4use core::fmt::Debug;
5
6pub use uefi_raw::Status;
7
8/// Extension trait which provides some convenience methods for [`Status`].
9pub trait StatusExt {
10 /// Converts this status code into a [`uefi::Result`].
11 ///
12 /// If the status does not indicate success, the status representing the specific error
13 /// code is embedded into the `Err` variant of type [`uefi::Error`].
14 fn to_result(self) -> Result;
15
16 /// Converts this status code into a [`uefi::Result`] with a given `Ok` value.
17 ///
18 /// If the status does not indicate success, the status representing the specific error
19 /// code is embedded into the `Err` variant of type [`uefi::Error`].
20 fn to_result_with_val<T>(self, val: impl FnOnce() -> T) -> Result<T, ()>;
21
22 /// Converts this status code into a [`uefi::Result`] with a given `Err` payload.
23 ///
24 /// If the status does not indicate success, the status representing the specific error
25 /// code is embedded into the `Err` variant of type [`uefi::Error`].
26 fn to_result_with_err<ErrData: Debug>(
27 self,
28 err: impl FnOnce(Status) -> ErrData,
29 ) -> Result<(), ErrData>;
30
31 /// Convert this status code into a result with a given `Ok` value and `Err` payload.
32 ///
33 /// If the status does not indicate success, the status representing the specific error
34 /// code is embedded into the `Err` variant of type [`uefi::Error`].
35 fn to_result_with<T, ErrData: Debug>(
36 self,
37 val: impl FnOnce() -> T,
38 err: impl FnOnce(Status) -> ErrData,
39 ) -> Result<T, ErrData>;
40}
41
42impl StatusExt for Status {
43 #[inline]
44 fn to_result(self) -> Result {
45 if self.is_success() {
46 Ok(())
47 } else {
48 Err(self.into())
49 }
50 }
51
52 #[inline]
53 fn to_result_with_val<T>(self, val: impl FnOnce() -> T) -> Result<T, ()> {
54 if self.is_success() {
55 Ok(val())
56 } else {
57 Err(self.into())
58 }
59 }
60
61 #[inline]
62 fn to_result_with_err<ErrData: Debug>(
63 self,
64 err: impl FnOnce(Self) -> ErrData,
65 ) -> Result<(), ErrData> {
66 if self.is_success() {
67 Ok(())
68 } else {
69 Err(Error::new(self, err(self)))
70 }
71 }
72
73 #[inline]
74 fn to_result_with<T, ErrData: Debug>(
75 self,
76 val: impl FnOnce() -> T,
77 err: impl FnOnce(Self) -> ErrData,
78 ) -> Result<T, ErrData> {
79 if self.is_success() {
80 Ok(val())
81 } else {
82 Err(Error::new(self, err(self)))
83 }
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[test]
92 fn test_status_to_result() {
93 assert!(Status::SUCCESS.to_result().is_ok());
94 assert!(Status::WARN_DELETE_FAILURE.to_result().is_err());
95 assert!(Status::BUFFER_TOO_SMALL.to_result().is_err());
96
97 assert_eq!(Status::SUCCESS.to_result_with_val(|| 123).unwrap(), 123);
98 assert!(
99 Status::WARN_DELETE_FAILURE
100 .to_result_with_val(|| 123)
101 .is_err()
102 );
103 assert!(Status::BUFFER_TOO_SMALL.to_result_with_val(|| 123).is_err());
104
105 assert!(Status::SUCCESS.to_result_with_err(|_| 123).is_ok());
106 assert_eq!(
107 *Status::WARN_DELETE_FAILURE
108 .to_result_with_err(|_| 123)
109 .unwrap_err()
110 .data(),
111 123
112 );
113 assert_eq!(
114 *Status::BUFFER_TOO_SMALL
115 .to_result_with_err(|_| 123)
116 .unwrap_err()
117 .data(),
118 123
119 );
120
121 assert_eq!(
122 Status::SUCCESS.to_result_with(|| 123, |_| 456).unwrap(),
123 123
124 );
125 assert_eq!(
126 *Status::WARN_DELETE_FAILURE
127 .to_result_with(|| 123, |_| 456)
128 .unwrap_err()
129 .data(),
130 456
131 );
132 assert_eq!(
133 *Status::BUFFER_TOO_SMALL
134 .to_result_with(|| 123, |_| 456)
135 .unwrap_err()
136 .data(),
137 456
138 );
139 }
140}