Skip to content

Commit 47f12b3

Browse files
authored
feat: add make asset hrefs absolute (#753)
1 parent f067a72 commit 47f12b3

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

crates/core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1515
- `Format` methods for providing your own object store ([#730](https://github.com/stac-utils/rustac/pull/730))
1616
- `type` field to **stac-geoparquet** writes ([#736](https://github.com/stac-utils/rustac/pull/736))
1717
- `SelfHref::set_self_href` and `SelfHref::clear_self_href` ([#746](https://github.com/stac-utils/rustac/pull/746))
18+
- `Assets::make_assets_absolute` ([#753](https://github.com/stac-utils/rustac/pull/753))
1819

1920
### Changed
2021

crates/core/src/asset.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Band, DataType, Href, Statistics};
1+
use crate::{Band, DataType, Href, Result, Statistics};
22
use indexmap::IndexMap;
33
use serde::{Deserialize, Serialize};
44
use serde_json::{Map, Value};
@@ -112,6 +112,14 @@ pub trait Assets {
112112
/// item.assets_mut().insert("foo".to_string(), Asset::new("./asset.tif"));
113113
/// ```
114114
fn assets_mut(&mut self) -> &mut IndexMap<String, Asset>;
115+
116+
/// Makes all asset hrefs absolute.
117+
fn make_assets_absolute(&mut self, base: impl AsRef<Href>) -> Result<()> {
118+
for asset in self.assets_mut().values_mut() {
119+
asset.href = asset.href.into_absolute(&base)?;
120+
}
121+
Ok(())
122+
}
115123
}
116124

117125
impl Asset {
@@ -174,7 +182,8 @@ impl<'a> From<&'a str> for Asset {
174182

175183
#[cfg(test)]
176184
mod tests {
177-
use super::Asset;
185+
use super::{Asset, Assets};
186+
use crate::{Href, Item};
178187

179188
#[test]
180189
fn new() {
@@ -195,4 +204,14 @@ mod tests {
195204
assert!(value.get("type").is_none());
196205
assert!(value.get("roles").is_none());
197206
}
207+
208+
#[test]
209+
fn make_absolute() {
210+
let asset = Asset::new("an-href");
211+
let mut item = Item::new("an-item");
212+
let _ = item.assets.insert("data".into(), asset);
213+
item.make_assets_absolute(Href::from("http://rustac.test"))
214+
.unwrap();
215+
assert_eq!(item.assets["data"].href, "http://rustac.test/an-href");
216+
}
198217
}

0 commit comments

Comments
 (0)