From 5979b6c8d86541582c6e51e44c4f881617cf9ed7 Mon Sep 17 00:00:00 2001 From: Cameron Bannasch Date: Thu, 22 Feb 2024 13:14:22 -0800 Subject: [PATCH 1/5] Modify `list_cpus()` to return a result and adjust the functions which depend on them to return result. --- src/system.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/system.rs b/src/system.rs index e4d9b62..915bab0 100644 --- a/src/system.rs +++ b/src/system.rs @@ -182,8 +182,7 @@ pub fn check_available_governors() -> Result, Error> { } /// Get all the cpus (cores), returns cpus from 0 to the (amount of cores -1) the machine has -#[once] -pub fn list_cpus() -> Vec { +pub fn list_cpus() -> Result, Error> { let mut cpus: Vec = Vec::::new(); // Get each item in the cpu directory @@ -228,30 +227,30 @@ pub fn list_cpus() -> Vec { gov: "Unknown".to_string(), }; - new.init_cpu().unwrap(); + new.init_cpu()?; - new.update().unwrap(); + new.update()?; to_return.push(new) } to_return.sort_by(|a, b| a.number.cmp(&b.number)); - to_return + Ok(to_return) } /// Get a vector of speeds reported from each cpu from list_cpus -pub fn list_cpu_speeds() -> Vec { - list_cpus().into_iter().map(|x| x.cur_freq).collect() +pub fn list_cpu_speeds() -> Result, Error> { + Ok(list_cpus()?.into_iter().map(|x| x.cur_freq).collect()) } /// Get a vector of temperatures reported from each cpu from list_cpus -pub fn list_cpu_temp() -> Vec { - list_cpus().into_iter().map(|x| x.cur_temp).collect() +pub fn list_cpu_temp() -> Result, Error> { + Ok(list_cpus()?.into_iter().map(|x| x.cur_temp).collect()) } /// Get a vector of the governors that the cpus from list_cpus -pub fn list_cpu_governors() -> Vec { - list_cpus().into_iter().map(|x| x.gov).collect() +pub fn list_cpu_governors() -> Result, Error> { + Ok(list_cpus()?.into_iter().map(|x| x.gov).collect()) } pub fn read_int(path: &str) -> Result { From c9ac547cdce4f26bbc7df44d4e6789a758a1481b Mon Sep 17 00:00:00 2001 From: Cameron Bannasch Date: Thu, 22 Feb 2024 13:15:22 -0800 Subject: [PATCH 2/5] Add updated error handling for use of `list_cpus()` --- src/daemon.rs | 15 ++++++++++-- src/interface.rs | 61 +++++++++++++++++++++++++++++++++++++----------- src/system.rs | 2 +- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index 854b5d6..5da1ca7 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -640,8 +640,19 @@ pub fn daemon_init(settings: Settings, config: Config) -> Arc> { } // Make a cpu struct for each cpu listed - for cpu in list_cpus() { - daemon.cpus.push(cpu); + match list_cpus() { + Ok(cpus) => { + + for cpu in cpus { + daemon.cpus.push(cpu); + } + + }, + Err(e) => { + + daemon.logger.log(&format!("Failed to read from CPUs: {:?}", e), logger::Severity::Error) + + }, } let daemon_mutex = Arc::new(Mutex::new(daemon)); diff --git a/src/interface.rs b/src/interface.rs index 496a0fe..2b0cc11 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -141,8 +141,15 @@ pub trait Getter { impl Getter for Get { fn freq(&self, raw: bool) { - let f = check_cpu_freq(&list_cpus()); - print_freq(f, raw); + match list_cpus() { + Ok(cpus) => { + let f = check_cpu_freq(&cpus); + print_freq(f, raw); + }, + Err(e) => { + eprint!("Failed to get cpu information, an error occured: {:?}", e); + }, + } } fn power(&self, raw: bool) { @@ -230,26 +237,54 @@ impl Getter for Get { } fn cpus(&self, raw: bool) { - let cpus = list_cpus(); - match check_cpu_name() { - Ok(name) => print_cpus(cpus, name, raw), - Err(_) => println!("Failed get list of cpus"), - }; + let cpus_result = list_cpus(); + match cpus_result { + Ok(cpus) => { + match check_cpu_name() { + Ok(name) => print_cpus(cpus, name, raw), + Err(_) => println!("Failed get list of cpus"), + }; + }, + Err(e) => { + eprintln!("Failed to get cpu information, an error occured: {:?}", e); + }, + } } fn speeds(&self, raw: bool) { - let speeds = list_cpu_speeds(); - print_cpu_speeds(speeds, raw); + let speeds_result = list_cpu_speeds(); + match speeds_result { + Ok(speeds) => { + print_cpu_speeds(speeds, raw); + }, + Err(e) => { + eprintln!("Failed to get cpu speed information, an error occured: {:?}", e); + }, + } } fn temp(&self, raw: bool) { - let cpu_temp = list_cpu_temp(); - print_cpu_temp(cpu_temp, raw); + let cpu_temp_result = list_cpu_temp(); + match cpu_temp_result { + Ok(cpu_temp) => { + print_cpu_temp(cpu_temp, raw); + }, + Err(e) => { + eprintln!("Failed to get cpu temperature information, an error occured: {:?}", e); + }, + } } fn govs(&self, raw: bool) { - let govs = list_cpu_governors(); - print_cpu_governors(govs, raw); + let govs_result = list_cpu_governors(); + match govs_result { + Ok(govs) => { + print_cpu_governors(govs, raw); + }, + Err(e) => { + eprintln!("Failed to get cpu governor information, an error occured: {:?}", e); + }, + } } fn bat_cond(&self, raw: bool) { diff --git a/src/system.rs b/src/system.rs index 915bab0..3932d18 100644 --- a/src/system.rs +++ b/src/system.rs @@ -281,7 +281,7 @@ mod tests { #[test] fn check_cpu_freq_acs_test() { - assert!(check_cpu_freq(&list_cpus()) > 0.0); + assert!(check_cpu_freq(&list_cpus().unwrap()) > 0.0); } #[test] From 8435ce20d9e6f3bfc9f714b6599c204d76a4b2b2 Mon Sep 17 00:00:00 2001 From: Cameron Bannasch Date: Thu, 22 Feb 2024 18:20:41 -0800 Subject: [PATCH 3/5] Update using cargo fmt --- src/daemon.rs | 13 +++++-------- src/interface.rs | 35 ++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index 5da1ca7..757968e 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -642,17 +642,14 @@ pub fn daemon_init(settings: Settings, config: Config) -> Arc> { // Make a cpu struct for each cpu listed match list_cpus() { Ok(cpus) => { - for cpu in cpus { daemon.cpus.push(cpu); } - - }, - Err(e) => { - - daemon.logger.log(&format!("Failed to read from CPUs: {:?}", e), logger::Severity::Error) - - }, + } + Err(e) => daemon.logger.log( + &format!("Failed to read from CPUs: {:?}", e), + logger::Severity::Error, + ), } let daemon_mutex = Arc::new(Mutex::new(daemon)); diff --git a/src/interface.rs b/src/interface.rs index 2b0cc11..4ac4839 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -145,10 +145,10 @@ impl Getter for Get { Ok(cpus) => { let f = check_cpu_freq(&cpus); print_freq(f, raw); - }, + } Err(e) => { eprint!("Failed to get cpu information, an error occured: {:?}", e); - }, + } } } @@ -244,10 +244,10 @@ impl Getter for Get { Ok(name) => print_cpus(cpus, name, raw), Err(_) => println!("Failed get list of cpus"), }; - }, + } Err(e) => { eprintln!("Failed to get cpu information, an error occured: {:?}", e); - }, + } } } @@ -256,10 +256,13 @@ impl Getter for Get { match speeds_result { Ok(speeds) => { print_cpu_speeds(speeds, raw); - }, + } Err(e) => { - eprintln!("Failed to get cpu speed information, an error occured: {:?}", e); - }, + eprintln!( + "Failed to get cpu speed information, an error occured: {:?}", + e + ); + } } } @@ -268,10 +271,13 @@ impl Getter for Get { match cpu_temp_result { Ok(cpu_temp) => { print_cpu_temp(cpu_temp, raw); - }, + } Err(e) => { - eprintln!("Failed to get cpu temperature information, an error occured: {:?}", e); - }, + eprintln!( + "Failed to get cpu temperature information, an error occured: {:?}", + e + ); + } } } @@ -280,10 +286,13 @@ impl Getter for Get { match govs_result { Ok(govs) => { print_cpu_governors(govs, raw); - }, + } Err(e) => { - eprintln!("Failed to get cpu governor information, an error occured: {:?}", e); - }, + eprintln!( + "Failed to get cpu governor information, an error occured: {:?}", + e + ); + } } } From 3eb429b08cb41729aabd7e5b1fc03708f591fa9e Mon Sep 17 00:00:00 2001 From: Cameron Bannasch Date: Thu, 22 Feb 2024 18:39:31 -0800 Subject: [PATCH 4/5] Fix tests to use new error handling --- src/system.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/system.rs b/src/system.rs index 3932d18..92bf2c7 100644 --- a/src/system.rs +++ b/src/system.rs @@ -1,4 +1,3 @@ -use cached::proc_macro::once; use std::fs::{self, read_dir}; use std::path::Path; use std::string::String; @@ -520,9 +519,9 @@ microcode : 0xea #[test] fn list_cpus_acs_test() { - assert_eq!(type_of(list_cpus()), type_of(Vec::::new())); + assert_eq!(type_of(list_cpus().unwrap()), type_of(Vec::::new())); - for x in list_cpus() { + for x in list_cpus().unwrap() { assert!(!x.name.is_empty()); assert!(x.max_freq > 0); assert!(x.min_freq > 0); @@ -537,9 +536,9 @@ microcode : 0xea #[test] fn list_cpu_speeds_acs_test() -> Result<(), Error> { // Type check - assert_eq!(type_of(list_cpu_speeds()), type_of(Vec::::new())); + assert_eq!(type_of(list_cpu_speeds().unwrap()), type_of(Vec::::new())); - for x in list_cpu_speeds() { + for x in list_cpu_speeds().unwrap() { assert!(x > 0); } Ok(()) @@ -548,9 +547,9 @@ microcode : 0xea #[test] fn list_cpu_temp_acs_test() { // Type check - assert_eq!(type_of(list_cpu_temp()), type_of(Vec::::new())); + assert_eq!(type_of(list_cpu_temp().unwrap()), type_of(Vec::::new())); - for x in list_cpu_temp() { + for x in list_cpu_temp().unwrap() { assert!(x > -100); } } @@ -558,9 +557,9 @@ microcode : 0xea #[test] fn list_cpu_governors_acs_test() { // Type check - assert_eq!(type_of(list_cpu_governors()), type_of(Vec::::new())); + assert_eq!(type_of(list_cpu_governors().unwrap()), type_of(Vec::::new())); - for x in list_cpu_governors() { + for x in list_cpu_governors().unwrap() { assert!(x == "powersave" || x == "performance" || x == "schedutil"); } } From b1de4625b083397b6302a9670af86054389c6e29 Mon Sep 17 00:00:00 2001 From: Cameron Bannasch Date: Thu, 22 Feb 2024 18:41:30 -0800 Subject: [PATCH 5/5] Reformat tests --- src/system.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/system.rs b/src/system.rs index 92bf2c7..e7138db 100644 --- a/src/system.rs +++ b/src/system.rs @@ -536,7 +536,10 @@ microcode : 0xea #[test] fn list_cpu_speeds_acs_test() -> Result<(), Error> { // Type check - assert_eq!(type_of(list_cpu_speeds().unwrap()), type_of(Vec::::new())); + assert_eq!( + type_of(list_cpu_speeds().unwrap()), + type_of(Vec::::new()) + ); for x in list_cpu_speeds().unwrap() { assert!(x > 0); @@ -547,7 +550,10 @@ microcode : 0xea #[test] fn list_cpu_temp_acs_test() { // Type check - assert_eq!(type_of(list_cpu_temp().unwrap()), type_of(Vec::::new())); + assert_eq!( + type_of(list_cpu_temp().unwrap()), + type_of(Vec::::new()) + ); for x in list_cpu_temp().unwrap() { assert!(x > -100); @@ -557,7 +563,10 @@ microcode : 0xea #[test] fn list_cpu_governors_acs_test() { // Type check - assert_eq!(type_of(list_cpu_governors().unwrap()), type_of(Vec::::new())); + assert_eq!( + type_of(list_cpu_governors().unwrap()), + type_of(Vec::::new()) + ); for x in list_cpu_governors().unwrap() { assert!(x == "powersave" || x == "performance" || x == "schedutil");