diff --git a/src/legacy/disks.rs b/src/legacy/disks.rs index ff02159..614d336 100644 --- a/src/legacy/disks.rs +++ b/src/legacy/disks.rs @@ -121,10 +121,16 @@ fn darwin_disks(lib_path: &str) -> Vec> { } }; let disks: Vec<&str> = output.lines().collect(); - let disk_lines: Vec<&str> = disks.into_iter().filter(|&line| line.starts_with("/dev/disk")).collect(); + let disk_lines: Vec<&str> = disks + .into_iter() + .filter(|&line| line.starts_with("/dev/disk")) + .collect(); let mut disk_info = Vec::new(); for line in disk_lines { - let device_id = line.split_whitespace().next().unwrap(); + let device_id = line + .split_whitespace() + .next() + .unwrap_or_default(); if !is_physical_disk(lib_path, device_id) { continue; } @@ -136,18 +142,41 @@ fn darwin_disks(lib_path: &str) -> Vec> { return Vec::new(); } }; - let info_lines: Vec<&str> = disk_info_output.lines().collect(); + let info_lines: Vec<&str> = disk_info_output + .lines() + .collect(); let mut disk_data = HashMap::new(); for info_line in info_lines { if info_line.contains("Device / Media Name:") { - disk_data.insert("Name".to_string(), info_line.split(":").nth(1).unwrap().trim().to_string()); + disk_data.insert( + "Name".to_string(), + info_line + .split(":") + .nth(1) + .unwrap_or_default() + .trim().to_string() + ); } if info_line.contains("Disk Size:") { - let size_info = info_line.split(":").nth(1).unwrap().split("(").next().unwrap().trim().to_string(); - disk_data.insert("Size".to_string(), size_info); + let size_info = info_line + .split(":") + .nth(1) + .unwrap_or_default() + .split("(") + .next() + .unwrap_or_default() + .trim() + .to_string(); + disk_data.insert( + "Size".to_string(), + size_info + ); } } - disk_data.insert("DeviceID".to_string(), device_id.to_string()); + disk_data.insert( + "DeviceID".to_string(), + device_id.to_string() + ); disk_info.push(disk_data); } disk_info @@ -168,7 +197,14 @@ fn reformat_windows(data: &mut HashMap) -> HashMap Vec> { - let operating_system = std::env::consts::OS; - match operating_system { - "macos" => get_gpu_info_darwin("/usr/sbin/system_profiler"), - "linux" => get_gpu_info_linux("/usr/bin/lspci"), - "windows" => get_gpu_info_windows("C:\\Windows\\System32\\wbem\\wmic.exe"), - _ => { - log::error!("Unsupported operating system: {}", operating_system); - Vec::new() - } - } -} - +/// Function to get GPU information for macOS machines. +/// +/// # Arguments +/// +/// * `lib_path` - The path to the library used to get GPU information. +/// +/// # Returns +/// +/// A `Vec` of `HashMap` containing GPU(s) information if successful, otherwise an empty `Vec`. fn get_gpu_info_darwin(lib_path: &str) -> Vec> { - let result = squire::util::run_command( + let result: Result = squire::util::run_command( lib_path, &["SPDisplaysDataType", "-json"], - true + true, ); - if result.is_err() { - return Vec::new(); + let displays: Vec; + match result { + Ok(json_str) => { + match serde_json::from_str::(&json_str) { + Ok(json) => { + if let Some(d) = json.get("SPDisplaysDataType").and_then(Value::as_array) { + displays = d.to_vec(); + } else { + log::error!("Key 'SPDisplaysDataType' not found or is not an array."); + return Vec::new(); + } + } + Err(err) => { + log::error!("Failed to parse JSON: {}", err); + return Vec::new(); + } + } + } + Err(err) => { + log::error!("Error retrieving result: {}", err); + return Vec::new(); + } } - let json: Value = serde_json::from_str(&result.unwrap()).unwrap(); - - let displays = json["SPDisplaysDataType"].as_array().unwrap(); let mut gpu_info = Vec::new(); - let na = "N/A".to_string(); for display in displays { if let Some(model) = display.get("sppci_model") { @@ -37,7 +49,7 @@ fn get_gpu_info_darwin(lib_path: &str) -> Vec> { "model".to_string(), model.as_str() .unwrap_or(na.as_str()) - .to_string() + .to_string(), ); // Handle cores @@ -68,18 +80,26 @@ fn get_gpu_info_darwin(lib_path: &str) -> Vec> { .unwrap_or(&na) .to_string(), ); - gpu_info.push(info); } } gpu_info } +/// Function to get GPU information for Linux machines. +/// +/// # Arguments +/// +/// * `lib_path` - The path to the library used to get GPU information. +/// +/// # Returns +/// +/// A `Vec` of `HashMap` containing GPU(s) information if successful, otherwise an empty `Vec`. fn get_gpu_info_linux(lib_path: &str) -> Vec> { let result = squire::util::run_command( lib_path, &[], - true + true, ); if result.is_err() { return Vec::new(); @@ -96,11 +116,20 @@ fn get_gpu_info_linux(lib_path: &str) -> Vec> { gpu_info } +/// Function to get GPU information for Windows machines. +/// +/// # Arguments +/// +/// * `lib_path` - The path to the library used to get GPU information. +/// +/// # Returns +/// +/// A `Vec` of `HashMap` containing GPU(s) information if successful, otherwise an empty `Vec`. fn get_gpu_info_windows(lib_path: &str) -> Vec> { let result = squire::util::run_command( lib_path, &["path", "win32_videocontroller", "get", "Name,AdapterCompatibility", "/format:csv"], - true + true, ); let output = match result { Ok(output) => output.to_uppercase(), @@ -108,7 +137,7 @@ fn get_gpu_info_windows(lib_path: &str) -> Vec> { return Vec::new(); } }; - let gpus_raw: Vec<&str> = output.lines().filter(|line| !line.trim().is_empty()).collect(); + let gpus_raw: Vec<&str> = output.lines().filter(|line| !line.trim().is_empty()).collect(); if gpus_raw.is_empty() { log::info!("No GPUs found!"); return vec![]; @@ -117,7 +146,6 @@ fn get_gpu_info_windows(lib_path: &str) -> Vec> { let keys: Vec = gpus_raw[0] .trim() .to_lowercase() - .replace("node", "node") .replace("adaptercompatibility", "vendor") .replace("name", "model") .split(',') @@ -129,7 +157,7 @@ fn get_gpu_info_windows(lib_path: &str) -> Vec> { let mut gpu_info = Vec::new(); let key_len = keys.len(); for chunk in values.chunks(key_len) { - let mut map = std::collections::HashMap::new(); + let mut map = HashMap::new(); for (key, value) in keys.iter().zip(chunk) { map.insert(key.to_string(), value.to_string()); } @@ -137,3 +165,21 @@ fn get_gpu_info_windows(lib_path: &str) -> Vec> { } gpu_info } + +/// OS-agnostic function to get GPU name. +/// +/// # Returns +/// +/// A `Vec` of `HashMap` containing GPU(s) information if successful, otherwise an empty `Vec`. +pub fn get_gpu_info() -> Vec> { + let operating_system = std::env::consts::OS; + match operating_system { + "macos" => get_gpu_info_darwin("/usr/sbin/system_profiler"), + "linux" => get_gpu_info_linux("/usr/bin/lspci"), + "windows" => get_gpu_info_windows("C:\\Windows\\System32\\wbem\\wmic.exe"), + _ => { + log::error!("Unsupported operating system: {}", operating_system); + Vec::new() + } + } +} diff --git a/src/resources/info.rs b/src/resources/info.rs index 7edab60..7a57af6 100644 --- a/src/resources/info.rs +++ b/src/resources/info.rs @@ -41,7 +41,7 @@ pub fn get_disks(disks: &Disks) -> Vec> { fn get_gpu_info() -> Vec { let gpu_info = legacy::gpu::get_gpu_info(); - log::info!("GPUs: {:?}", gpu_info); + log::debug!("GPUs: {:?}", gpu_info); let mut gpus: Vec = vec![]; for gpu in gpu_info { if let Some(gpu_model) = gpu.get("model") {