From fd7fa7a51bfc1e8a74abb3935215633ddcd0c2d5 Mon Sep 17 00:00:00 2001 From: jecsham Date: Sun, 22 May 2022 16:30:44 -0500 Subject: [PATCH 1/3] implement error handler --- src-tauri/src/main.rs | 41 ++++++++++++++++++++++++++++------------- src/libs/std_format.ts | 16 +++++++++++++--- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index a2275a5..41dde4e 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,30 +3,35 @@ #![allow(non_snake_case)] use serde::{Deserialize, Serialize}; use serde_json::json; -use wmi::WMIConnection; +use wmi::variant::Variant; +use wmi::{COMLibrary, WMIConnection}; fn get_system_data() -> Result> { - // let com_con = COMLibrary::new().expect("Failed to load COM library"); - // let wmi_con = WMIConnection::new(com_con.into()).expect("Failed to create WMI connection"); + // let com_con = COMLibrary::new()?; + // let wmi_con = WMIConnection::new(com_con.into())?; let wmi_con = unsafe { WMIConnection::with_initialized_com(Some("ROOT\\CIMV2"))? }; + // println!("executed wmi_con"); #[derive(Serialize, Deserialize, Debug)] struct Win32_OperatingSystem { Caption: String, } let result_os: Vec = wmi_con.query()?; + // println!("executed result_os"); #[derive(Serialize, Deserialize, Debug)] struct Win32_Processor { Name: String, } let result_cpu: Vec = wmi_con.query()?; + // println!("executed result_cpu"); #[derive(Serialize, Deserialize, Debug)] struct Win32_VideoController { Name: String, } let result_gpu: Vec = wmi_con.query()?; + // println!("executed result_gpu"); #[derive(Serialize, Deserialize, Debug)] struct Win32_DiskDrive { @@ -34,15 +39,17 @@ fn get_system_data() -> Result> { Size: u64, } let result_disk: Vec = wmi_con.query()?; + // println!("executed result_disk"); #[derive(Serialize, Deserialize, Debug)] struct Win32_PhysicalMemory { Capacity: u64, - Speed: u32, + Speed: Variant, Manufacturer: String, - ConfiguredClockSpeed: u32, + ConfiguredClockSpeed: Variant, } let result_ram: Vec = wmi_con.query()?; + // println!("executed result_ram"); #[derive(Serialize, Deserialize, Debug)] struct Win32_BaseBoard { @@ -50,13 +57,7 @@ fn get_system_data() -> Result> { Product: String, } let result_motherboard: Vec = wmi_con.query()?; - - // used to test not available data - // #[derive(Serialize, Deserialize, Debug)] - // struct Win32_TapeDrive { - // Name: String, - // } - // let not_found: Vec = wmi_con.query()?; + // println!("executed result_motherboard"); let json_response = json!({ "os": result_os, @@ -70,11 +71,25 @@ fn get_system_data() -> Result> { Ok(json_response.to_string()) } +fn error_response(e: String) -> String { + let empty_vec: Vec = vec![]; + return json!({ + "os": empty_vec, + "cpu": empty_vec, + "gpu": empty_vec, + "disk": empty_vec, + "ram": empty_vec, + "motherboard": empty_vec, + "error": vec![e], + }) + .to_string(); +} + #[tauri::command] fn get_system_data_command() -> String { match get_system_data() { Ok(result) => result, - Err(e) => format!("{}", e), + Err(e) => error_response(e.to_string()), } } diff --git a/src/libs/std_format.ts b/src/libs/std_format.ts index 74dab5a..5fac3db 100644 --- a/src/libs/std_format.ts +++ b/src/libs/std_format.ts @@ -27,10 +27,12 @@ const _formatRam = (jsonData: any) => { let orgSize = convert(e.Capacity, "bytes").to("best"); let quantity = Number(orgSize.quantity).toFixed(0); let unit = orgSize.unit.replace("i", ""); + let speed = e.Speed || 0; + let configuredClockSpeed = e.ConfiguredClockSpeed || 0; return `${quantity} ${unit} ${ - e.ConfiguredClockSpeed && e.ConfiguredClockSpeed === e.Speed - ? e.Speed - : `${e.ConfiguredClockSpeed}/${e.Speed}` + configuredClockSpeed === speed + ? speed + : `${configuredClockSpeed}/${speed}` } MHz | ${e.Manufacturer}`; }); return formattedContent; @@ -62,6 +64,14 @@ const _formatOs = (jsonData: any) => { }; const dataStandardFormat = (jsonData: any) => { + if (jsonData.error) { + return [ + { + title: "Error", + content: jsonData.error, + }, + ]; + } let formattedContent = [ { title: "CPU", From 824b11452ffcb7580fb13832215a8d652eb412ee Mon Sep 17 00:00:00 2001 From: jecsham Date: Sun, 22 May 2022 16:30:59 -0500 Subject: [PATCH 2/3] formatting --- src/App.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/App.tsx b/src/App.tsx index 7fedab0..c864877 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -61,7 +61,9 @@ function App() { data-tauri-drag-region className="uk-text-small uk-text-center p-n" > - msi.jecsham.com + + msi.jecsham.com +