From dd8f3e4528d45b7602df7ea6380b92a87dc4f711 Mon Sep 17 00:00:00 2001 From: mtul0729 <52401682+mtul0729@users.noreply.github.com> Date: Wed, 26 Mar 2025 08:34:00 +0800 Subject: refactor: replace `lazy_static` with `std::sync::LazyLock` (#1246) --- src/utils/request.rs | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'src/utils/request.rs') diff --git a/src/utils/request.rs b/src/utils/request.rs index 4516806..8cee0a0 100644 --- a/src/utils/request.rs +++ b/src/utils/request.rs @@ -8,6 +8,7 @@ use reqwest::Url; use scraper::{Html, Selector}; use serde::Deserialize; use serde_json::Value; +use std::sync::LazyLock; use std::{ collections::{HashMap, HashSet}, sync::Arc, @@ -26,28 +27,35 @@ const MAX_CRAWLS: usize = 5; const BREAK_ON_ERROR: bool = false; const USER_AGENT: &str = "curl/8.6.0"; -lazy_static::lazy_static! { - static ref CLIENT: Result = { - let builder = reqwest::ClientBuilder::new().timeout(Duration::from_secs(16)); - let client = builder.build()?; - Ok(client) - }; +static CLIENT: LazyLock> = LazyLock::new(|| { + let builder = reqwest::ClientBuilder::new().timeout(Duration::from_secs(16)); + let client = builder.build()?; + Ok(client) +}); - static ref PRESET: Vec<(Regex, CrawlOptions)> = vec![ +static PRESET: LazyLock> = LazyLock::new(|| { + vec![ ( Regex::new(r"github.com/([^/]+)/([^/]+)/tree/([^/]+)").unwrap(), - CrawlOptions { exclude: vec!["changelog".into(), "changes".into(), "license".into()], ..Default::default() } + CrawlOptions { + exclude: vec!["changelog".into(), "changes".into(), "license".into()], + ..Default::default() + }, ), ( Regex::new(r"github.com/([^/]+)/([^/]+)/wiki").unwrap(), - CrawlOptions { exclude: vec!["_history".into()], extract: Some("#wiki-body".into()), ..Default::default() } - - ) - ]; + CrawlOptions { + exclude: vec!["_history".into()], + extract: Some("#wiki-body".into()), + ..Default::default() + }, + ), + ] +}); - static ref EXTENSION_RE: Regex = Regex::new(r"\.[^.]+$").unwrap(); - static ref GITHUB_REPO_RE: Regex = Regex::new(r"^https://github\.com/([^/]+)/([^/]+)/tree/([^/]+)").unwrap(); -} +static EXTENSION_RE: LazyLock = LazyLock::new(|| Regex::new(r"\.[^.]+$").unwrap()); +static GITHUB_REPO_RE: LazyLock = + LazyLock::new(|| Regex::new(r"^https://github\.com/([^/]+)/([^/]+)/tree/([^/]+)").unwrap()); pub async fn fetch(url: &str) -> Result { let client = match *CLIENT { -- cgit v1.2.3