Browse Source

implement suggestions + cleanup

pull/140/head
Lena 1 year ago committed by Lena
parent
commit
208abd1fbd
No known key found for this signature in database
GPG Key ID: 5A940B96C2DA3683
  1. 10
      ctru-rs/src/error.rs
  2. 12
      ctru-rs/src/services/ac.rs

10
ctru-rs/src/error.rs

@ -96,6 +96,8 @@ pub enum Error {
}, },
/// An error that doesn't fit into the other categories. /// An error that doesn't fit into the other categories.
Other(String), Other(String),
/// UTF-8 buffer to [String] conversion error
Utf8(std::string::FromUtf8Error),
} }
impl Error { impl Error {
@ -137,6 +139,12 @@ impl From<ResultCode> for Error {
} }
} }
impl From<std::string::FromUtf8Error> for Error {
fn from(err: std::string::FromUtf8Error) -> Self {
Error::Utf8(err)
}
}
impl fmt::Debug for Error { impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
@ -157,6 +165,7 @@ impl fmt::Debug for Error {
.field("wanted", wanted) .field("wanted", wanted)
.finish(), .finish(),
Self::Other(err) => f.debug_tuple("Other").field(err).finish(), Self::Other(err) => f.debug_tuple("Other").field(err).finish(),
Self::Utf8(e) => f.debug_tuple("Utf8").field(e).finish(),
} }
} }
} }
@ -181,6 +190,7 @@ impl fmt::Display for Error {
} }
Self::BufferTooShort{provided, wanted} => write!(f, "the provided buffer's length is too short (length = {provided}) to hold the wanted data (size = {wanted})"), Self::BufferTooShort{provided, wanted} => write!(f, "the provided buffer's length is too short (length = {provided}) to hold the wanted data (size = {wanted})"),
Self::Other(err) => write!(f, "{err}"), Self::Other(err) => write!(f, "{err}"),
Self::Utf8(e) => write!(f, "UTF-8 conversion error: {e}")
} }
} }
} }

12
ctru-rs/src/services/ac.rs

@ -1,7 +1,4 @@
use crate::error::ResultCode; use crate::error::ResultCode;
use std::ffi::CString;
use std::io::Write;
use std::marker::PhantomData;
pub struct Ac(()); pub struct Ac(());
impl Ac { impl Ac {
@ -43,7 +40,7 @@ impl Ac {
/// let ac = Ac::new()?; /// let ac = Ac::new()?;
/// ///
/// println!("Waiting for an internet connection..."); /// println!("Waiting for an internet connection...");
/// ac.wait_for_internet_connection()?; /// ac.wait_internet_connection()?;
/// println!("Connected."); /// println!("Connected.");
/// # /// #
/// # Ok(()) /// # Ok(())
@ -143,7 +140,7 @@ impl Ac {
let mut vec = vec![0u8; len as usize]; let mut vec = vec![0u8; len as usize];
ResultCode(ctru_sys::ACU_GetSSID(vec.as_mut_ptr()))?; ResultCode(ctru_sys::ACU_GetSSID(vec.as_mut_ptr()))?;
// how do i handle this error? // how do i handle this error?
Ok(String::from_utf8(vec).unwrap()) Ok(String::from_utf8(vec)?)
} }
} }
@ -234,7 +231,7 @@ impl Ac {
ResultCode(ctru_sys::ACU_GetProxyUserName(vec.as_mut_ptr()))?; ResultCode(ctru_sys::ACU_GetProxyUserName(vec.as_mut_ptr()))?;
// how do i handle this error? // how do i handle this error?
Ok(String::from_utf8(vec).unwrap()) Ok(String::from_utf8(vec)?)
} }
} }
@ -265,7 +262,7 @@ impl Ac {
ResultCode(ctru_sys::ACU_GetProxyPassword(vec.as_mut_ptr()))?; ResultCode(ctru_sys::ACU_GetProxyPassword(vec.as_mut_ptr()))?;
// how do i handle this error? // how do i handle this error?
Ok(String::from_utf8(vec).unwrap()) Ok(String::from_utf8(vec)?)
} }
} }
@ -308,6 +305,7 @@ impl Drop for Ac {
#[doc(alias = "acSecurityMode")] #[doc(alias = "acSecurityMode")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)] #[repr(u32)]
#[non_exhaustive]
pub enum SecurityMode { pub enum SecurityMode {
Open = 0, Open = 0,
WEP40Bit = 1, WEP40Bit = 1,

Loading…
Cancel
Save