Skip to content

Commit

Permalink
feat(cli): improve handling for invalid STAC
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Oct 9, 2024
1 parent 146d572 commit 275c54e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
22 changes: 16 additions & 6 deletions cli/src/args/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@ pub(crate) struct Args {

impl Run for Args {
async fn run(self, input: Input, _: Option<Sender<Value>>) -> Result<Option<Value>> {
let value = input.get().await?;
let value = input.get_json().await?;
let result = value.validate().await;
if let Err(stac::Error::Validation(ref errors)) = result {
let message_base = match value {
stac::Value::Item(item) => format!("[item={}] ", item.id),
stac::Value::Catalog(catalog) => format!("[catalog={}] ", catalog.id),
stac::Value::Collection(collection) => format!("[collection={}] ", collection.id),
stac::Value::ItemCollection(_) => "[item-collection] ".to_string(),
let id = value
.get("id")
.and_then(|v| v.as_str())
.map(|v| format!("={}", v))
.unwrap_or_default();
let message_base = match value
.get("type")
.and_then(|v| v.as_str())
.unwrap_or_default()
{
"Feature" => format!("[item={}] ", id),
"Catalog" => format!("[catalog={}] ", id),
"Collection" => format!("[collection={}] ", id),
"FeatureCollection" => "[item-collection] ".to_string(),
_ => String::new(),
};
for error in errors {
eprintln!(
Expand Down
4 changes: 4 additions & 0 deletions cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ pub enum Error {
/// Unsupported format.
#[error("unsupported (or unknown) format: {0}")]
UnsupportedFormat(String),

/// [url::ParseError]
#[error(transparent)]
UrlParse(#[from] url::ParseError),
}

impl Error {
Expand Down
32 changes: 32 additions & 0 deletions cli/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,36 @@ impl Input {
.map_err(Error::from)
}
}

/// Gets a serde_json value from the input.
pub(crate) async fn get_json(&self) -> Result<serde_json::Value> {
if let Some(href) = self.href.as_deref() {
let (object_store, path) =
object_store::parse_url_opts(&href.parse()?, self.options.iter())?;
let get_result = object_store.get(&path).await?;
let bytes = get_result.bytes().await?;
let format = self
.format
.or_else(|| Format::infer_from_href(href))
.unwrap_or_default();
match format {
Format::Json(..) => serde_json::from_slice(&bytes).map_err(Error::from),
_ => {
let value: Value = format.from_bytes(bytes)?;
serde_json::to_value(value).map_err(Error::from)
}
}
} else {
let mut buf = Vec::new();
let _ = std::io::stdin().read_to_end(&mut buf);
let format = self.format.unwrap_or_default();
match format {
Format::Json(..) => serde_json::from_slice(&buf).map_err(Error::from),
_ => {
let value: Value = format.from_bytes(buf)?;
serde_json::to_value(value).map_err(Error::from)
}
}
}
}
}
2 changes: 1 addition & 1 deletion core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub enum Error {
NoHref,

/// There is no type.
#[error("no type")]
#[error("no type field")]
NoType,

/// No version field on an object.
Expand Down

0 comments on commit 275c54e

Please sign in to comment.