h3o_cli/commands/
compact.rs

1//! Expose [`CellIndex::compact`]
2
3use anyhow::{Context, Result as AnyResult};
4use clap::{Parser, ValueEnum};
5use h3o::CellIndex;
6
7/// Compact the given set of indexes (from stdin).
8///
9/// All indexes must have the same resolution.
10#[derive(Parser, Debug)]
11pub struct Args {
12    /// Output format.
13    #[arg(short, long, value_enum, default_value_t = Format::Text)]
14    format: Format,
15
16    /// Prettify the output (JSON only).
17    #[arg(short, long, default_value_t = false)]
18    pretty: bool,
19}
20
21#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
22enum Format {
23    Text,
24    Json,
25}
26
27/// Run the `compact` command.
28pub fn run(args: &Args) -> AnyResult<()> {
29    let indexes =
30        crate::io::read_cell_indexes().collect::<AnyResult<Vec<_>>>()?;
31
32    let compacted = CellIndex::compact(indexes).context("compaction")?;
33    match args.format {
34        Format::Text => {
35            for index in compacted {
36                println!("{index}");
37            }
38        }
39        Format::Json => {
40            let compacted = compacted
41                .into_iter()
42                .map(Into::into)
43                .collect::<Vec<crate::json::CellIndex>>();
44            crate::json::print(&compacted, args.pretty)?;
45        }
46    }
47
48    Ok(())
49}