use anyhow::{Context, Result as AnyResult};
use clap::{ArgGroup, Parser, ValueEnum};
use either::Either;
use h3o::{LatLng, Resolution};
#[derive(Parser, Debug)]
#[command(group(ArgGroup::new("ll")
.args(["lat", "lng"])
.multiple(true)
.requires_all(["lat", "lng"]))
)]
pub struct Args {
#[arg(short, long)]
resolution: Resolution,
#[arg(long)]
lat: Option<f64>,
#[arg(long)]
lng: Option<f64>,
#[arg(short, long, value_enum, default_value_t = Format::Text)]
format: Format,
#[arg(short, long, default_value_t = false)]
pretty: bool,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
enum Format {
Text,
Json,
}
pub fn run(args: &Args) -> AnyResult<()> {
let indexes = if let (Some(lat), Some(lng)) = (args.lat, args.lng) {
Either::Left(std::iter::once(
LatLng::new(lat, lng).context("invalid lat/lng"),
))
} else {
Either::Right(crate::io::read_coords())
}
.map(|input| input.map(|ll| ll.to_cell(args.resolution)));
match args.format {
Format::Text => {
for index in indexes {
println!("{}", index?);
}
}
Format::Json => {
let indexes = indexes
.map(|result| result.map(Into::into))
.collect::<AnyResult<Vec<crate::json::CellIndex>>>(
)?;
crate::json::print(&indexes, args.pretty)?;
}
}
Ok(())
}