Clean up unwrap()'s. Print out info in asusctl if error

This commit is contained in:
Luke D. Jones
2021-08-28 11:07:47 +12:00
parent 3d41a7978a
commit c6cc304a42
17 changed files with 143 additions and 70 deletions

View File

@@ -27,3 +27,9 @@ impl fmt::Display for ProfileError {
}
impl std::error::Error for ProfileError {}
impl From<std::io::Error> for ProfileError {
fn from(err: std::io::Error) -> Self {
ProfileError::Io(err)
}
}

View File

@@ -41,7 +41,7 @@ impl Profile {
.unwrap_or_else(|_| panic!("{} not found", &PLATFORM_PROFILE));
let mut buf = String::new();
file.read_to_string(&mut buf).unwrap();
file.read_to_string(&mut buf)?;
Ok(buf.as_str().into())
}
@@ -52,17 +52,18 @@ impl Profile {
.unwrap_or_else(|_| panic!("{} not found", &PLATFORM_PROFILES));
let mut buf = String::new();
file.read_to_string(&mut buf).unwrap();
file.read_to_string(&mut buf)?;
Ok(buf.rsplit(' ').map(|p| p.into()).collect())
}
pub fn set_profile(profile: Profile) {
pub fn set_profile(profile: Profile) -> Result<(), ProfileError> {
let mut file = OpenOptions::new()
.write(true)
.open(PLATFORM_PROFILE)
.unwrap_or_else(|_| panic!("{} not found", PLATFORM_PROFILE));
file.write_all(<&str>::from(profile).as_bytes()).unwrap();
file.write_all(<&str>::from(profile).as_bytes())?;
Ok(())
}
}