humanize_url/
lib.rs

1pub use unrestrictive_url::ParseError;
2use unrestrictive_url::{UnrestrictiveUrl, Url};
3
4pub fn humanize_url(url: &str) -> Result<String, ParseError> {
5    let url = Url::parse(url)?;
6    let mut url = UnrestrictiveUrl::from(&url);
7
8    // Remove protocol.
9    url.scheme = None;
10    // Remove authentication.
11    url.username = None;
12    url.password = None;
13
14    // Remove trailing slashes.
15    let url = url.to_string();
16    let mut chars = url.chars();
17    if chars.next_back() == Some('/') {
18        Ok(chars.collect())
19    } else {
20        Ok(url)
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::humanize_url;
27
28    #[test]
29    fn removes_scheme() {
30        let url = humanize_url("https://github.com/SirWindfield").unwrap();
31        assert_eq!("github.com/SirWindfield", url);
32    }
33
34    #[test]
35    fn removes_trailing_slash() {
36        let url = humanize_url("https://github.com/SirWindfield/").unwrap();
37        assert_eq!("github.com/SirWindfield", url);
38    }
39
40    #[test]
41    fn removes_authentication() {
42        let url = humanize_url("https://user:pw@github.com/SirWindfield").unwrap();
43        assert_eq!("github.com/SirWindfield", url);
44    }
45}