h3o_cli/commands/
cell_to_parent.rs1use anyhow::Result as AnyResult;
4use clap::{Parser, ValueEnum};
5use h3o::{CellIndex, Resolution};
6use serde::Serialize;
7
8#[derive(Parser, Debug)]
14#[command(author, version)]
15pub struct Args {
16 #[arg(short, long)]
18 child: Option<CellIndex>,
19
20 #[arg(short, long, default_value_t = Resolution::Zero)]
22 resolution: Resolution,
23
24 #[arg(short, long, value_enum, default_value_t = Format::Text)]
26 format: Format,
27
28 #[arg(short, long, default_value_t = false)]
30 pretty: bool,
31}
32
33#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
34enum Format {
35 Text,
36 Json,
37}
38
39pub fn run(args: &Args) -> AnyResult<()> {
41 let indexes = crate::utils::get_cell_indexes(args.child)
42 .map(|child| child.map(|child| (child, child.parent(args.resolution))));
43
44 match args.format {
45 Format::Text => {
46 for result in indexes {
47 let (child, parent) = result?;
48 println!("{}", parent.unwrap_or(child));
49 }
50 }
51 Format::Json => {
52 #[derive(Serialize)]
53 struct ChildParent {
54 child: crate::json::CellIndex,
55 parent: Option<crate::json::CellIndex>,
56 }
57
58 let indexes = indexes
59 .map(|result| {
60 result.map(|(child, parent)| ChildParent {
61 child: child.into(),
62 parent: parent.map(Into::into),
63 })
64 })
65 .collect::<AnyResult<Vec<_>>>()?;
66
67 crate::json::print(&indexes, args.pretty)?;
68 }
69 }
70
71 Ok(())
72}