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

Add Href.clear_href #299

Merged
merged 1 commit into from
Aug 26, 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
1 change: 1 addition & 0 deletions stac/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed

- Use `DateTime<Utc>` 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

Expand Down
4 changes: 4 additions & 0 deletions stac/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions stac/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions stac/src/href.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,49 @@ 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();
/// assert!(item.href().is_some());
/// ```
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`.
Expand Down
4 changes: 4 additions & 0 deletions stac/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions stac/src/item_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions stac/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down