|
| 1 | +use crate::config::{fetch_text, AppError, FetchMirrors, LogFormatter}; |
| 2 | +use crate::mirror::Mirror; |
| 3 | +use crate::target_configs::arch4edu::Arch4eduTarget; |
| 4 | +use std::fmt::Display; |
| 5 | +use std::sync::mpsc; |
| 6 | +use url::Url; |
| 7 | + |
| 8 | +const ARCH4EDU_MIRRORLIST_URL: &str = |
| 9 | + "https://raw.githubusercontent.com/arch4edu/mirrorlist/refs/heads/master/mirrorlist.arch4edu"; |
| 10 | + |
| 11 | +fn parse_mirror_url(line: &str) -> Option<Url> { |
| 12 | + let mut cleaned = line.trim_start(); |
| 13 | + while cleaned.starts_with('#') { |
| 14 | + cleaned = cleaned.trim_start_matches('#').trim_start(); |
| 15 | + } |
| 16 | + |
| 17 | + let raw_url = cleaned |
| 18 | + .strip_prefix("Global Server = ") |
| 19 | + .or_else(|| cleaned.strip_prefix("Server = "))? |
| 20 | + .trim() |
| 21 | + .replace("$arch", ""); |
| 22 | + |
| 23 | + if raw_url.is_empty() { |
| 24 | + return None; |
| 25 | + } |
| 26 | + |
| 27 | + Url::parse(&raw_url).ok() |
| 28 | +} |
| 29 | + |
| 30 | +impl LogFormatter for Arch4eduTarget { |
| 31 | + fn format_comment(&self, message: impl Display) -> String { |
| 32 | + format!("{}{}", self.comment_prefix, message) |
| 33 | + } |
| 34 | + |
| 35 | + fn format_mirror(&self, mirror: &Mirror) -> String { |
| 36 | + let arch = if self.arch == "auto" { |
| 37 | + "$arch" |
| 38 | + } else { |
| 39 | + &self.arch |
| 40 | + }; |
| 41 | + |
| 42 | + format!("Server = {}{}", mirror.url, arch) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl FetchMirrors for Arch4eduTarget { |
| 47 | + fn fetch_mirrors(&self, _tx_progress: mpsc::Sender<String>) -> Result<Vec<Mirror>, AppError> { |
| 48 | + let output = fetch_text(ARCH4EDU_MIRRORLIST_URL, self.fetch_mirrors_timeout)?; |
| 49 | + |
| 50 | + let mirrors = output |
| 51 | + .lines() |
| 52 | + .filter_map(parse_mirror_url) |
| 53 | + .map(|url| { |
| 54 | + let url_to_test = url |
| 55 | + .join(&self.path_to_test) |
| 56 | + .expect("failed to join path_to_test"); |
| 57 | + Mirror { |
| 58 | + country: None, |
| 59 | + url, |
| 60 | + url_to_test, |
| 61 | + } |
| 62 | + }) |
| 63 | + .collect(); |
| 64 | + |
| 65 | + Ok(mirrors) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[cfg(test)] |
| 70 | +mod tests { |
| 71 | + use super::parse_mirror_url; |
| 72 | + use crate::config::LogFormatter; |
| 73 | + use crate::mirror::Mirror; |
| 74 | + use crate::target_configs::arch4edu::Arch4eduTarget; |
| 75 | + use url::Url; |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn parse_server_line() { |
| 79 | + let url = parse_mirror_url("#Server = https://mirror.example/arch4edu/$arch").unwrap(); |
| 80 | + assert_eq!(url.as_str(), "https://mirror.example/arch4edu/"); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn parse_global_server_line() { |
| 85 | + let url = |
| 86 | + parse_mirror_url("## Global Server = https://mirror.example/arch4edu/$arch").unwrap(); |
| 87 | + assert_eq!(url.as_str(), "https://mirror.example/arch4edu/"); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn ignore_non_server_lines() { |
| 92 | + assert!(parse_mirror_url("# random comment").is_none()); |
| 93 | + assert!(parse_mirror_url("Include = /etc/pacman.d/mirrorlist").is_none()); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn format_mirror_uses_arch_placeholder_for_auto() { |
| 98 | + let target = Arch4eduTarget { |
| 99 | + fetch_mirrors_timeout: 15_000, |
| 100 | + path_to_test: "arch4edu/x86_64/arch4edu.files".to_string(), |
| 101 | + arch: "auto".to_string(), |
| 102 | + comment_prefix: "# ".to_string(), |
| 103 | + }; |
| 104 | + let mirror = Mirror { |
| 105 | + country: None, |
| 106 | + url: Url::parse("https://mirror.example/arch4edu/").unwrap(), |
| 107 | + url_to_test: Url::parse("https://mirror.example/arch4edu/x86_64/arch4edu.files") |
| 108 | + .unwrap(), |
| 109 | + }; |
| 110 | + |
| 111 | + assert_eq!( |
| 112 | + target.format_mirror(&mirror), |
| 113 | + "Server = https://mirror.example/arch4edu/$arch" |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn format_mirror_uses_custom_arch() { |
| 119 | + let target = Arch4eduTarget { |
| 120 | + fetch_mirrors_timeout: 15_000, |
| 121 | + path_to_test: "arch4edu/x86_64/arch4edu.files".to_string(), |
| 122 | + arch: "x86_64".to_string(), |
| 123 | + comment_prefix: "# ".to_string(), |
| 124 | + }; |
| 125 | + let mirror = Mirror { |
| 126 | + country: None, |
| 127 | + url: Url::parse("https://mirror.example/arch4edu/").unwrap(), |
| 128 | + url_to_test: Url::parse("https://mirror.example/arch4edu/x86_64/arch4edu.files") |
| 129 | + .unwrap(), |
| 130 | + }; |
| 131 | + |
| 132 | + assert_eq!( |
| 133 | + target.format_mirror(&mirror), |
| 134 | + "Server = https://mirror.example/arch4edu/x86_64" |
| 135 | + ); |
| 136 | + } |
| 137 | +} |
0 commit comments