| 1 | use urlrust-analyzer cargo url 2.5.8 crate/::Urlrust-analyzer cargo url 2.5.8 Url#; |
| 2 | |
| 3 | /// Strip authentication information from a URL and return it separately |
| 4 | pub fn strip_auth_from_urlrust-analyzer cargo apt-sources 0.3.0 utils/strip_auth_from_url().(url: &Urlrust-analyzer cargo url 2.5.8 Url#) -> (Urlrust-analyzer cargo url 2.5.8 Url#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#)>) { |
| 5 | let auth = if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().url.usernamerust-analyzer cargo url 2.5.8 impl#[Url]username().().is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 6 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(( |
| 7 | url.usernamerust-analyzer cargo url 2.5.8 impl#[Url]username().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 8 | url.passwordrust-analyzer cargo url 2.5.8 impl#[Url]password().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().("").to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 9 | )) |
| 10 | } else { |
| 11 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 12 | }; |
| 13 | |
| 14 | let mut clean_url = url.clonerust-analyzer cargo url 2.5.8 impl#[Url][Clone]clone().(); |
| 15 | if auth.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 16 | clean_url.set_usernamerust-analyzer cargo url 2.5.8 impl#[Url]set_username().("").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(); |
| 17 | clean_url.set_passwordrust-analyzer cargo url 2.5.8 impl#[Url]set_password().(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(); |
| 18 | } |
| 19 | |
| 20 | (clean_url, auth) |
| 21 | } |
| 22 | |
| 23 | /// Generate a sanitized filename from a URL |
| 24 | pub fn generate_filename_from_urlrust-analyzer cargo apt-sources 0.3.0 utils/generate_filename_from_url().(url: &Urlrust-analyzer cargo url 2.5.8 Url#, extension: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 25 | let host = url.host_strrust-analyzer cargo url 2.5.8 impl#[Url]host_str().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().("unknown"); |
| 26 | let path = url.pathrust-analyzer cargo url 2.5.8 impl#[Url]path().(); |
| 27 | |
| 28 | // Remove leading/trailing slashes and replace internal slashes |
| 29 | let path_part = path |
| 30 | .trim_start_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start_matches().('/') |
| 31 | .trim_end_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end_matches().('/') |
| 32 | .replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('/', "-"); |
| 33 | |
| 34 | let base = if path_part.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() { |
| 35 | host.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 36 | } else { |
| 37 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}-{}", host, path_part) |
| 38 | }; |
| 39 | |
| 40 | // Sanitize the filename |
| 41 | let sanitized = base.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().(':', "-").replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().(' ', "-").to_lowercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_lowercase().(); |
| 42 | |
| 43 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}.{}", sanitized, extension) |
| 44 | } |
| 45 | |
| 46 | #[cfg(test)] |
| 47 | mod testsrust-analyzer cargo apt-sources 0.3.0 utils/tests/ { |
| 48 | use superrust-analyzer cargo apt-sources 0.3.0 utils/::*; |
| 49 | |
| 50 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 51 | fn test_strip_auth_from_urlrust-analyzer cargo apt-sources 0.3.0 utils/tests/test_strip_auth_from_url().() { |
| 52 | let url = Urlrust-analyzer cargo url 2.5.8 Url#::parserust-analyzer cargo url 2.5.8 impl#[Url]parse().("https://user:[email protected]/path").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 53 | let (clean_url, auth) = strip_auth_from_urlrust-analyzer cargo apt-sources 0.3.0 utils/strip_auth_from_url().(&url); |
| 54 | |
| 55 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(clean_url.as_strrust-analyzer cargo url 2.5.8 impl#[Url]as_str().(), "https://example.com/path"); |
| 56 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(auth, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(("user".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "pass".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))); |
| 57 | |
| 58 | // URL without auth |
| 59 | let url = Urlrust-analyzer cargo url 2.5.8 Url#::parserust-analyzer cargo url 2.5.8 impl#[Url]parse().("https://example.com/path").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 60 | let (clean_url, auth) = strip_auth_from_urlrust-analyzer cargo apt-sources 0.3.0 utils/strip_auth_from_url().(&url); |
| 61 | |
| 62 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(clean_url.as_strrust-analyzer cargo url 2.5.8 impl#[Url]as_str().(), "https://example.com/path"); |
| 63 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(auth, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 64 | } |
| 65 | |
| 66 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 67 | fn test_generate_filename_from_urlrust-analyzer cargo apt-sources 0.3.0 utils/tests/test_generate_filename_from_url().() { |
| 68 | let url = Urlrust-analyzer cargo url 2.5.8 Url#::parserust-analyzer cargo url 2.5.8 impl#[Url]parse().("https://example.com/ubuntu").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 69 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 70 | generate_filename_from_urlrust-analyzer cargo apt-sources 0.3.0 utils/generate_filename_from_url().(&url, "sources"), |
| 71 | "example.com-ubuntu.sources" |
| 72 | ); |
| 73 | |
| 74 | let url = Urlrust-analyzer cargo url 2.5.8 Url#::parserust-analyzer cargo url 2.5.8 impl#[Url]parse().("https://ppa.launchpad.net/user/repo/ubuntu").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 75 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 76 | generate_filename_from_urlrust-analyzer cargo apt-sources 0.3.0 utils/generate_filename_from_url().(&url, "list"), |
| 77 | "ppa.launchpad.net-user-repo-ubuntu.list" |
| 78 | ); |
| 79 | } |
| 80 | } |