hardware_query/
storage.rs

1use crate::Result;
2use serde::{Deserialize, Serialize};
3
4/// Storage device type
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6pub enum StorageType {
7    SSD,
8    HDD,
9    NVMe,
10    EMmc,
11    SD,
12    USB,
13    Unknown,
14}
15
16impl std::fmt::Display for StorageType {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            StorageType::SSD => write!(f, "SSD"),
20            StorageType::HDD => write!(f, "HDD"),
21            StorageType::NVMe => write!(f, "NVMe"),
22            StorageType::EMmc => write!(f, "eMMC"),
23            StorageType::SD => write!(f, "SD Card"),
24            StorageType::USB => write!(f, "USB"),
25            StorageType::Unknown => write!(f, "Unknown"),
26        }
27    }
28}
29
30/// Storage device information
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct StorageInfo {
33    /// Device name/model
34    pub model: String,
35    /// Storage type
36    pub storage_type: StorageType,
37    /// Total capacity in GB
38    pub capacity_gb: f64,
39    /// Available space in GB
40    pub available_gb: f64,
41    /// Used space in GB
42    pub used_gb: f64,
43    /// Mount point or drive letter
44    pub mount_point: String,
45    /// File system type
46    pub file_system: Option<String>,
47    /// Is removable
48    pub removable: bool,
49    /// Read speed in MB/s (if available)
50    pub read_speed_mb_s: Option<f32>,
51    /// Write speed in MB/s (if available)
52    pub write_speed_mb_s: Option<f32>,
53}
54
55impl StorageInfo {
56    /// Query all storage devices
57    pub fn query_all() -> Result<Vec<Self>> {
58        // Note: The current version of sysinfo doesn't expose disk APIs
59        // This would be implemented using platform-specific APIs
60        let storage_devices = vec![Self {
61            model: "System Disk".to_string(),
62            storage_type: StorageType::SSD,
63            capacity_gb: 256.0,
64            available_gb: 128.0,
65            used_gb: 128.0,
66            mount_point: "C:".to_string(),
67            file_system: Some("NTFS".to_string()),
68            removable: false,
69            read_speed_mb_s: None,
70            write_speed_mb_s: None,
71        }];
72
73        Ok(storage_devices)
74    }
75
76    /// Get device model/name
77    pub fn model(&self) -> &str {
78        &self.model
79    }
80
81    /// Get storage type
82    pub fn drive_type(&self) -> &StorageType {
83        &self.storage_type
84    }
85
86    /// Get total capacity in GB
87    pub fn capacity_gb(&self) -> f64 {
88        self.capacity_gb
89    }
90
91    /// Get available space in GB
92    pub fn available_gb(&self) -> f64 {
93        self.available_gb
94    }
95
96    /// Get used space in GB
97    pub fn used_gb(&self) -> f64 {
98        self.used_gb
99    }
100
101    /// Get usage percentage
102    pub fn usage_percent(&self) -> f64 {
103        if self.capacity_gb > 0.0 {
104            (self.used_gb / self.capacity_gb) * 100.0
105        } else {
106            0.0
107        }
108    }
109
110    /// Check if device has sufficient free space
111    pub fn has_free_space(&self, required_gb: f64) -> bool {
112        self.available_gb >= required_gb
113    }
114}