metadata.rs (1645B)
1 use crate::format::Format; 2 3 /// Info about a single sheet (Excel) or the entire file (other formats). 4 #[derive(Debug, Clone)] 5 pub struct SheetInfo { 6 pub name: String, 7 pub rows: usize, // total rows including header 8 pub cols: usize, 9 } 10 11 /// Info about the file. 12 #[derive(Debug)] 13 pub struct FileInfo { 14 pub file_size: u64, 15 pub format: Format, 16 pub sheets: Vec<SheetInfo>, 17 } 18 19 /// Format file size for display: "245 KB", "1.2 MB", etc. 20 pub fn format_file_size(bytes: u64) -> String { 21 if bytes < 1_024 { 22 format!("{bytes} B") 23 } else if bytes < 1_048_576 { 24 format!("{:.0} KB", bytes as f64 / 1_024.0) 25 } else if bytes < 1_073_741_824 { 26 format!("{:.1} MB", bytes as f64 / 1_048_576.0) 27 } else { 28 format!("{:.1} GB", bytes as f64 / 1_073_741_824.0) 29 } 30 } 31 32 /// Format name for a Format variant. 33 pub fn format_name(fmt: Format) -> &'static str { 34 match fmt { 35 Format::Csv => "CSV", 36 Format::Tsv => "TSV", 37 Format::Parquet => "Parquet", 38 Format::Arrow => "Arrow IPC", 39 Format::Json => "JSON", 40 Format::Ndjson => "NDJSON", 41 Format::Excel => "Excel", 42 } 43 } 44 45 #[cfg(test)] 46 mod tests { 47 use super::*; 48 49 #[test] 50 fn test_format_file_size() { 51 assert_eq!(format_file_size(500), "500 B"); 52 assert_eq!(format_file_size(2_048), "2 KB"); 53 assert_eq!(format_file_size(1_500_000), "1.4 MB"); 54 } 55 56 #[test] 57 fn test_format_name() { 58 assert_eq!(format_name(Format::Csv), "CSV"); 59 assert_eq!(format_name(Format::Parquet), "Parquet"); 60 assert_eq!(format_name(Format::Excel), "Excel"); 61 } 62 }