From 94ebb30609c8b2eb70dd0b101f05d290408772fa Mon Sep 17 00:00:00 2001 From: monosans Date: Fri, 11 Apr 2025 17:05:21 +0300 Subject: [PATCH] minor fixes & refactoring --- src/checker.rs | 11 +++++------ src/proxy.rs | 2 +- src/scraper.rs | 8 ++++---- src/storage.rs | 9 +++++---- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/checker.rs b/src/checker.rs index 3cbbb9c..50ffb7f 100644 --- a/src/checker.rs +++ b/src/checker.rs @@ -28,15 +28,14 @@ async fn check_one( } Err(e) => { if log::log_enabled!(log::Level::Debug) { - let mut s = proxy.as_str(true); - s.push_str(" | "); - s.push_str( - &e.chain() + log::debug!( + "{} | {}", + proxy.as_str(true), + e.chain() .map(ToString::to_string) .collect::>() - .join(" → "), + .join(" → ") ); - log::debug!("{s}"); } Err(e) } diff --git a/src/proxy.rs b/src/proxy.rs index b5a2ae9..0a6f23f 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -24,7 +24,7 @@ impl TryFrom<&str> for ProxyType { fn try_from(string: &str) -> color_eyre::Result { match string { - "http" | "https " => Ok(ProxyType::Http), + "http" | "https" => Ok(ProxyType::Http), "socks4" => Ok(ProxyType::Socks4), "socks5" => Ok(ProxyType::Socks5), _ => Err(eyre!("Failed to convert {string} to ProxyType")), diff --git a/src/scraper.rs b/src/scraper.rs index 67c1034..2149cc9 100644 --- a/src/scraper.rs +++ b/src/scraper.rs @@ -16,7 +16,7 @@ async fn fetch_text( http_client: reqwest::Client, source: &str, ) -> color_eyre::Result { - Ok(if is_http_url(source) { + if is_http_url(source) { http_client .get(source) .timeout(config.source_timeout) @@ -33,14 +33,14 @@ async fn fetch_text( .await .wrap_err_with(move || { format!("failed to decode {source} response as text") - })? + }) } else { tokio::fs::read_to_string( source.strip_prefix("file://").unwrap_or(source), ) .await - .wrap_err("failed to read file to string")? - }) + .wrap_err("failed to read file to string") + } } async fn scrape_one( diff --git a/src/storage.rs b/src/storage.rs index 45b9466..c3e81f7 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -19,13 +19,14 @@ impl ProxyStorage { } pub(crate) fn get_grouped(&self) -> HashMap> { - let mut groups: HashMap> = HashMap::new(); + let mut groups: HashMap> = self + .enabled_protocols + .iter() + .map(|p| (p.clone(), Vec::new())) + .collect(); for proxy in &self.proxies { groups.entry(proxy.protocol.clone()).or_default().push(proxy); } - for protocol in &self.enabled_protocols { - groups.entry(protocol.clone()).or_default(); - } groups }