diff --git a/src-tauri/src/lib/os.rs b/src-tauri/src/lib/os.rs index ba232a9..5030e05 100644 --- a/src-tauri/src/lib/os.rs +++ b/src-tauri/src/lib/os.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; #[cfg(target_os = "macos")] pub fn get_current_window Option>( resolve_resource: F, -) -> Option { +) -> Result { use std::process::Command; let script_location = resolve_resource("./src/assets/macos_active_window.as").unwrap(); let mut command = Command::new("sh"); @@ -18,10 +18,9 @@ pub fn get_current_window Option>( let success = result.trim().len() > 0; if success { - return Some(result); + return Ok(result); } - println!("failed to get active window, remove and readd freedeck-configurator to accessibility list?"); - return None; + Err(anyhow!("failed to get active window, remove and readd freedeck-configurator to accessibility list?")) } #[cfg(target_os = "linux")] @@ -48,7 +47,7 @@ pub fn get_current_window Option>( #[cfg(target_os = "windows")] pub fn get_current_window Option>( _resolve_resource: F, -) -> Option { +) -> Result { use std::{ffi::OsString, os::windows::prelude::OsStringExt}; use winapi::um::winuser::{GetForegroundWindow, GetWindowTextW}; unsafe { @@ -56,11 +55,17 @@ pub fn get_current_window Option>( let mut text: [u16; 512] = [0; 512]; let _result: usize = GetWindowTextW(window, text.as_mut_ptr(), text.len() as i32) as usize; let (short, _) = text.split_at(_result); - let result = OsString::from_wide(&short).to_str().unwrap().to_string(); - let success = result.trim().len() > 0; + let result = OsString::from_wide(short) + .to_str() + .expect("os string conversion error") + .to_string(); + let success = !result.trim().is_empty(); if success { - return Some(OsString::from_wide(&short).to_str().unwrap().to_string()); + return Ok(OsString::from_wide(short) + .to_str() + .expect("os string conversion error") + .to_string()); } } - None + Err(anyhow!("windows did an oopsy")) } diff --git a/src-tauri/src/lib/system/win.rs b/src-tauri/src/lib/system/win.rs index 2863308..27f79f2 100644 --- a/src-tauri/src/lib/system/win.rs +++ b/src-tauri/src/lib/system/win.rs @@ -1,5 +1,5 @@ -use std::{collections::HashMap, sync::Arc}; - +use anyhow::Result; +use std::collections::HashMap; use wmi::*; // struct SaveCOM(COMLibrary); @@ -10,48 +10,59 @@ pub struct SystemInfo { } impl SystemInfo { - pub fn new() -> Option { - match WMIConnection::with_namespace_path( - "root\\OpenHardwareMonitor", - COMLibrary::new().unwrap(), - ) { - Ok(sys) => Some(SystemInfo { sys }), - Err(_) => None, - } + pub fn new() -> Result { + let lib = COMLibrary::new()?; + let connection = WMIConnection::with_namespace_path("root\\OpenHardwareMonitor", lib)?; + Ok(SystemInfo { sys: connection }) } pub fn cpu_temp(&mut self) -> f32 { - let cpu_results: Vec> = - self.sys.raw_query("SELECT * FROM Sensor where Identifier = '/amdcpu/0/temperature/0' or Identifier = '/intelcpu/0/temperature/0' AND SensorType = 'Temperature'").unwrap(); - if cpu_results.len() == 0 { + let cpu_results: Result>, WMIError> = + self.sys.raw_query("SELECT * FROM Sensor where Identifier = '/amdcpu/0/temperature/0' or Identifier = '/intelcpu/0/temperature/0' AND SensorType = 'Temperature'"); + + let cpu_results = match cpu_results { + Ok(result) => result, + Err(_e) => return 0.0, + }; + + if cpu_results.is_empty() { 0.0 } else { let cpu = &cpu_results[0]; if let Some(Variant::R4(value)) = cpu.get("Value") { - value.clone() + value.to_owned() } else { 0.0 } } } pub fn gpu_temp(&mut self) -> f32 { - let gpu_results: Vec> = - self.sys.raw_query("SELECT * FROM Sensor where Identifier = '/atigpu/0/temperature/0' or Identifier = '/nvidiagpu/0/temperature/0' AND SensorType = 'Temperature'").unwrap(); - if gpu_results.len() == 0 { + let gpu_results: Result>, WMIError> = + self.sys.raw_query("SELECT * FROM Sensor where Identifier = '/atigpu/0/temperature/0' or Identifier = '/nvidiagpu/0/temperature/0' AND SensorType = 'Temperature'"); + + let gpu_results = match gpu_results { + Ok(result) => result, + Err(_e) => return 0.0, + }; + + if gpu_results.is_empty() { 0.0 } else { let cpu = &gpu_results[0]; if let Some(Variant::R4(value)) = cpu.get("Value") { - value.clone() + value.to_owned() } else { 0.0 } } } pub fn list_sensors(&mut self) -> Vec { - let sensor_list_result: Vec> = self + let sensor_list_result: Vec> = match self .sys .raw_query("SELECT * FROM Sensor where SensorType='Temperature'") - .unwrap(); + { + Ok(result) => result, + Err(e) => return vec![e.to_string()], + }; sensor_list_result .iter()