tauri_plugin_system_info/model/
common.rs

1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4#[derive(Debug, Serialize, Deserialize)]
5pub enum DiskKind {
6    /// HDD type.
7    HDD,
8    /// SSD type.
9    SSD,
10    /// Unknown type.
11    Unknown(isize),
12}
13impl From<sysinfo::DiskKind> for DiskKind {
14    fn from(value: sysinfo::DiskKind) -> Self {
15        match value {
16            sysinfo::DiskKind::HDD => DiskKind::HDD,
17            sysinfo::DiskKind::SSD => DiskKind::SSD,
18            sysinfo::DiskKind::Unknown(isize) => DiskKind::Unknown(isize),
19        }
20    }
21}
22
23#[derive(Debug, Serialize, Deserialize)]
24pub struct MacAddr(pub [u8; 6]);
25
26impl From<sysinfo::MacAddr> for MacAddr {
27    fn from(value: sysinfo::MacAddr) -> Self {
28        MacAddr(value.0)
29    }
30}
31
32#[derive(Debug, Serialize, Deserialize)]
33pub enum ProcessStatus {
34    Idle,
35    Run,
36    Sleep,
37    Stop,
38    Zombie,
39    Tracing,
40    Dead,
41    Wakekill,
42    Waking,
43    Parked,
44    LockBlocked,
45    UninterruptibleDiskSleep,
46    Unknown(u32),
47}
48
49impl From<sysinfo::ProcessStatus> for ProcessStatus {
50    fn from(ps: sysinfo::ProcessStatus) -> Self {
51        match ps {
52            sysinfo::ProcessStatus::Idle => ProcessStatus::Idle,
53            sysinfo::ProcessStatus::Run => ProcessStatus::Run,
54            sysinfo::ProcessStatus::Sleep => ProcessStatus::Sleep,
55            sysinfo::ProcessStatus::Stop => ProcessStatus::Stop,
56            sysinfo::ProcessStatus::Zombie => ProcessStatus::Zombie,
57            sysinfo::ProcessStatus::Tracing => ProcessStatus::Tracing,
58            sysinfo::ProcessStatus::Dead => ProcessStatus::Dead,
59            sysinfo::ProcessStatus::Wakekill => ProcessStatus::Wakekill,
60            sysinfo::ProcessStatus::Waking => ProcessStatus::Waking,
61            sysinfo::ProcessStatus::Parked => ProcessStatus::Parked,
62            sysinfo::ProcessStatus::LockBlocked => ProcessStatus::LockBlocked,
63            sysinfo::ProcessStatus::UninterruptibleDiskSleep => {
64                ProcessStatus::UninterruptibleDiskSleep
65            }
66            sysinfo::ProcessStatus::Unknown(u32) => ProcessStatus::Unknown(u32),
67        }
68    }
69}
70
71#[derive(Debug, Serialize, Deserialize)]
72pub struct DiskUsage {
73    /// Total number of written bytes.
74    pub total_written_bytes: u64,
75    /// Number of written bytes since the last refresh.
76    pub written_bytes: u64,
77    /// Total number of read bytes.
78    pub total_read_bytes: u64,
79    /// Number of read bytes since the last refresh.
80    pub read_bytes: u64,
81}
82
83impl From<sysinfo::DiskUsage> for DiskUsage {
84    fn from(value: sysinfo::DiskUsage) -> Self {
85        DiskUsage {
86            total_written_bytes: value.total_written_bytes,
87            written_bytes: value.written_bytes,
88            total_read_bytes: value.total_read_bytes,
89            read_bytes: value.read_bytes,
90        }
91    }
92}
93
94#[derive(Debug, Serialize, Deserialize)]
95pub struct Cpu {
96    name: String,
97    frequency: u64,
98    cpu_usage: f32,
99    vendor_id: String,
100    brand: String,
101}
102
103impl From<&sysinfo::Cpu> for Cpu {
104    fn from(cpu: &sysinfo::Cpu) -> Self {
105        Cpu {
106            name: cpu.name().to_string(),
107            frequency: cpu.frequency(),
108            cpu_usage: cpu.cpu_usage(),
109            vendor_id: cpu.vendor_id().to_string(),
110            brand: cpu.brand().to_string(),
111        }
112    }
113}
114
115#[derive(Debug, Serialize, Deserialize)]
116pub struct Disk {
117    kind: DiskKind,
118    name: String,
119    file_system: String,
120    mount_point: PathBuf,
121    total_space: u64,
122    available_space: u64,
123    is_removable: bool,
124}
125
126impl From<&sysinfo::Disk> for Disk {
127    fn from(disk: &sysinfo::Disk) -> Self {
128        Disk {
129            kind: disk.kind().into(),
130            name: disk.name().to_string_lossy().into_owned(),
131            file_system: disk.file_system().to_string_lossy().into_owned(),
132            mount_point: disk.mount_point().into(),
133            total_space: disk.total_space(),
134            available_space: disk.available_space(),
135            is_removable: disk.is_removable(),
136        }
137    }
138}
139
140#[derive(Debug, Serialize, Deserialize)]
141pub struct Network {
142    interface_name: String,
143    received: u64,
144    total_received: u64,
145    transmitted: u64,
146    total_transmitted: u64,
147    packets_received: u64,
148    total_packets_received: u64,
149    packets_transmitted: u64,
150    total_packets_transmitted: u64,
151    errors_on_received: u64,
152    total_errors_on_received: u64,
153    errors_on_transmitted: u64,
154    total_errors_on_transmitted: u64,
155    mac_address: MacAddr,
156    mac_address_str: String,
157}
158
159impl Network {
160    pub fn new(name: &str, network_data: &sysinfo::NetworkData) -> Self {
161        Network {
162            interface_name: name.to_string(),
163            received: network_data.received(),
164            total_received: network_data.total_received(),
165            transmitted: network_data.transmitted(),
166            total_transmitted: network_data.total_transmitted(),
167            packets_received: network_data.packets_received(),
168            total_packets_received: network_data.total_packets_received(),
169            packets_transmitted: network_data.packets_transmitted(),
170            total_packets_transmitted: network_data.total_packets_transmitted(),
171            errors_on_received: network_data.errors_on_received(),
172            total_errors_on_received: network_data.total_errors_on_received(),
173            errors_on_transmitted: network_data.errors_on_transmitted(),
174            total_errors_on_transmitted: network_data.total_errors_on_transmitted(),
175            mac_address: network_data.mac_address().into(),
176            mac_address_str: network_data.mac_address().to_string(),
177        }
178    }
179}
180
181#[derive(Debug, Serialize, Deserialize)]
182pub struct Component {
183    temperature: f32,
184    max: f32,
185    critical: Option<f32>,
186    label: String,
187}
188
189impl From<&sysinfo::Component> for Component {
190    fn from(component: &sysinfo::Component) -> Self {
191        Component {
192            temperature: component.temperature(),
193            max: component.max(),
194            critical: component.critical(),
195            label: component.label().to_string(),
196        }
197    }
198}
199
200pub type Uid = String;
201pub type Gid = String;
202pub type Pid = u32;
203
204#[derive(Debug, Serialize, Deserialize)]
205pub struct Process {
206    name: String,
207    cmd: Vec<String>,
208    exe: Option<PathBuf>,
209    pid: Pid,
210    environ: Vec<String>,
211    cwd: Option<PathBuf>,
212    root: Option<PathBuf>,
213    memory: u64,
214    virtual_memory: u64,
215    parent: Option<Pid>,
216    status: ProcessStatus,
217    start_time: u64,
218    run_time: u64,
219    cpu_usage: f32,
220    disk_usage: DiskUsage,
221    user_id: Option<Uid>,
222    effective_user_id: Option<Uid>,
223    group_id: Option<Gid>,
224    effective_group_id: Option<Gid>,
225    session_id: Option<Pid>,
226}
227
228impl From<&sysinfo::Process> for Process {
229    fn from(proc: &sysinfo::Process) -> Self {
230        Process {
231            name: proc.name().to_string(),
232            cmd: proc.cmd().to_vec(),
233            exe: proc.exe().map(|exe| exe.into()),
234            pid: proc.pid().as_u32(),
235            environ: proc.environ().to_vec(),
236            cwd: proc.cwd().map(|cwd| cwd.into()),
237            root: proc.root().map(|root| root.into()),
238            memory: proc.memory(),
239            virtual_memory: proc.virtual_memory(),
240            parent: proc.parent().map(|parent| parent.as_u32()),
241            status: proc.status().into(),
242            start_time: proc.start_time(),
243            run_time: proc.run_time(),
244            cpu_usage: proc.cpu_usage(),
245            disk_usage: proc.disk_usage().into(),
246            user_id: proc.user_id().map(|uid| uid.to_string()),
247            effective_user_id: proc.effective_user_id().map(|uid| uid.to_string()),
248            group_id: proc.group_id().map(|gid| gid.to_string()),
249            effective_group_id: proc.effective_group_id().map(|gid| gid.to_string()),
250            session_id: proc.session_id().map(|session_id| session_id.as_u32()),
251        }
252    }
253}
254
255#[derive(Debug, Serialize, Deserialize)]
256pub enum BatteryState {
257    Unknown,
258    Charging,
259    Discharging,
260    Empty,
261    Full,
262}
263
264impl From<starship_battery::State> for BatteryState {
265    fn from(state: starship_battery::State) -> Self {
266        match state {
267            starship_battery::State::Unknown => BatteryState::Unknown,
268            starship_battery::State::Charging => BatteryState::Charging,
269            starship_battery::State::Discharging => BatteryState::Discharging,
270            starship_battery::State::Empty => BatteryState::Empty,
271            starship_battery::State::Full => BatteryState::Full,
272        }
273    }
274}
275
276#[derive(Debug, Serialize, Deserialize)]
277pub enum Technology {
278    Unknown,
279    LithiumIon,
280    LeadAcid,
281    LithiumPolymer,
282    NickelMetalHydride,
283    NickelCadmium,
284    NickelZinc,
285    LithiumIronPhosphate,
286    RechargeableAlkalineManganese,
287}
288
289impl From<starship_battery::Technology> for Technology {
290    fn from(tech: starship_battery::Technology) -> Self {
291        match tech {
292            starship_battery::Technology::Unknown => Technology::Unknown,
293            starship_battery::Technology::LithiumIon => Technology::LithiumIon,
294            starship_battery::Technology::LeadAcid => Technology::LeadAcid,
295            starship_battery::Technology::LithiumPolymer => Technology::LithiumPolymer,
296            starship_battery::Technology::NickelMetalHydride => Technology::NickelMetalHydride,
297            starship_battery::Technology::NickelCadmium => Technology::NickelCadmium,
298            starship_battery::Technology::NickelZinc => Technology::NickelZinc,
299            starship_battery::Technology::LithiumIronPhosphate => Technology::LithiumIronPhosphate,
300            starship_battery::Technology::RechargeableAlkalineManganese => {
301                Technology::RechargeableAlkalineManganese
302            }
303            _ => Technology::Unknown,
304        }
305    }
306}
307
308#[derive(Debug, Serialize, Deserialize)]
309pub struct Battery {
310    state_of_charge: f32,
311    energy: f32,
312    energy_full: f32,
313    energy_full_design: f32,
314    energy_rate: f32,
315    voltage: f32,
316    state_of_health: f32,
317    state: BatteryState,
318    technology: Technology,
319    temperature_kelvin: Option<f32>,
320    temperature_celsius: Option<f32>,
321    temperature_fahrenheit: Option<f32>,
322    cycle_count: Option<u32>,
323    vendor: Option<String>,
324    model: Option<String>,
325    serial_number: Option<String>,
326    time_to_full: Option<f32>,
327    time_to_empty: Option<f32>,
328}
329
330impl From<starship_battery::Battery> for Battery {
331    fn from(battery: starship_battery::Battery) -> Self {
332        let temp_kelvin = battery.temperature().map(|temp| temp.value);
333        let temp_celsius = temp_kelvin.map(|temp| temp - 273.15);
334        let temp_fahrenheit = temp_celsius.map(|temp| temp * 9.0 / 5.0 + 32.0);
335        Battery {
336            state_of_charge: battery.state_of_charge().value,
337            energy: battery.energy().value,
338            energy_full: battery.energy_full().value,
339            energy_full_design: battery.energy_full_design().value,
340            energy_rate: battery.energy_rate().value,
341            voltage: battery.voltage().value,
342            state_of_health: battery.state_of_health().value,
343            state: battery.state().into(),
344            technology: battery.technology().into(),
345            temperature_kelvin: temp_kelvin,
346            temperature_celsius: temp_celsius,
347            temperature_fahrenheit: temp_fahrenheit,
348            cycle_count: battery.cycle_count(),
349            vendor: battery.vendor().map(|vendor| vendor.to_string()),
350            model: battery.model().map(|model| model.to_string()),
351            serial_number: battery
352                .serial_number()
353                .map(|serial_number| serial_number.to_string()),
354            time_to_full: battery.time_to_full().map(|time| time.value),
355            time_to_empty: battery.time_to_empty().map(|time| time.value),
356        }
357    }
358}