From 1f3c75f87800248f1f40e10ebd2f553d2eb8fb6d Mon Sep 17 00:00:00 2001 From: Vignesh Rao Date: Sat, 28 Sep 2024 06:55:23 -0500 Subject: [PATCH] Replace `.is_err` with `match` statements Add a todo section --- src/legacy/cpu.rs | 24 ++++++++++-------------- src/legacy/disks.rs | 7 ++++--- src/legacy/gpu.rs | 9 +++++---- src/lib.rs | 1 + 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/legacy/cpu.rs b/src/legacy/cpu.rs index 4390806..792d79b 100644 --- a/src/legacy/cpu.rs +++ b/src/legacy/cpu.rs @@ -12,12 +12,8 @@ use crate::squire; /// # Returns /// /// A `Option` containing the processor information if successful, otherwise `None`. -fn get_processor_info_darwin(lib_path: &str) -> Result { - let result = squire::util::run_command(lib_path, &["-n", "machdep.cpu.brand_string"], true); - if result.is_err() { - return Err("Failed to get processor info"); - } - Ok(result.unwrap()) +fn get_processor_info_darwin(lib_path: &str) -> Result { + squire::util::run_command(lib_path, &["-n", "machdep.cpu.brand_string"], true) } /// Function to get processor information on Linux. @@ -29,10 +25,10 @@ fn get_processor_info_darwin(lib_path: &str) -> Result { /// # Returns /// /// A `Option` containing the processor information if successful, otherwise `None`. -fn get_processor_info_linux(lib_path: &str) -> Result { +fn get_processor_info_linux(lib_path: &str) -> Result { let file = match File::open(lib_path) { Ok(file) => file, - Err(_) => return Err("Failed to open file"), + Err(_) => return Err(format!("Failed to open '{}'", lib_path)), }; for line in io::BufReader::new(file).lines() { match line { @@ -44,10 +40,10 @@ fn get_processor_info_linux(lib_path: &str) -> Result { } } } - Err(_) => return Err("Error reading line"), + Err(_) => return Err(format!("Error reading lines in '{}'", lib_path)), } } - Err("Model name not found") + Err(format!("Model name not found in '{}'", lib_path)) } /// Function to get processor information on Windows. @@ -59,17 +55,17 @@ fn get_processor_info_linux(lib_path: &str) -> Result { /// # Returns /// /// A `Option` containing the processor information if successful, otherwise `None`. -fn get_processor_info_windows(lib_path: &str) -> Result { +fn get_processor_info_windows(lib_path: &str) -> Result { let result = squire::util::run_command(lib_path, &["cpu", "get", "name"], true); let output = match result { Ok(output) => output, - Err(_) => return Err("Failed to get processor info"), + Err(_) => return Err("Failed to get processor info".to_string()), }; let lines: Vec<&str> = output.trim().split('\n').collect(); if lines.len() > 1 { Ok(lines[1].trim().to_string()) } else { - Err("Invalid output from command") + Err("Invalid output from command".to_string()) } } @@ -86,7 +82,7 @@ pub fn get_name() -> Option { "windows" => get_processor_info_windows("C:\\Windows\\System32\\wbem\\wmic.exe"), _ => { log::error!("Unsupported operating system: {}", operating_system); - Err("Unsupported operating system") + Err("Unsupported operating system".to_string()) } }; match result { diff --git a/src/legacy/disks.rs b/src/legacy/disks.rs index 614d336..4359419 100644 --- a/src/legacy/disks.rs +++ b/src/legacy/disks.rs @@ -154,7 +154,8 @@ fn darwin_disks(lib_path: &str) -> Vec> { .split(":") .nth(1) .unwrap_or_default() - .trim().to_string() + .trim() + .to_string() ); } if info_line.contains("Disk Size:") { @@ -200,9 +201,9 @@ fn reformat_windows(data: &mut HashMap) -> HashMap Vec> { &[], true, ); - if result.is_err() { - return Vec::new(); - } + let output = match result { + Ok(output) => output, + Err(_) => return Vec::new(), + }; let mut gpu_info = Vec::new(); - for line in result.unwrap().lines() { + for line in output.lines() { if line.contains("VGA") { let gpu = line.split(':').last().unwrap().trim(); let mut info = HashMap::new(); diff --git a/src/lib.rs b/src/lib.rs index 675309a..0f7e6e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,7 @@ mod legacy; /// ``` pub async fn start() -> io::Result<()> { let metadata = constant::build_info(); + // todo: Move all commandline process to config - so it configurable via .env files let config = squire::startup::get_config(&metadata); squire::startup::init_logger(config.debug, config.utc_logging, &metadata.crate_name);