zenyx-engine-telemetry/subcrates/telemetry/src/modules/storage.rs
D0RYU f8316f8ee4 refactored formatted scripts
cpu struct types being worked on
cpu error handling fixed
moved from anyhow -> thiserror
other shit i'm too tired to mention
2025-05-03 01:08:55 -04:00

49 lines
1.3 KiB
Rust

use crate::modules::FmtBytes;
use serde::{Serialize, Deserialize};
use serde_json::{Value, json};
pub use sysinfo::DiskKind;
use sysinfo::Disks;
use thiserror::Error;
#[derive(Debug, Serialize, Deserialize)]
pub struct StorageInfo {
name: String,
mount_point: String,
disk_kind: DiskKind,
space_left: FmtBytes,
total: FmtBytes,
}
#[derive(Debug, Error)]
pub enum StorageError {
#[error("No storage drives found")]
NoDisksFound,
#[error("Failed to build JSON for Vec<StorageInfo>")]
JsonError(#[from] serde_json::Error),
}
#[allow(dead_code)]
pub fn get_list() -> Result<Vec<StorageInfo>, StorageError> {
let disks = Disks::new_with_refreshed_list();
let disk_list = disks.list();
if disk_list.is_empty() {
return Err(StorageError::NoDisksFound);
}
Ok(disk_list
.iter()
.map(|disk| StorageInfo {
name: disk.name().to_string_lossy().into_owned(),
mount_point: disk.mount_point().to_string_lossy().replace('\\', ""),
disk_kind: disk.kind(),
space_left: FmtBytes(disk.available_space()),
total: FmtBytes(disk.total_space()),
})
.collect())
}
#[allow(dead_code)]
pub fn get_json() -> Result<Value, StorageError> {
Ok(json!(get_list()?))
}