use anyhow::{ensure, Context, Result as AnyResult};
use clap::{ArgGroup, Parser, ValueEnum};
use either::Either;
use h3o::CellIndex;
#[derive(Parser, Debug)]
#[command(group(ArgGroup::new("ll")
.args(["source", "destination"])
.multiple(true)
.requires_all(["source", "destination"]))
)]
pub struct Args {
#[arg(short, long)]
source: Option<CellIndex>,
#[arg(short, long)]
destination: Option<CellIndex>,
#[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(src), Some(dst)) = (args.source, args.destination) {
vec![src, dst]
} else {
crate::io::read_cell_indexes().collect::<AnyResult<Vec<_>>>()?
};
ensure!(indexes.len() >= 2, "not enough cell indexes");
let mut path = indexes
.windows(2)
.flat_map(|segment| match segment[0].grid_path_cells(segment[1]) {
Ok(iter) => Either::Right(iter),
Err(err) => Either::Left(std::iter::once(Err(err))),
})
.collect::<Result<Vec<_>, _>>()
.context("compute paths")?;
path.dedup();
match args.format {
Format::Text => {
for index in path {
println!("{index}");
}
}
Format::Json => {
let path = path
.into_iter()
.map(Into::into)
.collect::<Vec<crate::json::CellIndex>>();
crate::json::print(&path, args.pretty)?;
}
}
Ok(())
}