Skip to main content

stac_io/
realized_href.rs

1use std::path::PathBuf;
2use url::Url;
3
4/// An href that has been realized to a path or a url.
5#[derive(Debug)]
6pub enum RealizedHref {
7    /// A path buf
8    PathBuf(PathBuf),
9
10    /// A url
11    Url(Url),
12}
13
14impl From<&str> for RealizedHref {
15    fn from(s: &str) -> RealizedHref {
16        if stac::href::is_windows_absolute_path(s) {
17            return RealizedHref::PathBuf(PathBuf::from(s));
18        }
19        if let Ok(url) = Url::parse(s) {
20            if url.scheme() == "file" {
21                url.to_file_path()
22                    .map(RealizedHref::PathBuf)
23                    .unwrap_or_else(|_| RealizedHref::Url(url))
24            } else {
25                RealizedHref::Url(url)
26            }
27        } else {
28            RealizedHref::PathBuf(PathBuf::from(s))
29        }
30    }
31}