1use std::path::Path;
4
5pub fn dir_size(path: &Path) -> Option<u64> {
7 if !path.is_dir() {
8 return Some(0);
9 }
10 let mut total = 0u64;
11 let entries = std::fs::read_dir(path).ok()?;
12 for entry in entries.flatten() {
13 let path = entry.path();
14 if path.is_dir() {
15 total += dir_size(&path)?;
16 } else {
17 total += entry.metadata().ok().map(|m| m.len()).unwrap_or(0);
18 }
19 }
20 Some(total)
21}
22
23pub fn format_bytes(bytes: u64) -> String {
25 const KB: u64 = 1024;
26 const MB: u64 = KB * 1024;
27 const GB: u64 = MB * 1024;
28 if bytes >= GB {
29 format!("{:.1} GB", bytes as f64 / GB as f64)
30 } else if bytes >= MB {
31 format!("{:.1} MB", bytes as f64 / MB as f64)
32 } else if bytes >= KB {
33 format!("{:.1} KB", bytes as f64 / KB as f64)
34 } else {
35 format!("{} B", bytes)
36 }
37}