Skip to main content

tp_lib_core/detections/
load.rs

1//! Detection loading & format dispatch (T011).
2//!
3//! Public entry point: [`load_detections`].
4
5use std::path::Path;
6
7use crate::io::csv::detections as csv_loader;
8use crate::io::geojson::detections as geojson_loader;
9use crate::models::{Detection, DetectionKind};
10
11use super::error::DetectionError;
12
13/// Load detections from a file. Format is inferred from the extension:
14/// `.csv` → CSV, `.geojson` / `.json` → GeoJSON. Anything else returns
15/// [`DetectionError::UnsupportedExtension`].
16///
17/// `expected_kind` corresponds to the CLI flag (`--punctual-detections` or
18/// `--linear-detections`). The parser validates that the file content matches
19/// the expected kind.
20pub fn load_detections(
21    path: &Path,
22    expected_kind: DetectionKind,
23) -> Result<Vec<Detection>, DetectionError> {
24    let ext = path
25        .extension()
26        .and_then(|s| s.to_str())
27        .map(|s| s.to_ascii_lowercase())
28        .unwrap_or_default();
29
30    match ext.as_str() {
31        "csv" => csv_loader::load(path, expected_kind),
32        "geojson" | "json" => geojson_loader::load(path, expected_kind),
33        _ => Err(DetectionError::UnsupportedExtension(ext)),
34    }
35}