Skip to content

Commit

Permalink
feat: add proj and raster extensions
Browse files Browse the repository at this point in the history
Includes two new traits:

- `Fields` for getting and setting fields, including prefixed ones
- `Extension` for defining extension schema urls and prefixes
  • Loading branch information
gadomski committed Apr 10, 2024
1 parent 260d3f4 commit 066f7db
Show file tree
Hide file tree
Showing 11 changed files with 721 additions and 37 deletions.
23 changes: 23 additions & 0 deletions stac/src/asset.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::{Extensions, Fields};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::collections::HashMap;
Expand Down Expand Up @@ -54,6 +55,9 @@ pub struct Asset {
/// Additional fields on the asset.
#[serde(flatten)]
pub additional_fields: Map<String, Value>,

#[serde(skip)]
extensions: Vec<String>,
}

/// Trait implemented by anything that has assets.
Expand Down Expand Up @@ -107,10 +111,29 @@ impl Asset {
created: None,
updated: None,
additional_fields: Map::new(),
extensions: Vec::new(),
}
}
}

impl Fields for Asset {
fn fields(&self) -> &Map<String, Value> {
&self.additional_fields
}
fn fields_mut(&mut self) -> &mut Map<String, Value> {
&mut self.additional_fields
}
}

impl Extensions for Asset {
fn extensions(&self) -> &Vec<String> {
&self.extensions
}
fn extensions_mut(&mut self) -> &mut Vec<String> {
&mut self.extensions
}
}

#[cfg(test)]
mod tests {
use super::Asset;
Expand Down
27 changes: 20 additions & 7 deletions stac/src/catalog.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Error, Extensions, Href, Link, Links, Result, STAC_VERSION};
use crate::{Error, Extensions, Fields, Href, Link, Links, Result, STAC_VERSION};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

Expand Down Expand Up @@ -33,8 +33,9 @@ pub struct Catalog {

/// A list of extension identifiers the `Catalog` implements.
#[serde(rename = "stac_extensions")]
#[serde(skip_serializing_if = "Option::is_none")]
pub extensions: Option<Vec<String>>,
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub extensions: Vec<String>,

/// Identifier for the `Catalog`.
pub id: String,
Expand Down Expand Up @@ -74,7 +75,7 @@ impl Catalog {
Catalog {
r#type: CATALOG_TYPE.to_string(),
version: STAC_VERSION.to_string(),
extensions: None,
extensions: Vec::new(),
id: id.to_string(),
title: None,
description: description.to_string(),
Expand Down Expand Up @@ -122,9 +123,21 @@ impl TryFrom<Map<String, Value>> for Catalog {
}
}

impl Fields for Catalog {
fn fields(&self) -> &Map<String, Value> {
&self.additional_fields
}
fn fields_mut(&mut self) -> &mut Map<String, Value> {
&mut self.additional_fields
}
}

impl Extensions for Catalog {
fn extensions(&self) -> Option<&[String]> {
self.extensions.as_deref()
fn extensions(&self) -> &Vec<String> {
&self.extensions
}
fn extensions_mut(&mut self) -> &mut Vec<String> {
&mut self.extensions
}
}

Expand Down Expand Up @@ -154,7 +167,7 @@ mod tests {
assert_eq!(catalog.description, "a description");
assert_eq!(catalog.r#type, "Catalog");
assert_eq!(catalog.version, STAC_VERSION);
assert!(catalog.extensions.is_none());
assert!(catalog.extensions.is_empty());
assert_eq!(catalog.id, "an-id");
assert!(catalog.links.is_empty());
}
Expand Down
27 changes: 20 additions & 7 deletions stac/src/collection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Asset, Assets, Error, Extensions, Href, Link, Links, Result, STAC_VERSION};
use crate::{Asset, Assets, Error, Extensions, Fields, Href, Link, Links, Result, STAC_VERSION};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::collections::HashMap;
Expand Down Expand Up @@ -36,8 +36,9 @@ pub struct Collection {

/// A list of extension identifiers the `Collection` implements.
#[serde(rename = "stac_extensions")]
#[serde(skip_serializing_if = "Option::is_none")]
pub extensions: Option<Vec<String>>,
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub extensions: Vec<String>,

/// Identifier for the `Collection` that is unique across the provider.
pub id: String,
Expand Down Expand Up @@ -171,7 +172,7 @@ impl Collection {
Collection {
r#type: COLLECTION_TYPE.to_string(),
version: STAC_VERSION.to_string(),
extensions: None,
extensions: Vec::new(),
id: id.to_string(),
title: None,
description: description.to_string(),
Expand Down Expand Up @@ -253,9 +254,21 @@ impl Assets for Collection {
}
}

impl Fields for Collection {
fn fields(&self) -> &Map<String, Value> {
&self.additional_fields
}
fn fields_mut(&mut self) -> &mut Map<String, Value> {
&mut self.additional_fields
}
}

impl Extensions for Collection {
fn extensions(&self) -> Option<&[String]> {
self.extensions.as_deref()
fn extensions(&self) -> &Vec<String> {
&self.extensions
}
fn extensions_mut(&mut self) -> &mut Vec<String> {
&mut self.extensions
}
}

Expand Down Expand Up @@ -311,7 +324,7 @@ mod tests {
assert!(collection.assets.is_empty());
assert_eq!(collection.r#type, "Collection");
assert_eq!(collection.version, STAC_VERSION);
assert!(collection.extensions.is_none());
assert!(collection.extensions.is_empty());
assert_eq!(collection.id, "an-id");
assert!(collection.links.is_empty());
}
Expand Down
4 changes: 4 additions & 0 deletions stac/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ pub enum Error {
#[error("value is not a collection")]
NotACollection(Value),

/// This value is not an object.
#[error("not an object")]
NotAnObject(serde_json::Value),

/// Returned when trying to read from a url but the `reqwest` feature is not enabled.
#[error("reqwest is not enabled")]
ReqwestNotEnabled,
Expand Down
13 changes: 0 additions & 13 deletions stac/src/extensions.rs

This file was deleted.

Loading

0 comments on commit 066f7db

Please sign in to comment.