1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! Expose [`h3o_zip::decompress`]

use anyhow::{Context, Result as AnyResult};
use clap::{Parser, ValueEnum};
use std::io::{self, Read};

/// Decompress and print the cell indexes from the compressed input on stdin.
#[derive(Parser, Debug)]
pub struct Args {
    /// Output format.
    #[arg(short, long, value_enum, default_value_t = Format::Text)]
    format: Format,

    /// Prettify the output (JSON only).
    #[arg(short, long, default_value_t = false)]
    pretty: bool,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
enum Format {
    Text,
    Json,
}

/// Run the `cellToPolygon` command.
pub fn run(args: &Args) -> AnyResult<()> {
    let mut bytes = Vec::new();
    io::stdin()
        .read_to_end(&mut bytes)
        .context("read bytes from stdin")?;
    let indexes = h3o_zip::decompress(bytes.as_slice());

    match args.format {
        Format::Text => {
            for index in indexes {
                println!("{}", index.context("decompress")?);
            }
        }
        Format::Json => {
            let indexes = indexes
                .map(|index| index.map(Into::into))
                .collect::<Result<Vec<crate::json::CellIndex>, _>>()
                .context("decompress")?;
            crate::json::print(&indexes, args.pretty)?;
        }
    }

    Ok(())
}