From 0ce2f1a8b42d1deb5ed8278fc3ecd974a25d74cf Mon Sep 17 00:00:00 2001 From: monosans Date: Mon, 5 May 2025 14:47:59 +0300 Subject: [PATCH] add `move` to closures --- src/config.rs | 2 +- src/fs.rs | 2 +- src/geodb.rs | 2 +- src/output.rs | 18 +++++++++--------- src/parsers.rs | 10 ++++++---- src/raw_config.rs | 2 +- src/scraper.rs | 8 ++++++-- src/storage.rs | 2 +- src/ui/tui.rs | 10 ++++++---- src/utils.rs | 4 ++-- 10 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/config.rs b/src/config.rs index 9d8b29f..c504dde 100644 --- a/src/config.rs +++ b/src/config.rs @@ -210,7 +210,7 @@ impl Config { (ProxyType::Socks5, raw_config.socks5), ] .into_iter() - .filter_map(|(proxy_type, section)| { + .filter_map(move |(proxy_type, section)| { section.enabled.then_some((proxy_type, section.sources)) }) .collect(), diff --git a/src/fs.rs b/src/fs.rs index b7591f6..161ccf2 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -8,7 +8,7 @@ pub async fn get_cache_path() -> color_eyre::Result { static CACHE: tokio::sync::OnceCell = tokio::sync::OnceCell::const_new(); Ok(CACHE - .get_or_try_init(async || -> color_eyre::Result { + .get_or_try_init(async move || -> color_eyre::Result { let mut path = tokio::task::spawn_blocking(dirs::cache_dir) .await .wrap_err("failed to spawn task to get user's cache directory")? diff --git a/src/geodb.rs b/src/geodb.rs index eb4c989..dd631d5 100644 --- a/src/geodb.rs +++ b/src/geodb.rs @@ -95,7 +95,7 @@ pub async fn download_geodb( get_geodb_path().await.wrap_err("failed to get GeoDB path")?; let mut headers = reqwest::header::HeaderMap::new(); - if tokio::fs::metadata(&geodb_file).await.is_ok_and(|m| m.is_file()) { + if tokio::fs::metadata(&geodb_file).await.is_ok_and(move |m| m.is_file()) { if let Some(etag) = read_etag().await.wrap_err("failed to read ETag")? { headers.insert(reqwest::header::IF_NONE_MATCH, etag); } diff --git a/src/output.rs b/src/output.rs index 9564b6f..e9ae8f4 100644 --- a/src/output.rs +++ b/src/output.rs @@ -21,7 +21,7 @@ fn sort_by_timeout(proxy: &Proxy) -> tokio::time::Duration { fn sort_naturally(proxy: &Proxy) -> (ProxyType, Vec, u16) { let host_key = proxy.host.parse::().map_or_else( move |_| iter::repeat_n(u8::MAX, 4).chain(proxy.host.bytes()).collect(), - |ip| ip.octets().to_vec(), + move |ip| ip.octets().to_vec(), ); (proxy.protocol.clone(), host_key, proxy.port) } @@ -60,7 +60,7 @@ pub async fn save_proxies( }; let mut sorted_proxies: Vec<_> = storage.iter().collect(); - sorted_proxies.sort_by_key(|p| sort_by_timeout(p)); + sorted_proxies.sort_by_key(move |p| sort_by_timeout(p)); let mut proxy_dicts = Vec::with_capacity(sorted_proxies.len()); @@ -87,9 +87,9 @@ pub async fn save_proxies( password: proxy.password.clone(), host: proxy.host.clone(), port: proxy.port, - timeout: proxy - .timeout - .map(|d| (d.as_secs_f64() * 100.0).round() / 100.0_f64), + timeout: proxy.timeout.map(move |d| { + (d.as_secs_f64() * 100.0).round() / 100.0_f64 + }), exit_ip: proxy.exit_ip.clone(), geolocation, }); @@ -124,9 +124,9 @@ pub async fn save_proxies( if config.output_txt { let mut sorted_proxies: Vec<_> = storage.iter().collect(); if config.sort_by_speed { - sorted_proxies.sort_by_key(|p| sort_by_timeout(p)); + sorted_proxies.sort_by_key(move |p| sort_by_timeout(p)); } else { - sorted_proxies.sort_by_key(|p| sort_naturally(p)); + sorted_proxies.sort_by_key(move |p| sort_naturally(p)); } let mut grouped_proxies = storage.get_grouped(); @@ -166,9 +166,9 @@ pub async fn save_proxies( for (proto, proxies) in &mut grouped_proxies { if config.sort_by_speed { - proxies.sort_by_key(|p| sort_by_timeout(p)); + proxies.sort_by_key(move |p| sort_by_timeout(p)); } else { - proxies.sort_by_key(|p| sort_naturally(p)); + proxies.sort_by_key(move |p| sort_naturally(p)); } let text = create_proxy_list_str(proxies, anonymous_only, false); diff --git a/src/parsers.rs b/src/parsers.rs index f85a30b..6167b02 100644 --- a/src/parsers.rs +++ b/src/parsers.rs @@ -2,16 +2,18 @@ use std::sync::LazyLock; use color_eyre::eyre::{OptionExt as _, WrapErr as _}; -pub static PROXY_REGEX: LazyLock = LazyLock::new(|| { - fancy_regex::RegexBuilder::new( +pub static PROXY_REGEX: LazyLock = LazyLock::new( + move || { + fancy_regex::RegexBuilder::new( r"(?:^|[^\dA-Za-z])(?:(?Phttps?|socks[45]):\/\/)?(?:(?P[^\s:@]+):(?P[^\s:@]+)@)?(?P(?:[\-\.\dA-Za-z]+|(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(?:\.(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3})):(?P\d|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(?=[^\dA-Za-z]|$)", ) .case_insensitive(true) .build() .unwrap() -}); + }, +); -static IPV4_REGEX: LazyLock = LazyLock::new(|| { +static IPV4_REGEX: LazyLock = LazyLock::new(move || { fancy_regex::Regex::new( r"^(?:[0-9:A-Fa-f]+,)?\s*(?P(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(?:\.(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3})(?::(?:\d|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]))?\s*$" ).unwrap() diff --git a/src/raw_config.rs b/src/raw_config.rs index 2116a00..1e27c29 100644 --- a/src/raw_config.rs +++ b/src/raw_config.rs @@ -99,7 +99,7 @@ pub struct ProxySection { const CONFIG_ENV_VAR: &str = "PROXY_SCRAPER_CHECKER_CONFIG"; pub fn get_config_path() -> String { - env::var(CONFIG_ENV_VAR).unwrap_or_else(|_| "config.toml".to_owned()) + env::var(CONFIG_ENV_VAR).unwrap_or_else(move |_| "config.toml".to_owned()) } pub async fn read_config(path: &str) -> color_eyre::Result { diff --git a/src/scraper.rs b/src/scraper.rs index 4fdbd40..e56191b 100644 --- a/src/scraper.rs +++ b/src/scraper.rs @@ -102,8 +102,12 @@ async fn scrape_one( .ok_or_eyre("failed to match \"port\" regex capture group")? .as_str() .parse()?, - username: capture.name("username").map(|m| m.as_str().to_owned()), - password: capture.name("password").map(|m| m.as_str().to_owned()), + username: capture + .name("username") + .map(move |m| m.as_str().to_owned()), + password: capture + .name("password") + .map(move |m| m.as_str().to_owned()), timeout: None, exit_ip: None, }; diff --git a/src/storage.rs b/src/storage.rs index 7b835b8..a5d71bc 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -22,7 +22,7 @@ impl ProxyStorage { let mut groups: HashMap<_, _> = self .enabled_protocols .iter() - .map(|p| (p.clone(), Vec::new())) + .map(move |p| (p.clone(), Vec::new())) .collect(); for proxy in &self.proxies { if let Some(proxies) = groups.get_mut(&proxy.protocol) { diff --git a/src/ui/tui.rs b/src/ui/tui.rs index b70cb3a..ab6c0f7 100644 --- a/src/ui/tui.rs +++ b/src/ui/tui.rs @@ -209,7 +209,9 @@ fn draw(f: &mut Frame, state: &AppState, logger_state: &TuiWidgetState) { let proxies_layout = Layout::default() .direction(Direction::Horizontal) - .constraints(state.sources_total.keys().map(|_| Constraint::Fill(1))) + .constraints( + state.sources_total.keys().map(move |_| Constraint::Fill(1)), + ) .split(outer_layout[2]); let mut proxy_types: Vec<_> = state.sources_total.keys().collect(); @@ -358,7 +360,7 @@ async fn handle_event( state .sources_scraped .entry(proxy_type) - .and_modify(|c| *c = c.saturating_add(1)) + .and_modify(move |c| *c = c.saturating_add(1)) .or_insert(1); } AppEvent::TotalProxies(proxy_type, amount) => { @@ -368,14 +370,14 @@ async fn handle_event( state .proxies_checked .entry(proxy_type) - .and_modify(|c| *c = c.saturating_add(1)) + .and_modify(move |c| *c = c.saturating_add(1)) .or_insert(1); } AppEvent::ProxyWorking(proxy_type) => { state .proxies_working .entry(proxy_type) - .and_modify(|c| *c = c.saturating_add(1)) + .and_modify(move |c| *c = c.saturating_add(1)) .or_insert(1); } AppEvent::Done => { diff --git a/src/utils.rs b/src/utils.rs index a190ff2..839cc61 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,7 +5,7 @@ pub async fn is_docker() -> bool { tokio::sync::OnceCell::const_new(); *CACHE - .get_or_init(async || { + .get_or_init(async move || { tokio::fs::try_exists("/.dockerenv").await.unwrap_or(false) }) .await @@ -17,7 +17,7 @@ pub async fn is_docker() -> bool { } pub fn is_http_url(value: &str) -> bool { - url::Url::parse(value).is_ok_and(|parsed_url| { + url::Url::parse(value).is_ok_and(move |parsed_url| { let scheme = parsed_url.scheme(); (scheme == "http" || scheme == "https") && parsed_url.host_str().is_some()