Skip to content

Commit

Permalink
Merge pull request #409 from JakeRoggenbuck/reduce-unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
Camerooooon authored Sep 10, 2022
2 parents c79a935 + 5dbb95f commit 78deed2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ impl Getter for Get {
}

fn thermal(&self, raw: bool) {
let zones = read_thermal_zones();
let zones = match read_thermal_zones() {
Ok(zones) => zones,
Err(error) => {
println!("Error: {:?}", error);
return;
}
};
if raw {
println!("{:?}", zones)
} else {
Expand Down
15 changes: 7 additions & 8 deletions src/thermal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::system::{read_int, read_str};
use super::Error;
use crate::error::Error;
use colored::*;
use std::fmt::Display;
use std::fmt::Formatter;
Expand Down Expand Up @@ -47,24 +47,23 @@ impl Display for ThermalZone {
}
}

pub fn read_thermal_zones() -> Vec<ThermalZone> {
pub fn read_thermal_zones() -> Result<Vec<ThermalZone>, Error> {
let mut zones = Vec::<ThermalZone>::new();

for a in read_dir(THERMAL_ZONE_DIR).expect("Could not read thermal directory") {
let path_string: String = format!("{}", a.unwrap().path().to_string_lossy());
let path_string: String = format!("{}", a?.path().to_string_lossy());
if !path_string.starts_with(&[THERMAL_ZONE_DIR, "thermal_zone"].concat()) {
continue;
}

let mut zone = ThermalZone::default();

zone.temp = read_int(&[&path_string, "/temp"].concat()).unwrap_or(0);
zone.name = read_str(&[&path_string, "/type"].concat()).unwrap_or("unknown".to_string());
zone.enabled = read_str(&[&path_string, "/mode"].concat()).unwrap_or("disable".to_string())
== "enabled";
zone.temp = read_int(&[&path_string, "/temp"].concat())?;
zone.name = read_str(&[&path_string, "/type"].concat())?;
zone.enabled = read_str(&[&path_string, "/mode"].concat())? == "enabled";
zone.path = path_string;

zones.push(zone);
}
zones
Ok(zones)
}

0 comments on commit 78deed2

Please sign in to comment.