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

Improve handling for invalid STAC #448

Merged
merged 2 commits into from
Oct 9, 2024
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bb8-postgres = "0.8.1"
bytes = "1.7"
chrono = "0.4.38"
clap = "4.5"
duckdb = "1.0"
duckdb = "=1.0"
fluent-uri = "0.3.1"
futures = "0.3.31"
gdal = "0.17.1"
Expand Down
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
Loading