1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use super::{Error, Result};
use core::fmt::Debug;

pub use uefi_raw::Status;

/// Extension trait which provides some convenience methods for [`Status`].
pub trait StatusExt {
    /// Converts this status code into a [`uefi::Result`].
    ///
    /// If the status does not indicate success, the status representing the specific error
    /// code is embedded into the `Err` variant of type [`uefi::Error`].
    fn to_result(self) -> Result;

    /// Converts this status code into a [`uefi::Result`] with a given `Ok` value.
    ///
    /// If the status does not indicate success, the status representing the specific error
    /// code is embedded into the `Err` variant of type [`uefi::Error`].
    fn to_result_with_val<T>(self, val: impl FnOnce() -> T) -> Result<T, ()>;

    /// Converts this status code into a [`uefi::Result`] with a given `Err` payload.
    ///
    /// If the status does not indicate success, the status representing the specific error
    /// code is embedded into the `Err` variant of type [`uefi::Error`].
    fn to_result_with_err<ErrData: Debug>(
        self,
        err: impl FnOnce(Status) -> ErrData,
    ) -> Result<(), ErrData>;

    /// Convert this status code into a result with a given `Ok` value and `Err` payload.
    ///
    /// If the status does not indicate success, the status representing the specific error
    /// code is embedded into the `Err` variant of type [`uefi::Error`].
    fn to_result_with<T, ErrData: Debug>(
        self,
        val: impl FnOnce() -> T,
        err: impl FnOnce(Status) -> ErrData,
    ) -> Result<T, ErrData>;
}

impl StatusExt for Status {
    #[inline]
    fn to_result(self) -> Result {
        if self.is_success() {
            Ok(())
        } else {
            Err(self.into())
        }
    }

    #[inline]
    fn to_result_with_val<T>(self, val: impl FnOnce() -> T) -> Result<T, ()> {
        if self.is_success() {
            Ok(val())
        } else {
            Err(self.into())
        }
    }

    #[inline]
    fn to_result_with_err<ErrData: Debug>(
        self,
        err: impl FnOnce(Status) -> ErrData,
    ) -> Result<(), ErrData> {
        if self.is_success() {
            Ok(())
        } else {
            Err(Error::new(self, err(self)))
        }
    }

    #[inline]
    fn to_result_with<T, ErrData: Debug>(
        self,
        val: impl FnOnce() -> T,
        err: impl FnOnce(Status) -> ErrData,
    ) -> Result<T, ErrData> {
        if self.is_success() {
            Ok(val())
        } else {
            Err(Error::new(self, err(self)))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_status_to_result() {
        assert!(Status::SUCCESS.to_result().is_ok());
        assert!(Status::WARN_DELETE_FAILURE.to_result().is_err());
        assert!(Status::BUFFER_TOO_SMALL.to_result().is_err());

        assert_eq!(Status::SUCCESS.to_result_with_val(|| 123).unwrap(), 123);
        assert!(Status::WARN_DELETE_FAILURE
            .to_result_with_val(|| 123)
            .is_err());
        assert!(Status::BUFFER_TOO_SMALL.to_result_with_val(|| 123).is_err());

        assert!(Status::SUCCESS.to_result_with_err(|_| 123).is_ok());
        assert_eq!(
            *Status::WARN_DELETE_FAILURE
                .to_result_with_err(|_| 123)
                .unwrap_err()
                .data(),
            123
        );
        assert_eq!(
            *Status::BUFFER_TOO_SMALL
                .to_result_with_err(|_| 123)
                .unwrap_err()
                .data(),
            123
        );

        assert_eq!(
            Status::SUCCESS.to_result_with(|| 123, |_| 456).unwrap(),
            123
        );
        assert_eq!(
            *Status::WARN_DELETE_FAILURE
                .to_result_with(|| 123, |_| 456)
                .unwrap_err()
                .data(),
            456
        );
        assert_eq!(
            *Status::BUFFER_TOO_SMALL
                .to_result_with(|| 123, |_| 456)
                .unwrap_err()
                .data(),
            456
        );
    }
}