Skip to content

Commit

Permalink
rust refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jecsham committed Jun 5, 2022
1 parent d45f82f commit 6f8918e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 96 deletions.
1 change: 1 addition & 0 deletions src-tauri/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod system_data;
92 changes: 92 additions & 0 deletions src-tauri/src/commands/system_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use serde::{Deserialize, Serialize};
use serde_json::json;
use wmi::variant::Variant;
use wmi::WMIConnection;

fn get_system_data() -> Result<String, Box<dyn std::error::Error>> {
// 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<Win32_OperatingSystem> = wmi_con.query()?;
// println!("executed result_os");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_Processor {
Name: String,
}
let result_cpu: Vec<Win32_Processor> = wmi_con.query()?;
// println!("executed result_cpu");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_VideoController {
Name: String,
}
let result_gpu: Vec<Win32_VideoController> = wmi_con.query()?;
// println!("executed result_gpu");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_DiskDrive {
Caption: String,
Size: u64,
}
let result_disk: Vec<Win32_DiskDrive> = wmi_con.query()?;
// println!("executed result_disk");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_PhysicalMemory {
Capacity: u64,
Speed: Variant,
Manufacturer: String,
ConfiguredClockSpeed: Variant,
}
let result_ram: Vec<Win32_PhysicalMemory> = wmi_con.query()?;
// println!("executed result_ram");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_BaseBoard {
Manufacturer: String,
Product: String,
}
let result_motherboard: Vec<Win32_BaseBoard> = wmi_con.query()?;
// println!("executed result_motherboard");

let json_response = json!({
"os": result_os,
"cpu": result_cpu,
"gpu": result_gpu,
"disk": result_disk,
"ram": result_ram,
"motherboard": result_motherboard,
});

Ok(json_response.to_string())
}

fn error_response(e: String) -> String {
let empty_vec: Vec<String> = vec![];
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]
pub fn system_data_command() -> String {
match get_system_data() {
Ok(result) => result,
Err(e) => error_response(e.to_string()),
}
}
99 changes: 4 additions & 95 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,101 +1,10 @@
#![windows_subsystem = "windows"]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use serde::{Deserialize, Serialize};
use serde_json::json;
use wmi::variant::Variant;
use wmi::WMIConnection;

fn get_system_data() -> Result<String, Box<dyn std::error::Error>> {
// 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<Win32_OperatingSystem> = wmi_con.query()?;
// println!("executed result_os");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_Processor {
Name: String,
}
let result_cpu: Vec<Win32_Processor> = wmi_con.query()?;
// println!("executed result_cpu");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_VideoController {
Name: String,
}
let result_gpu: Vec<Win32_VideoController> = wmi_con.query()?;
// println!("executed result_gpu");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_DiskDrive {
Caption: String,
Size: u64,
}
let result_disk: Vec<Win32_DiskDrive> = wmi_con.query()?;
// println!("executed result_disk");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_PhysicalMemory {
Capacity: u64,
Speed: Variant,
Manufacturer: String,
ConfiguredClockSpeed: Variant,
}
let result_ram: Vec<Win32_PhysicalMemory> = wmi_con.query()?;
// println!("executed result_ram");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_BaseBoard {
Manufacturer: String,
Product: String,
}
let result_motherboard: Vec<Win32_BaseBoard> = wmi_con.query()?;
// println!("executed result_motherboard");

let json_response = json!({
"os": result_os,
"cpu": result_cpu,
"gpu": result_gpu,
"disk": result_disk,
"ram": result_ram,
"motherboard": result_motherboard,
});

Ok(json_response.to_string())
}

fn error_response(e: String) -> String {
let empty_vec: Vec<String> = 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) => error_response(e.to_string()),
}
}
// #![windows_subsystem = "windows"]
mod commands;
use commands::system_data::system_data_command;

fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![get_system_data_command])
.invoke_handler(tauri::generate_handler![system_data_command])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function App() {
};

const getSystemDataFromAPI = async () => {
let val: any = await invoke("get_system_data_command");
let val: any = await invoke("system_data_command");
if (val) {
try {
let decodedJson = dataStandardFormat(JSON.parse(val));
Expand Down

0 comments on commit 6f8918e

Please sign in to comment.