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)]
ancestor: 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.ancestor).map(|parent| {
parent.map(|parent| (parent, parent.children(args.resolution)))
});
match args.format {
Format::Text => {
for result in indexes {
let (_, children) = result?;
for child in children {
println!("{child}");
}
}
}
Format::Json => {
#[derive(Serialize)]
struct ParentChildren {
parent: crate::json::CellIndex,
children: Option<Vec<crate::json::CellIndex>>,
}
let indexes = indexes
.map(|result| {
result.map(|(parent, children)| {
let children =
children.map(Into::into).collect::<Vec<_>>();
ParentChildren {
parent: parent.into(),
children: (!children.is_empty())
.then_some(children),
}
})
})
.collect::<AnyResult<Vec<_>>>()?;
crate::json::print(&indexes, args.pretty)?;
}
}
Ok(())
}