Skip to content

Commit 589b18a

Browse files
authored
fix: href parsing (#762)
1 parent 104961b commit 589b18a

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

crates/io/src/store.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,36 +92,23 @@ impl StacStore {
9292

9393
/// Gets a STAC value from the store.
9494
///
95-
/// The format will be inferred from the path's file extension.
96-
///
97-
/// # Examples
98-
///
99-
/// ```
100-
/// use object_store::local::LocalFileSystem;
101-
/// use stac_io::StacStore;
102-
///
103-
/// let store = LocalFileSystem::new_with_prefix(std::env::current_dir().unwrap()).unwrap();
104-
/// let stac_store = StacStore::from(store);
105-
/// # tokio_test::block_on(async {
106-
/// let item: stac::Item = stac_store.get("examples/simple-item.json").await.unwrap();
107-
/// });
108-
/// ```
109-
pub async fn get<T>(&self, path: impl Into<Path>) -> Result<T>
95+
/// The format will be inferred from the href's file extension.
96+
pub async fn get<T>(&self, href: impl ToString + AsRef<str> + Debug) -> Result<T>
11097
where
11198
T: Readable,
11299
{
113-
let path = path.into();
114-
let format = Format::infer_from_href(path.as_ref()).unwrap_or_default();
115-
self.get_format(path, format).await
100+
let format = Format::infer_from_href(href.as_ref()).unwrap_or_default();
101+
self.get_format(href, format).await
116102
}
117103

118104
/// Gets a STAC value from the store in a specific format.
119105
#[instrument(skip(self))]
120-
pub async fn get_format<T>(&self, path: impl Into<Path> + Debug, format: Format) -> Result<T>
106+
pub async fn get_format<T>(&self, href: impl ToString + Debug, format: Format) -> Result<T>
121107
where
122108
T: Readable,
123109
{
124-
let path = path.into();
110+
let href = href.to_string();
111+
let path = self.path(&href)?;
125112
let get_result = self.store.get(&path).await?;
126113
let bytes = get_result.bytes().await?;
127114
let mut value: T = format.from_bytes(bytes)?;
@@ -130,31 +117,41 @@ impl StacStore {
130117
}
131118

132119
/// Puts a STAC value to the store.
133-
pub async fn put<T>(&self, path: impl Into<Path>, value: T) -> Result<PutResult>
120+
pub async fn put<T>(&self, href: impl AsRef<str> + Debug, value: T) -> Result<PutResult>
134121
where
135122
T: Writeable + Debug,
136123
{
137-
let path = path.into();
138-
let format = Format::infer_from_href(path.as_ref()).unwrap_or_default();
139-
self.put_format(path, value, format).await
124+
let format = Format::infer_from_href(href.as_ref()).unwrap_or_default();
125+
self.put_format(href, value, format).await
140126
}
141127

142128
/// Puts a STAC value to the store in a specific format.
143129
#[instrument(skip(self))]
144130
pub async fn put_format<T>(
145131
&self,
146-
path: impl Into<Path> + Debug,
132+
href: impl AsRef<str> + Debug,
147133
value: T,
148134
format: Format,
149135
) -> Result<PutResult>
150136
where
151137
T: Writeable + Debug,
152138
{
153-
let path = path.into();
139+
let path = self.path(href.as_ref())?;
154140
let bytes = format.into_vec(value)?;
155141
let put_result = self.store.put(&path, bytes.into()).await?;
156142
Ok(put_result)
157143
}
144+
145+
fn path(&self, href: &str) -> Result<Path> {
146+
let result = if let Ok(url) = Url::parse(href) {
147+
// TODO check to see if the host and such match? or not?
148+
Path::from_url_path(url.path())
149+
} else {
150+
Path::parse(href)
151+
};
152+
let path = result.map_err(object_store::Error::from)?;
153+
Ok(path)
154+
}
158155
}
159156

160157
#[cfg(test)]
@@ -170,4 +167,11 @@ mod tests {
170167
assert!(self_href.starts_with("file:///"));
171168
assert!(self_href.ends_with("examples/simple-item.json"));
172169
}
170+
171+
#[tokio::test]
172+
async fn get_local_href() {
173+
let (store, path) = super::parse_href("examples/simple-item.json").unwrap();
174+
let href = format!("file:///{}", path);
175+
let _: Item = store.get(href).await.unwrap();
176+
}
173177
}

0 commit comments

Comments
 (0)