Skip to main content

oxigeo_cli/util/
mod.rs

1//! Utility modules for CLI operations
2
3pub mod cloud;
4pub mod creation_options;
5pub mod inspector;
6pub mod parallel;
7pub mod profiler;
8pub mod progress;
9pub mod raster;
10pub mod vector;
11
12pub use inspector::{InspectionReport, RasterSummary, VectorSummary, inspect_file};
13
14use std::path::Path;
15
16/// Detect file format from extension
17pub fn detect_format(path: &Path) -> Option<&'static str> {
18    path.extension()
19        .and_then(|ext| ext.to_str())
20        .and_then(|ext| match ext.to_lowercase().as_str() {
21            "tif" | "tiff" => Some("GeoTIFF"),
22            "json" | "geojson" => Some("GeoJSON"),
23            "shp" => Some("Shapefile"),
24            "fgb" => Some("FlatGeobuf"),
25            "parquet" | "geoparquet" => Some("GeoParquet"),
26            "zarr" => Some("Zarr"),
27            "gpkg" => Some("GeoPackage"),
28            "jp2" => Some("JPEG2000"),
29            "copc" => Some("COPC"),
30            "pmtiles" => Some("PMTiles"),
31            "mbtiles" => Some("MBTiles"),
32            _ => None,
33        })
34}
35
36/// Format file size for human-readable output
37pub fn format_size(bytes: u64) -> String {
38    const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
39    let mut size = bytes as f64;
40    let mut unit_index = 0;
41
42    while size >= 1024.0 && unit_index < UNITS.len() - 1 {
43        size /= 1024.0;
44        unit_index += 1;
45    }
46
47    if unit_index == 0 {
48        format!("{} {}", bytes, UNITS[unit_index])
49    } else {
50        format!("{:.2} {}", size, UNITS[unit_index])
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_detect_format() {
60        assert_eq!(detect_format(Path::new("test.tif")), Some("GeoTIFF"));
61        assert_eq!(detect_format(Path::new("test.tiff")), Some("GeoTIFF"));
62        assert_eq!(detect_format(Path::new("test.geojson")), Some("GeoJSON"));
63        assert_eq!(detect_format(Path::new("test.json")), Some("GeoJSON"));
64        assert_eq!(detect_format(Path::new("test.shp")), Some("Shapefile"));
65        assert_eq!(detect_format(Path::new("test.fgb")), Some("FlatGeobuf"));
66        assert_eq!(detect_format(Path::new("test.parquet")), Some("GeoParquet"));
67        assert_eq!(detect_format(Path::new("test.zarr")), Some("Zarr"));
68        assert_eq!(detect_format(Path::new("test.gpkg")), Some("GeoPackage"));
69        assert_eq!(detect_format(Path::new("test.jp2")), Some("JPEG2000"));
70        assert_eq!(detect_format(Path::new("test.copc")), Some("COPC"));
71        assert_eq!(detect_format(Path::new("test.pmtiles")), Some("PMTiles"));
72        assert_eq!(detect_format(Path::new("test.mbtiles")), Some("MBTiles"));
73        assert_eq!(detect_format(Path::new("test.unknown")), None);
74    }
75
76    #[test]
77    fn test_format_size() {
78        assert_eq!(format_size(512), "512 B");
79        assert_eq!(format_size(1024), "1.00 KB");
80        assert_eq!(format_size(1536), "1.50 KB");
81        assert_eq!(format_size(1_048_576), "1.00 MB");
82    }
83}