Skip to content

Commit 4fb24fb

Browse files
authored
refactor: add set_self_href and clear_self_href (#746)
1 parent b3337f6 commit 4fb24fb

File tree

8 files changed

+18
-7
lines changed

8 files changed

+18
-7
lines changed

crates/api/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl Client {
209209
let mut value = self
210210
.request::<(), V>(Method::GET, url.clone(), None, None)
211211
.await?;
212-
*value.self_href_mut() = Some(url.into());
212+
value.set_self_href(url);
213213
Ok(value)
214214
}
215215

crates/core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1414
- More permissive datetime interval parsing ([#715](https://github.com/stac-utils/rustac/pull/715))
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))
17+
- `SelfHref::set_self_href` and `SelfHref::clear_self_href` ([#746](https://github.com/stac-utils/rustac/pull/746))
1718

1819
### Changed
1920

crates/core/src/geoparquet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ mod tests {
278278
#[test]
279279
fn roundtrip() {
280280
let mut item: Item = crate::read("examples/simple-item.json").unwrap();
281-
*item.self_href_mut() = None;
281+
item.clear_self_href();
282282
let mut cursor = Cursor::new(Vec::new());
283283
super::into_writer(&mut cursor, vec![item.clone()]).unwrap();
284284
let bytes = Bytes::from(cursor.into_inner());

crates/core/src/href.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ pub trait SelfHref {
6262
/// *item.self_href_mut() = Option::Some("./a/relative/path.json".into());
6363
/// ```
6464
fn self_href_mut(&mut self) -> &mut Option<Href>;
65+
66+
/// Sets this object's self href.
67+
fn set_self_href(&mut self, href: impl Into<Href>) {
68+
*self.self_href_mut() = Some(href.into())
69+
}
70+
71+
/// Clear's this object's self href.
72+
fn clear_self_href(&mut self) {
73+
*self.self_href_mut() = None
74+
}
6575
}
6676

6777
impl Href {

crates/core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ where
173173
let path = path.as_ref();
174174
let file = File::open(path)?;
175175
let mut value: T = serde_json::from_reader(file)?;
176-
*value.self_href_mut() = Some(path.to_string_lossy().into_owned().into());
176+
value.set_self_href(path.to_string_lossy().into_owned());
177177
Ok(value)
178178
}
179179

crates/io/src/format.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl Format {
7171
value
7272
}
7373
};
74-
*value.self_href_mut() = Some(href);
74+
value.set_self_href(href);
7575
Ok(value)
7676
}
7777

@@ -164,7 +164,7 @@ impl Format {
164164
href,
165165
message: err.to_string(),
166166
})?;
167-
*value.self_href_mut() = Some(Href::Url(url));
167+
value.set_self_href(url);
168168
Ok(value)
169169
}
170170
RealizedHref::PathBuf(path) => {

crates/io/src/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub trait FromJsonPath: FromJson + SelfHref {
2020
let mut buf = Vec::new();
2121
let _ = File::open(path)?.read_to_end(&mut buf)?;
2222
let mut value = Self::from_json_slice(&buf)?;
23-
*value.self_href_mut() = Some(path.into());
23+
value.set_self_href(path);
2424
Ok(value)
2525
}
2626
}

crates/io/src/ndjson.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl FromNdjsonPath for ItemCollection {
5555
items.push(serde_json::from_str(&line?)?);
5656
}
5757
let mut item_collection = ItemCollection::from(items);
58-
*item_collection.self_href_mut() = Some(path.into());
58+
item_collection.set_self_href(path);
5959
Ok(item_collection)
6060
}
6161
}

0 commit comments

Comments
 (0)