2023-08-22 14:29:02 +02:00

58 lines
2.1 KiB
Rust

// /// Remove the leading http(s) part of a URI.
// /// ```
// /// # use yunohost_api::SSOWatConfig;
// /// let stripped = SSOWatConfig::strip_url_protocol("http://theanarchistlibrary.org/");
// /// # assert_eq!("theanarchistlibrary.org/", stripped)
// /// ```
// /// Result: theanarchistlibrary.org/
// /// ```
// /// # use yunohost_api::SSOWatConfig;
// /// let stripped = SSOWatConfig::strip_url_protocol("misformedhttpdomain/");
// /// # assert_eq!("misformedhttpdomain/", stripped);
// /// ```
// /// Result: misformedhttpdomain/
// pub fn strip_url_protocol<'a>(uri: &'a str) -> &'a str {
// let s = uri.strip_prefix("http").unwrap_or(&uri);
// let s = s.strip_prefix("s").unwrap_or(&s);
// let s = s.strip_prefix("://").unwrap_or(&s);
// s
// // uri
// // .strip_prefix("http")
// // .and_then(|s| s.strip_prefix(""))
// // .trim_start_matches("http")
// // .trim_start_matches("s")
// // .trim_start_matches("://")
// }
/// Extracts the domain part of a http(s) URI.
/// ```
/// # use yunohost_api::SSOWatConfig;
/// let domain = SSOWatConfig::extract_domain("https://mediaslibres.org/spip.php?page=sedna-rss");
/// assert_eq!("mediaslibres.org", domain);
/// ```
/// Result: mediaslibres.org
/// ```
/// # use yunohost_api::SSOWatConfig;
/// let domain = SSOWatConfig::extract_domain("http://foo.bar.example.com");
/// assert_eq!("foo.bar.example.com", domain);
/// ```
/// Result: foo.bar.example.com
/// ```
/// # use yunohost_api::SSOWatConfig;
/// let domain = SSOWatConfig::extract_domain("http://foo.bar.example.com/bar/baz");
/// assert_eq!("foo.bar.example.com", domain);
/// ```
/// Result: foo.bar.example.com
/// ```
/// # use yunohost_api::SSOWatConfig;
/// let domain = SSOWatConfig::extract_domain("misformedhttpdomain");
/// # assert_eq!("misformedhttpdomain", domain)
/// ```
/// Result: misformedhttpdomain
pub fn extract_domain<'a>(uri: &'a str) -> &'a str {
let mut split = Self::strip_url_protocol(uri).split('/');
// Even if nothing was matched, there should always be one split part
// If there was an additional slash, we just extracted the domain
split.next().unwrap()
}