forked from nonsensical-dev/zenyx-engine
47 lines
1.2 KiB
Rust
47 lines
1.2 KiB
Rust
|
use crate::{custom_anyhow, modules::FmtBytes};
|
||
|
use anyhow::Result;
|
||
|
use serde::Serialize;
|
||
|
use serde_json::{Value, json};
|
||
|
pub use sysinfo::DiskKind;
|
||
|
use sysinfo::Disks;
|
||
|
|
||
|
#[derive(Debug, Serialize)]
|
||
|
pub struct StorageInfo {
|
||
|
name: String,
|
||
|
mount_point: String,
|
||
|
disk_kind: DiskKind,
|
||
|
space_left: FmtBytes,
|
||
|
total: FmtBytes,
|
||
|
}
|
||
|
|
||
|
#[allow(dead_code)]
|
||
|
pub fn get_list() -> Result<Vec<StorageInfo>> {
|
||
|
let disks = Disks::new_with_refreshed_list();
|
||
|
let mut drive_data = Vec::new();
|
||
|
|
||
|
for disk in disks.list() {
|
||
|
drive_data.push(StorageInfo {
|
||
|
name: format!("{:?}", disk.name()).trim_matches('\"').to_string(),
|
||
|
mount_point: format!("{:?}", disk.mount_point())
|
||
|
.replace("\\", "")
|
||
|
.trim_matches('\"')
|
||
|
.to_string(),
|
||
|
disk_kind: disk.kind(),
|
||
|
space_left: FmtBytes(disk.available_space()),
|
||
|
total: FmtBytes(disk.total_space()),
|
||
|
});
|
||
|
}
|
||
|
|
||
|
if drive_data.is_empty() {
|
||
|
return Err(custom_anyhow!("No storage drives found")
|
||
|
.context("Drive_data is empty, expected disks.list() to return non-zero at line 39"));
|
||
|
}
|
||
|
|
||
|
Ok(drive_data)
|
||
|
}
|
||
|
|
||
|
#[allow(dead_code)]
|
||
|
pub fn get_json() -> Result<Value> {
|
||
|
Ok(json!(get_list()?))
|
||
|
}
|