add move to closures

This commit is contained in:
monosans
2025-05-05 14:47:59 +03:00
parent 1e7fb96dfc
commit 0ce2f1a8b4
10 changed files with 34 additions and 26 deletions
+1 -1
View File
@@ -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(),
+1 -1
View File
@@ -8,7 +8,7 @@ pub async fn get_cache_path() -> color_eyre::Result<PathBuf> {
static CACHE: tokio::sync::OnceCell<PathBuf> =
tokio::sync::OnceCell::const_new();
Ok(CACHE
.get_or_try_init(async || -> color_eyre::Result<PathBuf> {
.get_or_try_init(async move || -> color_eyre::Result<PathBuf> {
let mut path = tokio::task::spawn_blocking(dirs::cache_dir)
.await
.wrap_err("failed to spawn task to get user's cache directory")?
+1 -1
View File
@@ -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);
}
+9 -9
View File
@@ -21,7 +21,7 @@ fn sort_by_timeout(proxy: &Proxy) -> tokio::time::Duration {
fn sort_naturally(proxy: &Proxy) -> (ProxyType, Vec<u8>, u16) {
let host_key = proxy.host.parse::<Ipv4Addr>().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);
+6 -4
View File
@@ -2,16 +2,18 @@ use std::sync::LazyLock;
use color_eyre::eyre::{OptionExt as _, WrapErr as _};
pub static PROXY_REGEX: LazyLock<fancy_regex::Regex> = LazyLock::new(|| {
fancy_regex::RegexBuilder::new(
pub static PROXY_REGEX: LazyLock<fancy_regex::Regex> = LazyLock::new(
move || {
fancy_regex::RegexBuilder::new(
r"(?:^|[^\dA-Za-z])(?:(?P<protocol>https?|socks[45]):\/\/)?(?:(?P<username>[^\s:@]+):(?P<password>[^\s:@]+)@)?(?P<host>(?:[\-\.\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<port>\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<fancy_regex::Regex> = LazyLock::new(|| {
static IPV4_REGEX: LazyLock<fancy_regex::Regex> = LazyLock::new(move || {
fancy_regex::Regex::new(
r"^(?:[0-9:A-Fa-f]+,)?\s*(?P<host>(?:\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()
+1 -1
View File
@@ -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<RawConfig> {
+6 -2
View File
@@ -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,
};
+1 -1
View File
@@ -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) {
+6 -4
View File
@@ -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 => {
+2 -2
View File
@@ -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()