br-system 0.0.11

This is an System
Documentation
use std::process::Command;

#[cfg(target_os = "macos")]
mod mac;
#[cfg(target_os = "windows")]
mod win;
#[derive(Debug, Clone)]
pub struct SystemInfo {
    /// CPU核心数量
    pub cores: usize,
    /// 内存容量
    pub memory: String,
    /// 硬盘的序列号
    pub serial_number: Vec<String>,
    /// 硬件uuid
    pub hardware_uuid: String,
    /// 设备UUID
    pub uuid: String,
}

impl SystemInfo {
    pub fn new() -> Result<Self, String> {
        let mut that = Self {
            cores: 0,
            memory: "".to_string(),
            serial_number: vec![],
            hardware_uuid: "".to_string(),
            uuid: "".to_string(),
        };
        if cfg!(target_os = "macos") {
            #[cfg(target_os = "macos")]
            {
                return that.mac_os();
            }
        }
        if cfg!(target_os = "windows") {
            #[cfg(target_os = "windows")]
            {
                return that.windows().cloned();
            }
        }
        Ok(that)
    }
    #[cfg(target_os = "macos")]
    pub fn mac_os(&mut self) -> Result<Self, String> {
        self.uuid = mac::get_uuid().unwrap_or("".to_string());
        self.serial_number = vec![self.uuid.clone()];
        Ok(self.clone())
    }

    #[cfg(target_os = "windows")]
    pub fn windows(&mut self) -> Result<&mut Self, String> {
        // 硬盘序列号
        let disk_output = match Command::new("powershell")
            .args(&["(Get-WmiObject -Class Win32_ComputerSystemProduct).UUID.Trim()"])
            .output() {
            Ok(e) => e,
            Err(e) => {
                return Err(format!("{}", e.to_string()));
            }
        };
        let serial_number = String::from_utf8_lossy(&disk_output.stdout);
        for line in serial_number.lines() {
            let text = line.trim();
            self.uuid=text.to_string();
        }
        Ok(self)
    }
}