Skip to content

Commit f067a72

Browse files
authored
feat: use href for asset href (#752)
1 parent 1b58c8f commit f067a72

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

crates/core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2121
- Default to snappy compression for geoparquet ([#673](https://github.com/stac-utils/rustac/pull/673))
2222
- Ensure geoparquet->json provides valid datetime strings (UTC) ([#711](https://github.com/stac-utils/rustac/pull/711)])
2323
- Some `Href` method names ([#747](https://github.com/stac-utils/rustac/pull/747))
24+
- Use `Href` for `Asset.href` 🙈 ([#752](https://github.com/stac-utils/rustac/pull/752))
2425

2526
### Fixed
2627

crates/core/src/asset.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Band, DataType, Statistics};
1+
use crate::{Band, DataType, Href, Statistics};
22
use indexmap::IndexMap;
33
use serde::{Deserialize, Serialize};
44
use serde_json::{Map, Value};
@@ -10,7 +10,7 @@ pub struct Asset {
1010
/// URI to the asset object.
1111
///
1212
/// Relative and absolute URIs are both allowed.
13-
pub href: String,
13+
pub href: Href,
1414

1515
/// The displayed title for clients and users.
1616
#[serde(skip_serializing_if = "Option::is_none")]
@@ -124,9 +124,9 @@ impl Asset {
124124
/// let asset = Asset::new("an-href");
125125
/// assert_eq!(asset.href, "an-href");
126126
/// ```
127-
pub fn new(href: impl ToString) -> Asset {
127+
pub fn new(href: impl Into<Href>) -> Asset {
128128
Asset {
129-
href: href.to_string(),
129+
href: href.into(),
130130
title: None,
131131
description: None,
132132
r#type: None,

crates/core/src/item.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use serde::{Deserialize, Deserializer, Serialize};
88
use serde_json::{Map, Value};
99
use stac_derive::{Links, Migrate, SelfHref};
1010
use std::path::Path;
11-
use url::Url;
1211

1312
const TOP_LEVEL_ATTRIBUTES: [&str; 8] = [
1413
"type",
@@ -311,11 +310,11 @@ impl Builder {
311310
pub fn build(self) -> Result<Item> {
312311
let mut item = Item::new(self.id);
313312
for (key, mut asset) in self.assets {
314-
if Url::parse(&asset.href).is_err() && self.canonicalize_paths {
315-
asset.href = Path::new(&asset.href)
316-
.canonicalize()?
317-
.to_string_lossy()
318-
.into_owned();
313+
if let Href::String(ref s) = asset.href {
314+
if self.canonicalize_paths {
315+
asset.href =
316+
Href::String(Path::new(s).canonicalize()?.to_string_lossy().into_owned());
317+
}
319318
}
320319
let _ = item.assets.insert(key, asset);
321320
}
@@ -841,6 +840,7 @@ mod tests {
841840
assert!(
842841
asset
843842
.href
843+
.to_string()
844844
.ends_with(&format!("assets{}dataset.tif", std::path::MAIN_SEPARATOR))
845845
);
846846
}

0 commit comments

Comments
 (0)