Skip to content

Commit

Permalink
Handle error codes returned by the Error applet
Browse files Browse the repository at this point in the history
  • Loading branch information
FenrirWolf committed Feb 23, 2024
1 parent cd30981 commit fa96126
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions ctru-rs/src/applets/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {

Check failure on line 82 in ctru-rs/src/applets/error.rs

View workflow job for this annotation

GitHub Actions / lint (nightly-2024-02-18)

cannot find trait `Display` in this scope
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"),

Check failure on line 86 in ctru-rs/src/applets/error.rs

View workflow job for this annotation

GitHub Actions / lint (nightly-2024-02-18)

expected function, found macro `write`
Self::PowerButton => write(f, "power button pressed while error applet was running"),

Check failure on line 87 in ctru-rs/src/applets/error.rs

View workflow job for this annotation

GitHub Actions / lint (nightly-2024-02-18)

expected function, found macro `write`
Self::SoftwareReset => write(f, "reset button pressed while error applet was running"),

Check failure on line 88 in ctru-rs/src/applets/error.rs

View workflow job for this annotation

GitHub Actions / lint (nightly-2024-02-18)

expected function, found macro `write`
Self::Unknown => write(f, "an unknown error occurred"),

Check failure on line 89 in ctru-rs/src/applets/error.rs

View workflow job for this annotation

GitHub Actions / lint (nightly-2024-02-18)

expected function, found macro `write`
}
}
}

impl std::error::Error for Error {}

0 comments on commit fa96126

Please sign in to comment.