From 208abd1fbd477d040640037c8ac010bb4f6383f6 Mon Sep 17 00:00:00 2001 From: Lena Date: Mon, 30 Oct 2023 18:54:05 +0100 Subject: [PATCH] implement suggestions + cleanup --- ctru-rs/src/error.rs | 10 ++++++++++ ctru-rs/src/services/ac.rs | 12 +++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/ctru-rs/src/error.rs b/ctru-rs/src/error.rs index 0be67b7..b409318 100644 --- a/ctru-rs/src/error.rs +++ b/ctru-rs/src/error.rs @@ -96,6 +96,8 @@ pub enum Error { }, /// An error that doesn't fit into the other categories. Other(String), + /// UTF-8 buffer to [String] conversion error + Utf8(std::string::FromUtf8Error), } impl Error { @@ -137,6 +139,12 @@ impl From for Error { } } +impl From for Error { + fn from(err: std::string::FromUtf8Error) -> Self { + Error::Utf8(err) + } +} + impl fmt::Debug for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { @@ -157,6 +165,7 @@ impl fmt::Debug for Error { .field("wanted", wanted) .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::Other(err) => write!(f, "{err}"), + Self::Utf8(e) => write!(f, "UTF-8 conversion error: {e}") } } } diff --git a/ctru-rs/src/services/ac.rs b/ctru-rs/src/services/ac.rs index d85e819..be6f1c4 100644 --- a/ctru-rs/src/services/ac.rs +++ b/ctru-rs/src/services/ac.rs @@ -1,7 +1,4 @@ use crate::error::ResultCode; -use std::ffi::CString; -use std::io::Write; -use std::marker::PhantomData; pub struct Ac(()); impl Ac { @@ -43,7 +40,7 @@ impl Ac { /// let ac = Ac::new()?; /// /// println!("Waiting for an internet connection..."); - /// ac.wait_for_internet_connection()?; + /// ac.wait_internet_connection()?; /// println!("Connected."); /// # /// # Ok(()) @@ -143,7 +140,7 @@ impl Ac { let mut vec = vec![0u8; len as usize]; ResultCode(ctru_sys::ACU_GetSSID(vec.as_mut_ptr()))?; // 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()))?; // 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()))?; // 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")] #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(u32)] +#[non_exhaustive] pub enum SecurityMode { Open = 0, WEP40Bit = 1,