Browse Source

ctru-rs: Add Error module

pull/10/head
Fenrir 8 years ago
parent
commit
e6f3d78215
  1. 38
      ctru-rs/src/error.rs
  2. 3
      ctru-rs/src/lib.rs
  3. 4
      ctru-rs/src/sdmc.rs
  4. 4
      ctru-rs/src/services/apt.rs
  5. 4
      ctru-rs/src/services/hid.rs
  6. 4
      ctru-rs/src/srv.rs

38
ctru-rs/src/error.rs

@ -0,0 +1,38 @@ @@ -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"
}
}

3
ctru-rs/src/lib.rs

@ -8,11 +8,14 @@ extern crate widestring; @@ -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;

4
ctru-rs/src/sdmc.rs

@ -7,11 +7,11 @@ pub struct Sdmc { @@ -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 })
}

4
ctru-rs/src/services/apt.rs

@ -7,11 +7,11 @@ pub struct Apt { @@ -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 })
}

4
ctru-rs/src/services/hid.rs

@ -78,11 +78,11 @@ pub struct Hid { @@ -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 })
}

4
ctru-rs/src/srv.rs

@ -7,11 +7,11 @@ pub struct Srv { @@ -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 })
}

Loading…
Cancel
Save