Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Even less unwraps #417

Merged
merged 6 commits into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ https://github.com/JakeRoggenbuck/auto-clock-speed/issues/new/choose";
pub enum Error {
IO(std::io::Error),
TimeError(std::time::SystemTimeError),
Parse,
HdwNotFound,
Unknown,
DivisionByZero,
Expand All @@ -23,6 +24,12 @@ impl From<std::time::SystemTimeError> for Error {
}
}

impl From<std::num::ParseIntError> for Error {
fn from(_: std::num::ParseIntError) -> Self {
Error::Parse
}
}

impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "Something went wrong. Try running with sudo.")
Expand Down
15 changes: 5 additions & 10 deletions src/power/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ impl Battery {
sysfs::read(
&mut self.capacity,
&self.sys_parent_path.clone().join("capacity"),
)
.unwrap();
)?;

Ok(())
}
Expand Down Expand Up @@ -134,14 +133,12 @@ impl Battery {
sysfs::read(
&mut self.energy_full_design,
&self.sys_parent_path.clone().join("energy_full_design"),
)
.unwrap();
)?;

sysfs::read(
&mut self.energy_full,
&self.sys_parent_path.clone().join("energy_full"),
)
.unwrap();
)?;
Ok(())
}

Expand All @@ -150,14 +147,12 @@ impl Battery {
sysfs::read(
&mut self.charge_full_design,
&self.sys_parent_path.clone().join("charge_full_design"),
)
.unwrap();
)?;

sysfs::read(
&mut self.charge_full,
&self.sys_parent_path.clone().join("charge_full"),
)
.unwrap();
)?;
Ok(())
}

Expand Down
10 changes: 6 additions & 4 deletions src/sysfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ where
file_content.pop();

// Convert String to expected value
*val = file_content.parse::<T>().unwrap();
*val = file_content.parse::<T>().map_err(|_| Error::Unknown)?;

Ok(())
}
Expand All @@ -24,11 +24,13 @@ pub fn get_path_by_glob(sysfs_parent_path: &str, hdw_glob: &str) -> Result<PathB
let mut glob_path = sysfs_parent_path.to_string();
glob_path.push_str(hdw_glob);

let glob = Glob::new(&glob_path).unwrap().compile_matcher();
let entries = fs::read_dir(sysfs_parent_path).unwrap();
let glob = Glob::new(&glob_path)
.map_err(|_| Error::Unknown)?
.compile_matcher();
let entries = fs::read_dir(sysfs_parent_path).map_err(|_| Error::Unknown)?;

for entry in entries {
let entry = entry.unwrap();
let entry = entry.map_err(|_| Error::Unknown)?;
let pathbuf = entry.path();
if glob.is_match(&pathbuf) {
return Ok(pathbuf);
Expand Down
12 changes: 4 additions & 8 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ pub fn get_highest_temp(cpus: &[CPU]) -> i32 {
temp_max
}

fn open_cpu_info() -> String {
fs::read_to_string("/proc/cpuinfo").unwrap_or_else(|_| {
panic!("Could not read /proc/cpuinfo");
})
fn open_cpu_info() -> Result<String, Error> {
Ok(fs::read_to_string("/proc/cpuinfo")?)
}

fn get_name_from_cpu_info(cpu_info: String) -> Result<String, Error> {
Expand All @@ -61,7 +59,7 @@ fn get_name_from_cpu_info(cpu_info: String) -> Result<String, Error> {
}

pub fn check_cpu_name() -> Result<String, Error> {
let cpu_info: String = open_cpu_info();
let cpu_info: String = open_cpu_info()?;
let name: String = get_name_from_cpu_info(cpu_info)?;
Ok(name)
}
Expand Down Expand Up @@ -280,9 +278,7 @@ pub fn read_int(path: &str) -> Result<i32, Error> {

// Remove trailing newline
value.pop();
Ok(value
.parse::<i32>()
.unwrap_or_else(|e| panic!("Could not parse {}\n{}", path, e)))
Ok(value.parse::<i32>()?)
}

pub fn read_str(path: &str) -> Result<String, Error> {
Expand Down