From fa961262cde7ec522f29d4f8fdfea0aa2c987aef Mon Sep 17 00:00:00 2001 From: Fenrir Date: Fri, 23 Feb 2024 16:09:41 -0700 Subject: [PATCH] Handle error codes returned by the Error applet --- ctru-rs/src/applets/error.rs | 44 ++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/ctru-rs/src/applets/error.rs b/ctru-rs/src/applets/error.rs index b5af8717..24868a23 100644 --- a/ctru-rs/src/applets/error.rs +++ b/ctru-rs/src/applets/error.rs @@ -22,7 +22,24 @@ pub enum Kind { Top = ctru_sys::ERROR_TEXT_WORD_WRAP, } -impl ErrorApplet { +/// Error returned by an unsuccessful [`PopUp::launch()`]. +#[doc(alias = "errorReturnCode")] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[repr(i8)] +pub enum Error { + /// Unknown error occurred. + Unknown = ctru_sys::ERROR_UNKNOWN, + /// Operation not supported. + NotSupported = ctru_sys::ERROR_NOT_SUPPORTED, + /// Home button pressed. + HomeButton = ctru_sys::ERROR_HOME_BUTTON, + /// Power button pressed. + PowerButton = ctru_sys::ERROR_POWER_BUTTON, + /// Software reset occurred. + SoftwareReset = ctru_sys::ERROR_SOFTWARE_RESET, +} + +impl PopUp { /// Initialize the error applet with the provided text kind. #[doc(alias = "errorInit")] pub fn new(kind: Kind) -> Self { @@ -48,7 +65,30 @@ impl ErrorApplet { /// Launches the error applet. #[doc(alias = "errorDisp")] - pub fn launch(&mut self, _apt: &Apt, _gfx: &Gfx) { + pub fn launch(&mut self, _apt: &Apt, _gfx: &Gfx) -> Result<(), Error> { unsafe { ctru_sys::errorDisp(self.state.as_mut()) } + + match self.state.returnCode { + ctru_sys::ERROR_NONE | ctru_sys::ERROR_SUCCESS => Ok(()), + ctru_sys::ERROR_NOT_SUPPORTED => Err(Error::NotSupported), + ctru_sys::ERROR_HOME_BUTTON => Err(Error::HomeButton), + ctru_sys::ERROR_POWER_BUTTON => Err(Error::PowerButton), + ctru_sys::ERROR_SOFTWARE_RESET => Err(Error::SoftwareReset), + _ => Err(Error::Unknown), + } } } + +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::NotSupported => write!(f, "operation not supported"), + Self::HomeButton => write(f, "home button pressed while error applet was running"), + Self::PowerButton => write(f, "power button pressed while error applet was running"), + Self::SoftwareReset => write(f, "reset button pressed while error applet was running"), + Self::Unknown => write(f, "an unknown error occurred"), + } + } +} + +impl std::error::Error for Error {}