use url::Url;
pub trait Urlify {
fn url(&self) -> &Url;
fn relative_base_and_url(&self) -> Option<(&Url, String)> {
None
}
fn relative_url(&self) -> Option<String> {
self.relative_base_and_url().map(|(_, url)| url)
}
fn relative_base(&self) -> Option<&Url> {
self.relative_base_and_url().map(|(url, _)| url)
}
fn possibly_relative_url(&self) -> String {
self.relative_url()
.unwrap_or_else(|| self.url().to_string())
}
}
impl<T, E> Urlify for Result<T, E>
where
T: Urlify,
E: Urlify,
{
fn url(&self) -> &Url {
match self {
Ok(something) => something.url(),
Err(something) => something.url(),
}
}
}
pub fn ensure_slash(mut url: Url) -> Url {
if !url.path().ends_with("/") {
let new_path = format!("{}/", url.path());
url.set_path(&new_path);
}
url
}