From e765ceddfc8109abdc1990a813ed759a5ff2b7ee Mon Sep 17 00:00:00 2001 From: arch_agent Date: Mon, 15 Jun 2026 17:09:40 +0200 Subject: [PATCH] Add ./src/utils.rs --- src/utils.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/utils.rs diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..8f77bd7 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,28 @@ +use anyhow::Result; +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; + +/// Gemeinsame Utility-Funktionen +pub fn get_project_dirs() -> Result { + directories::ProjectDirs::from("eu", "heimatlosen", "aegisaur") + .ok_or_else(|| anyhow::anyhow!("Konnte Projekt-Verzeichnisse nicht ermitteln")) +} + +/// Formatiert Bytes als menschenlesbare Größe +pub fn format_bytes(bytes: u64) -> String { + const UNITS: [&str; 6] = ["B", "KB", "MB", "GB", "TB", "PB"]; + let mut size = bytes as f64; + let mut unit_index = 0; + + while size >= 1024.0 && unit_index < UNITS.len() - 1 { + size /= 1024.0; + unit_index += 1; + } + + format!("{:.2} {}", size, UNITS[unit_index]) +} + +/// Prüft ob ein Befehl verfügbar ist +pub fn command_exists(cmd: &str) -> bool { + which::which(cmd).is_ok() +}