aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFenrir <[email protected]>2017-01-27 15:53:25 -0700
committerFenrir <[email protected]>2017-01-27 15:53:49 -0700
commite6f3d7821587f6c3d31f43adfd352dd8e19afa95 (patch)
tree54a0794c35879b4054d18360f0ecc868a8cfda20
parentctr-libc: remove conditional dependency on std (diff)
downloadctru-rs-e6f3d7821587f6c3d31f43adfd352dd8e19afa95.tar.xz
ctru-rs-e6f3d7821587f6c3d31f43adfd352dd8e19afa95.zip
ctru-rs: Add Error module
-rw-r--r--ctru-rs/src/error.rs38
-rw-r--r--ctru-rs/src/lib.rs3
-rw-r--r--ctru-rs/src/sdmc.rs4
-rw-r--r--ctru-rs/src/services/apt.rs4
-rw-r--r--ctru-rs/src/services/hid.rs4
-rw-r--r--ctru-rs/src/srv.rs4
6 files changed, 49 insertions, 8 deletions
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<T> = ::std::result::Result<T, Error>;
+
+/// The error type returned by all libctru functions.
+pub enum Error {
+ Os(i32),
+}
+
+impl From<i32> 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<Sdmc, i32> {
+ pub fn init() -> ::Result<Sdmc> {
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<Apt, i32> {
+ pub fn init() -> ::Result<Apt> {
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<Hid, i32> {
+ pub fn init() -> ::Result<Hid> {
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<Srv, i32> {
+ pub fn init() -> ::Result<Srv> {
unsafe {
let r = srvInit();
if r < 0 {
- Err(r)
+ Err(::Error::from(r))
} else {
Ok(Srv { pd: PhantomData })
}