1use std::path::Path;
4
5use sysinfo::{Disks, MemoryRefreshKind, RefreshKind, System};
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub struct Machine {
10 pub memory: u64,
12 pub free_disk: u64,
14}
15
16impl Machine {
17 pub fn detect(folder: &Path) -> Self {
20 let system = System::new_with_specifics(
21 RefreshKind::nothing().with_memory(MemoryRefreshKind::nothing().with_ram()),
22 );
23 Self { memory: system.total_memory(), free_disk: free_disk(folder) }
24 }
25}
26
27fn free_disk(folder: &Path) -> u64 {
29 let absolute = std::path::absolute(folder).unwrap_or_else(|_| folder.to_path_buf());
30 let disks = Disks::new_with_refreshed_list();
31 disks
32 .list()
33 .iter()
34 .filter(|disk| absolute.starts_with(disk.mount_point()))
35 .max_by_key(|disk| disk.mount_point().as_os_str().len())
36 .map_or(0, sysinfo::Disk::available_space)
37}