geolith-cli 0.2.2

Rust vector tile generator — OSM + Overture Maps → protomaps PMTiles
Documentation
#![allow(clippy::unwrap_used)] // tests and benches may unwrap (AGENTS.md).

//! Command-line configuration parsing.

use clap::Parser;
use geolith_cli::config::Config;

const BASE_ARGS: [&str; 11] = [
    "geolith",
    "--data",
    "/tmp/data",
    "--osm-pbf",
    "/tmp/planet.osm.pbf",
    "--natural-earth",
    "/tmp/ne.sqlite",
    "--water-polygons",
    "/tmp/water",
    "--land-polygons",
    "/tmp/land",
];

fn parse_with(extra: &[&str]) -> Config {
    let mut args: Vec<&str> = BASE_ARGS.to_vec();
    args.extend_from_slice(extra);
    Config::parse_from(args)
}

#[test]
fn config_default_max_zoom() {
    let config = parse_with(&["--output", "/tmp/out.pmtiles"]);
    assert_eq!(config.max_zoom, 15, "Default max_zoom should be 15");
}

#[test]
fn config_custom_max_zoom() {
    let config = parse_with(&["--output", "/tmp/out.pmtiles", "--max-zoom", "10"]);
    assert_eq!(config.max_zoom, 10);
}

#[test]
fn config_bbox_parsing() {
    let config = parse_with(&["--output", "/tmp/out.pmtiles", "--bbox", "1.0,2.0,3.0,4.0"]);
    assert_eq!(config.bbox, Some("1.0,2.0,3.0,4.0".to_string()));
}

#[test]
fn config_no_bbox() {
    let config = parse_with(&["--output", "/tmp/out.pmtiles"]);
    assert!(config.bbox.is_none());
}

#[test]
fn config_reclaim_source_after_read_defaults_off() {
    let config = parse_with(&["--output", "/tmp/out.pmtiles"]);
    assert!(
        !config.reclaim_source_after_read,
        "source files must not be reclaimed unless explicitly requested"
    );
}

#[test]
fn config_reclaim_source_after_read_opt_in() {
    let config = parse_with(&[
        "--output",
        "/tmp/out.pmtiles",
        "--reclaim-source-after-read",
    ]);
    assert!(config.reclaim_source_after_read);
}

#[test]
fn config_all_flags() {
    let config = Config::parse_from([
        "geolith",
        "--data",
        "/tmp/overture",
        "--osm-pbf",
        "/tmp/planet.osm.pbf",
        "--natural-earth",
        "/tmp/ne.sqlite",
        "--water-polygons",
        "/tmp/water",
        "--land-polygons",
        "/tmp/land",
        "--output",
        "/tmp/planet.pmtiles",
        "--bbox",
        "-122.5,37.7,-122.3,37.8",
        "--max-zoom",
        "12",
        "--threads",
        "4",
        "--tmpdir",
        "/tmp/chunks",
        "--india-boundary",
        "/tmp/india.geojson",
        "--buildings-source",
        "osm",
        "--places-source",
        "overture",
        "--roads-source",
        "osm",
    ]);
    assert_eq!(
        config
            .data
            .as_deref()
            .and_then(|p| p.to_str())
            .expect("data path"),
        "/tmp/overture"
    );
    assert_eq!(
        config
            .output
            .as_deref()
            .and_then(|p| p.to_str())
            .expect("output path"),
        "/tmp/planet.pmtiles"
    );
    assert_eq!(config.bbox, Some("-122.5,37.7,-122.3,37.8".to_string()));
    assert_eq!(config.max_zoom, 12);
    assert_eq!(config.threads, Some(4));
    assert_eq!(
        config
            .tmpdir
            .as_ref()
            .expect("tmpdir")
            .to_str()
            .expect("tmpdir str"),
        "/tmp/chunks"
    );
    assert!(config.has_india_boundary());
    assert_eq!(config.buildings_source, "osm");
    assert_eq!(config.places_source, "overture");
    assert_eq!(config.roads_source, "osm");
}

#[test]
fn config_short_output_flag() {
    let config = parse_with(&["-o", "/tmp/short.pmtiles"]);
    assert_eq!(
        config
            .output
            .as_deref()
            .and_then(|p| p.to_str())
            .expect("output path"),
        "/tmp/short.pmtiles"
    );
}

#[test]
fn config_source_helpers() {
    let config = parse_with(&["--output", "/tmp/out.pmtiles"]);
    assert!(config.uses_osm(&config.buildings_source));
    assert!(config.uses_overture(&config.buildings_source));
    assert!(config.uses_overture(&config.divisions_source));
    assert!(!config.uses_osm(&config.divisions_source));
}

#[test]
fn config_layer_source_filtering() {
    use geolith_cli::layers::Layer;

    let config = Config::parse_from([
        "geolith",
        "--data",
        "/tmp/data",
        "--osm-pbf",
        "/tmp/planet.osm.pbf",
        "--natural-earth",
        "/tmp/ne.sqlite",
        "--water-polygons",
        "/tmp/water",
        "--land-polygons",
        "/tmp/land",
        "--output",
        "/tmp/out.pmtiles",
        "--buildings-source",
        "osm",
        "--roads-source",
        "overture",
        "--places-source",
        "overture",
        "--landcover-source",
        "none",
    ]);

    assert!(!config.overture_enabled(Layer::Buildings));
    assert!(config.osm_enabled(Layer::Buildings));

    assert!(config.overture_enabled(Layer::Roads));
    assert!(!config.osm_enabled(Layer::Roads));

    assert!(config.overture_enabled(Layer::Pois));
    assert!(config.osm_enabled(Layer::Pois)); // default pois_source="both"

    assert!(config.overture_enabled(Layer::Landuse));
    assert!(config.osm_enabled(Layer::Landuse)); // default landuse_source="both"

    assert!(!config.overture_enabled(Layer::Landcover));

    assert!(config.overture_enabled(Layer::Earth));
    assert!(config.osm_enabled(Layer::Earth));
    assert!(config.overture_enabled(Layer::Water));
    assert!(config.osm_enabled(Layer::Water)); // default water_source="both"
    assert!(config.osm_enabled(Layer::Places));
}

/// The usage error for missing tile flags, as the argument parser wrote it
/// when the flags were unconditionally required (captured before they
/// became format-dependent).
fn missing_message(flags: &[&str]) -> String {
    let mut s = String::from("error: the following required arguments were not provided:\n");
    for f in flags {
        s.push_str("  ");
        s.push_str(f);
        s.push('\n');
    }
    s.push_str(
        "\nUsage: geolith --data <DATA> --natural-earth <NATURAL_EARTH> \
         --water-polygons <WATER_POLYGONS> --land-polygons <LAND_POLYGONS> --output <OUTPUT>\n",
    );
    s
}

#[test]
fn tiles_reports_each_missing_path_flag() {
    let all = [
        ("--data", "--data <DATA>"),
        ("--natural-earth", "--natural-earth <NATURAL_EARTH>"),
        ("--water-polygons", "--water-polygons <WATER_POLYGONS>"),
        ("--land-polygons", "--land-polygons <LAND_POLYGONS>"),
        ("--output", "--output <OUTPUT>"),
    ];
    for (skip, usage) in all {
        let mut argv = vec!["geolith"];
        for (flag, _) in all.iter().filter(|(f, _)| *f != skip) {
            argv.extend([*flag, "/x"]);
        }
        let err = geolith_cli::cli::parse_from(argv).expect_err("a tiles flag is missing");
        assert_eq!(err.kind(), clap::error::ErrorKind::MissingRequiredArgument);
        let text = err.render().to_string();
        assert!(
            text.starts_with(&missing_message(&[usage])),
            "missing {skip}:\n{text}"
        );
    }
    let err = geolith_cli::cli::parse_from(["geolith"]).expect_err("every tiles flag is missing");
    let usages: Vec<&str> = all.iter().map(|(_, u)| *u).collect();
    assert!(
        err.render()
            .to_string()
            .starts_with(&missing_message(&usages))
    );
}

#[test]
fn routing_needs_only_the_pbf_and_the_output() {
    let command = geolith_cli::cli::parse_from([
        "geolith",
        "--output-format",
        "routing",
        "--osm-pbf",
        "/tmp/berlin.osm.pbf",
        "--routing-output",
        "/tmp/berlin.glx",
    ])
    .expect("routing reads no tile sources");
    let geolith_cli::cli::Command::Tiles(config) = command else {
        panic!("the default command parses the build config");
    };
    assert_eq!(config.data, None);
    assert_eq!(config.output, None);
    assert!(config.check_required().is_ok());
}

#[test]
fn geocoding_needs_data_and_an_output_path() {
    let err = geolith_cli::cli::parse_from(["geolith", "--output-format", "geocoding"])
        .expect_err("geocoding reads --data");
    assert!(
        err.render()
            .to_string()
            .contains("  --data <DATA>\n  --output <OUTPUT>\n")
    );
    let ok = geolith_cli::cli::parse_from([
        "geolith",
        "--output-format",
        "geocoding",
        "--data",
        "/tmp/overture",
        "--geocoding-output",
        "/tmp/places.glx",
    ]);
    assert!(ok.is_ok());
}

/// The two work-reuse switches live in `Config::reuse`, and the CLI still
/// spells them `--node-cache` and `--resume` with the same defaults.
#[test]
fn reuse_flags_keep_their_names_and_defaults() {
    let config = parse_with(&["--output", "/tmp/out.pmtiles"]);
    assert!(config.reuse.node_cache, "node caching is on by default");
    assert!(!config.reuse.resume, "resume is opt-in");

    let config = parse_with(&["--output", "/tmp/out.pmtiles", "--resume"]);
    assert!(config.reuse.resume);
    assert!(config.reuse.node_cache);
}