diff --git a/ctru-rs/src/error.rs b/ctru-rs/src/error.rs new file mode 100644 index 0000000..b1d968b --- /dev/null +++ b/ctru-rs/src/error.rs @@ -0,0 +1,38 @@ +use std::error; +use std::fmt; + +pub type Result = ::std::result::Result; + +/// The error type returned by all libctru functions. +pub enum Error { + Os(i32), +} + +impl From for Error { + fn from(err: i32) -> Self { + Error::Os(err) + } +} + +impl fmt::Debug for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Error::Os(err) => write!(f, "libctru result code: {:08X}", err), + } + } +} + +// TODO: Expand libctru result code into human-readable error message +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Error::Os(err) => write!(f, "libctru result code: 0x{:08X}", err), + } + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + "error originating from a libctru function" + } +} diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index bfdb614..10132a0 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -8,11 +8,14 @@ extern crate widestring; extern crate ctru_sys as libctru; pub mod console; +pub mod error; pub mod srv; pub mod gfx; pub mod services; pub mod sdmc; +pub use error::{Result, Error}; + pub use srv::Srv; pub use gfx::Gfx; pub use sdmc::Sdmc; diff --git a/ctru-rs/src/sdmc.rs b/ctru-rs/src/sdmc.rs index 8698f80..6196394 100644 --- a/ctru-rs/src/sdmc.rs +++ b/ctru-rs/src/sdmc.rs @@ -7,11 +7,11 @@ pub struct Sdmc { } impl Sdmc { - pub fn init() -> Result { + pub fn init() -> ::Result { unsafe { let r = sdmcInit(); if r < 0 { - Err(r) + Err(::Error::from(r)) } else { Ok(Sdmc { pd: PhantomData }) } diff --git a/ctru-rs/src/services/apt.rs b/ctru-rs/src/services/apt.rs index 6fa95ec..0c8e281 100644 --- a/ctru-rs/src/services/apt.rs +++ b/ctru-rs/src/services/apt.rs @@ -7,11 +7,11 @@ pub struct Apt { } impl Apt { - pub fn init() -> Result { + pub fn init() -> ::Result { unsafe { let r = apt::aptInit(); if r < 0 { - Err(r) + Err(::Error::from(r)) } else { Ok(Apt { pd: PhantomData }) } diff --git a/ctru-rs/src/services/hid.rs b/ctru-rs/src/services/hid.rs index c2a8b3d..c703c21 100644 --- a/ctru-rs/src/services/hid.rs +++ b/ctru-rs/src/services/hid.rs @@ -78,11 +78,11 @@ pub struct Hid { } impl Hid { - pub fn init() -> Result { + pub fn init() -> ::Result { unsafe { let r = hid::hidInit(); if r < 0 { - Err(r) + Err(::Error::from(r)) } else { Ok(Hid { pd: PhantomData }) } diff --git a/ctru-rs/src/srv.rs b/ctru-rs/src/srv.rs index a7376a5..f0e7026 100644 --- a/ctru-rs/src/srv.rs +++ b/ctru-rs/src/srv.rs @@ -7,11 +7,11 @@ pub struct Srv { } impl Srv { - pub fn init() -> Result { + pub fn init() -> ::Result { unsafe { let r = srvInit(); if r < 0 { - Err(r) + Err(::Error::from(r)) } else { Ok(Srv { pd: PhantomData }) }