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