Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CLI list possible values for format options. #549

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Added

- List available i/o formats in help ([#549](https://github.com/stac-utils/stac-rs/pull/549))

## [0.4.1] - 2024-10-22

### Changed
Expand Down
81 changes: 76 additions & 5 deletions crates/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ use crate::{
subcommand::{search, serve, translate, validate},
Error, Result, Value,
};
use clap::Parser;
use stac::Format;
use clap::{
builder::{PossibleValue, PossibleValuesParser, TypedValueParser},
Parser, ValueEnum,
};
use stac::{geoparquet::Compression, Format};
use std::{convert::Infallible, io::Write, str::FromStr};
use tokio::io::AsyncReadExt;
use tracing::metadata::Level;
Expand All @@ -17,7 +20,7 @@ use tracing::metadata::Level;
#[command(author, version, about, long_about = None)]
pub struct Args {
/// The input format, if not provided will be inferred from the input file's extension, falling back to json
#[arg(short, long, global = true)]
#[arg(short, long, global = true, value_parser=PossibleValuesParser::new(["json", "json-pretty", "ndjson", "geoparquet", "geoparquet[snappy]"]).try_map(|s| Format::from_str(&s[..])))]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one way, list all possible options as strings, use Format and pass them on to Format::from_str

pub input_format: Option<Format>,

/// key=value pairs to use for the input object store
Expand All @@ -26,7 +29,7 @@ pub struct Args {

/// The output format, if not provided will be inferred from the output file's extension, falling back to json
#[arg(short, long, global = true)]
pub output_format: Option<Format>,
pub output_format: Option<FormatWrapper>,

/// key=value pairs to use for the output object store
#[arg(long = "output-option")]
Expand Down Expand Up @@ -117,8 +120,9 @@ impl Args {
if href.as_deref() == Some("-") {
href = None;
}
let format = self
let format: Format = self
.input_format
.map(Into::into)
.or(href.as_deref().and_then(Format::infer_from_href))
.unwrap_or_default();
if let Some(href) = href {
Expand All @@ -143,6 +147,7 @@ impl Args {
}
let format = self
.output_format
.map(Into::into)
.or(href.and_then(Format::infer_from_href))
.unwrap_or_default();
let value = value.into();
Expand Down Expand Up @@ -366,3 +371,69 @@ pub struct Load {
pub collections: usize,
pub items: usize,
}

#[derive(Debug, Copy, Clone)]
pub enum FormatWrapper {
Json(bool),
Ndjson,
Geoparquet(Option<Compression>),
}

impl ValueEnum for FormatWrapper {
fn value_variants<'a>() -> &'a [Self] {
&[
FormatWrapper::Json(true),
FormatWrapper::Json(false),
FormatWrapper::Ndjson,
FormatWrapper::Geoparquet(None),
FormatWrapper::Geoparquet(Some(Compression::LZ4)),
FormatWrapper::Geoparquet(Some(Compression::SNAPPY)),
// Do not know how to deal with options below:
// FormatWrapper::Geoparquet(Some(Compression::BROTLI(???))),
// FormatWrapper::Geoparquet(Some(Compression::from_str("BROTLI").unwrap())),
]
Comment on lines +383 to +394
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another way. Also includes listing all possible options with additional complications - I have no idea how to construct FormatWrapper::Geoparquet(Compression::BROTLI(BrotliLevel)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could just make BROTLI go to "geoparquet[brotli(n)]", as the levels are all u32: https://docs.rs/parquet/latest/src/parquet/basic.rs.html#410-424

}

fn to_possible_value(&self) -> Option<PossibleValue> {
match self {
FormatWrapper::Json(false) => {
Some(PossibleValue::new("json").alias("geojson").help("GeoJson"))
}
FormatWrapper::Json(true) => Some(
PossibleValue::new("json-pretty")
.alias("geojson-pretty")
.help("Pretty-printed GeoJson"),
),
FormatWrapper::Ndjson => Some(PossibleValue::new("ndjson")),
FormatWrapper::Geoparquet(None) => Some(PossibleValue::new("geoparquet")),
FormatWrapper::Geoparquet(Some(Compression::SNAPPY)) => {
Some(PossibleValue::new("geoparquet[snappy]"))
}
FormatWrapper::Geoparquet(Some(Compression::LZ4)) => {
Some(PossibleValue::new("geoparquet[lz4]"))
}

_ => None,
}
}
}

impl From<Format> for FormatWrapper {
fn from(format: Format) -> Self {
match format {
Format::Json(pretty) => Self::Json(pretty),
Format::NdJson => Self::Ndjson,
Format::Geoparquet(comp) => Self::Geoparquet(comp),
}
}
}

impl From<FormatWrapper> for Format {
fn from(wrapper: FormatWrapper) -> Self {
match wrapper {
FormatWrapper::Json(pretty) => Self::Json(pretty),
FormatWrapper::Ndjson => Format::ndjson(),
FormatWrapper::Geoparquet(comp) => Self::Geoparquet(comp),
}
}
}
Loading