Skip to content

Commit 7b5ec99

Browse files
committed
feat: allow bare &str for args to Href::new
Requires allowing `single_use_lifetimes` due to rust-lang/rust#60554
1 parent 370323a commit 7b5ec99

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/href.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ impl Href {
2525
/// use stac::Href;
2626
/// let href = Href::new("data", None).unwrap();
2727
/// assert!(href.to_str().starts_with("/"));
28-
/// assert_eq!(Href::new("a/path", Some("http://example.com")).unwrap().to_str(), "http://example.com/a/path");
28+
/// assert_eq!(
29+
/// Href::new("a/path", "http://example.com").unwrap().to_str(),
30+
/// "http://example.com/a/path"
31+
/// );
2932
/// ```
30-
pub fn new(href: &str, base: Option<&str>) -> Result<Href, Error> {
33+
#[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/60554
34+
pub fn new<'a, T: Into<Option<&'a str>>>(href: &str, base: T) -> Result<Href, Error> {
35+
let base = base.into();
3136
if let Ok(url) = Url::parse(href) {
3237
return Ok(Href::Url(url));
3338
}
@@ -62,7 +67,9 @@ impl Href {
6267
/// ```
6368
/// use stac::Href;
6469
/// let href = Href::new("data/catalog.json", None).unwrap();
65-
/// println!("{}", href.to_str());
70+
/// assert!(href.as_path().is_some());
71+
/// let href = Href::new("./catalog.json", "http://example.com/stac").unwrap();
72+
/// assert!(href.as_path().is_none());
6673
/// ```
6774
pub fn to_str(&self) -> &str {
6875
match self {
@@ -80,7 +87,7 @@ mod tests {
8087
fn catalog_base() {
8188
let href = Href::new(
8289
"./extensions-collection/collection.json",
83-
Some("data/catalog.json"),
90+
"data/catalog.json",
8491
)
8592
.unwrap();
8693
assert!(href

src/reader.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ pub trait Read {
1616
/// let reader = Reader::default();
1717
/// let catalog = reader.read("data/catalog.json", None).unwrap();
1818
/// ```
19-
fn read(&self, href: &str, base: Option<&str>) -> Result<Object, Error> {
19+
#[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/60554
20+
fn read<'a, T: Into<Option<&'a str>>>(&self, href: &str, base: T) -> Result<Object, Error> {
2021
let href = Href::new(href, base)?;
2122
self.read_href(href)
2223
}

0 commit comments

Comments
 (0)