spin_common/
url.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//! Operations on URLs

use anyhow::{anyhow, Context};

use std::path::PathBuf;

/// Parse the path from a 'file:' URL
pub fn parse_file_url(url: &str) -> anyhow::Result<PathBuf> {
    url::Url::parse(url)
        .with_context(|| format!("Invalid URL: {url:?}"))?
        .to_file_path()
        .map_err(|_| anyhow!("Invalid file URL path: {url:?}"))
}

/// Remove the credentials from a URL string
pub fn remove_credentials(url: &str) -> anyhow::Result<String> {
    let mut url = url::Url::parse(url).with_context(|| format!("Invalid URL: {url:?}"))?;
    url.set_username("")
        .map_err(|_| anyhow!("Could not remove username"))?;
    url.set_password(None)
        .map_err(|_| anyhow!("Could not remove password"))?;
    Ok(url.to_string())
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn remove_credentials_removes_credentials() {
        assert_eq!(
            "redis://example.com:4567",
            remove_credentials("redis://example.com:4567").unwrap()
        );
        assert_eq!(
            "redis://example.com:4567",
            remove_credentials("redis://me:secret@example.com:4567").unwrap()
        );
        assert_eq!(
            "http://example.com/users",
            remove_credentials("http://me:secret@example.com/users").unwrap()
        );
    }
}