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

Reduce unwrap in thermal code #409

Merged
merged 3 commits into from
Sep 10, 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
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)
}