diff --git a/stac/CHANGELOG.md b/stac/CHANGELOG.md index dd0f0f91..493c143d 100644 --- a/stac/CHANGELOG.md +++ b/stac/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Changed - Use `DateTime` instead of `String` for datetimes ([#297](https://github.com/stac-utils/stac-rs/pull/297)) +- Add `Href.clear_href` ([#299](https://github.com/stac-utils/stac-rs/pull/299)) ## [0.8.0] - 2024-08-12 diff --git a/stac/src/catalog.rs b/stac/src/catalog.rs index 90fdba02..525b04ca 100644 --- a/stac/src/catalog.rs +++ b/stac/src/catalog.rs @@ -93,6 +93,10 @@ impl Href for Catalog { fn set_href(&mut self, href: impl ToString) { self.href = Some(href.to_string()) } + + fn clear_href(&mut self) { + self.href = None; + } } impl Links for Catalog { diff --git a/stac/src/collection.rs b/stac/src/collection.rs index 04524f3d..4b4bc705 100644 --- a/stac/src/collection.rs +++ b/stac/src/collection.rs @@ -195,6 +195,10 @@ impl Href for Collection { fn set_href(&mut self, href: impl ToString) { self.href = Some(href.to_string()) } + + fn clear_href(&mut self) { + self.href = None; + } } impl Links for Collection { diff --git a/stac/src/href.rs b/stac/src/href.rs index d70ed282..207c2c09 100644 --- a/stac/src/href.rs +++ b/stac/src/href.rs @@ -9,6 +9,7 @@ use url::Url; /// /// ``` /// use stac::{Item, Href}; +/// /// let item = Item::new("an-id"); /// assert!(item.href().is_none()); /// let item: Item = stac::read("data/simple-item.json").unwrap(); @@ -16,10 +17,41 @@ use url::Url; /// ``` pub trait Href { /// Gets this object's href. + /// + /// # Examples + /// + /// ``` + /// use stac::{Href, Item}; + /// + /// let item: Item = stac::read("data/simple-item.json").unwrap(); + /// assert_eq!(item.href(), Some("data/simple-item.json")); + /// ``` fn href(&self) -> Option<&str>; /// Sets this object's href. + /// + /// # Examples + /// + /// ``` + /// use stac::{Item, Href}; + /// + /// let mut item = Item::new("an-id"); + /// item.set_href("http://stac.test/item.json"); + /// ``` fn set_href(&mut self, href: impl ToString); + + /// Clears this object's href. + /// + /// # Examples + /// + /// ``` + /// use stac::{Href, Item}; + /// + /// let mut item: Item = stac::read("data/simple-item.json").unwrap(); + /// item.clear_href(); + /// assert!(item.href().is_none()); + /// ``` + fn clear_href(&mut self); } /// Parses an href into a [Url] if the scheme is `http` or `https`. diff --git a/stac/src/item.rs b/stac/src/item.rs index 411c9f89..6504dbf2 100644 --- a/stac/src/item.rs +++ b/stac/src/item.rs @@ -639,6 +639,10 @@ impl Href for Item { fn set_href(&mut self, href: impl ToString) { self.href = Some(href.to_string()) } + + fn clear_href(&mut self) { + self.href = None; + } } impl Links for Item { diff --git a/stac/src/item_collection.rs b/stac/src/item_collection.rs index 0c90c2ce..4d11e837 100644 --- a/stac/src/item_collection.rs +++ b/stac/src/item_collection.rs @@ -63,6 +63,10 @@ impl Href for ItemCollection { fn set_href(&mut self, href: impl ToString) { self.href = Some(href.to_string()) } + + fn clear_href(&mut self) { + self.href = None; + } } impl Links for ItemCollection { diff --git a/stac/src/value.rs b/stac/src/value.rs index cc7dc3a2..72c3c300 100644 --- a/stac/src/value.rs +++ b/stac/src/value.rs @@ -204,6 +204,16 @@ impl Href for Value { ItemCollection(item_collection) => item_collection.set_href(href), } } + + fn clear_href(&mut self) { + use Value::*; + match self { + Catalog(catalog) => catalog.clear_href(), + Collection(collection) => collection.clear_href(), + Item(item) => item.clear_href(), + ItemCollection(item_collection) => item_collection.clear_href(), + } + } } impl Links for Value {