-
Notifications
You must be signed in to change notification settings - Fork 12
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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[..])))] | ||
pub input_format: Option<Format>, | ||
|
||
/// key=value pairs to use for the input object store | ||
|
@@ -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")] | ||
|
@@ -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 { | ||
|
@@ -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(); | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you could just make BROTLI go to |
||
} | ||
|
||
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), | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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 toFormat::from_str