use anyhow::Result as AnyResult;
use clap::{Parser, ValueEnum};
use h3o::{CellIndex, Resolution};
use serde::Serialize;
#[derive(Parser, Debug)]
#[command(author, version)]
pub struct Args {
#[arg(short, long)]
child: Option<CellIndex>,
#[arg(short, long, default_value_t = Resolution::Zero)]
resolution: Resolution,
#[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 = crate::utils::get_cell_indexes(args.child)
.map(|child| child.map(|child| (child, child.parent(args.resolution))));
match args.format {
Format::Text => {
for result in indexes {
let (child, parent) = result?;
println!("{}", parent.unwrap_or(child));
}
}
Format::Json => {
#[derive(Serialize)]
struct ChildParent {
child: crate::json::CellIndex,
parent: Option<crate::json::CellIndex>,
}
let indexes = indexes
.map(|result| {
result.map(|(child, parent)| ChildParent {
child: child.into(),
parent: parent.map(Into::into),
})
})
.collect::<AnyResult<Vec<_>>>()?;
crate::json::print(&indexes, args.pretty)?;
}
}
Ok(())
}