Skip to content
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
3 changes: 3 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository.workspace = true
rust-version.workspace = true

[features]
stream = ["dep:futures", "dep:stream-kmerge"]
std = []
geo = ["dep:geo"]
geoarrow = [
Expand Down Expand Up @@ -51,6 +52,8 @@ serde_json = { workspace = true, features = ["preserve_order"] }
serde_urlencoded.workspace = true
stac-derive = { version = "0.3.0", path = "../derive" }
thiserror.workspace = true
stream-kmerge = { version = "0.2.0", optional = true }
futures = { workspace = true, optional = true }
tracing.workspace = true
url = { workspace = true, features = ["serde"] }
wkb = { workspace = true, optional = true }
Expand Down
28 changes: 28 additions & 0 deletions crates/core/src/item_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,34 @@ pub struct ItemCollection {
self_href: Option<String>,
}

impl ItemCollection {
/// Sorts the items in this collection.
///
/// If a `config` is provided, it must be a valid JSON representation of a SortConfig.
/// If `None`, the default [`crate::sort::ItemComparator`] is used.
///
/// # Examples
///
/// ```
/// use stac::{Item, ItemCollection};
/// use serde_json::json;
///
/// let mut ic = ItemCollection::from(vec![Item::new("b"), Item::new("a")]);
/// ic.sort(Some(json!({
/// "sortby": [
/// { "field": "id", "direction": "asc" }
/// ]
/// }))).unwrap();
/// assert_eq!(ic.items[0].id, "a");
/// ```
pub fn sort(&mut self, config: Option<Value>) -> Result<()> {
use crate::sort::Sortable;
let items = std::mem::take(&mut self.items);
self.items = items.sort(config)?;
Ok(())
}
}

impl From<Vec<Item>> for ItemCollection {
fn from(items: Vec<Item>) -> Self {
ItemCollection {
Expand Down
3 changes: 3 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ pub mod link;
mod migrate;
pub mod mime;
mod ndjson;
/// Sort STAC items.
pub mod sort;
mod statistics;
mod value;
mod version;
Expand All @@ -149,6 +151,7 @@ pub use json::{FromJson, ToJson};
pub use link::{Link, Links};
pub use migrate::Migrate;
pub use ndjson::{FromNdjson, ToNdjson};
pub use sort::Sortable;
pub use statistics::Statistics;
pub use value::Value;
pub use version::Version;
Expand Down
Loading
Loading