diff options
| author | mtul0729 <52401682+mtul0729@users.noreply.github.com> | 2025-03-26 08:34:00 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-26 08:34:00 +0800 |
| commit | dd8f3e4528d45b7602df7ea6380b92a87dc4f711 (patch) | |
| tree | faf8368ed6e06a78547959414710848ff19a06ff /src/utils | |
| parent | a71e7c6be72965afd537aae076e0aee9e6f0755e (diff) | |
| download | aichat-dd8f3e4528d45b7602df7ea6380b92a87dc4f711.tar.gz | |
refactor: replace `lazy_static` with `std::sync::LazyLock` (#1246)
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/clipboard.rs | 6 | ||||
| -rw-r--r-- | src/utils/command.rs | 5 | ||||
| -rw-r--r-- | src/utils/mod.rs | 19 | ||||
| -rw-r--r-- | src/utils/request.rs | 38 | ||||
| -rw-r--r-- | src/utils/variables.rs | 5 |
5 files changed, 42 insertions, 31 deletions
diff --git a/src/utils/clipboard.rs b/src/utils/clipboard.rs index 23bb4f3..12e1655 100644 --- a/src/utils/clipboard.rs +++ b/src/utils/clipboard.rs @@ -1,8 +1,6 @@ #[cfg(not(any(target_os = "android", target_os = "emscripten")))] -lazy_static::lazy_static! { - static ref CLIPBOARD: std::sync::Arc<std::sync::Mutex<Option<arboard::Clipboard>>> = - std::sync::Arc::new(std::sync::Mutex::new(arboard::Clipboard::new().ok())); -} +static CLIPBOARD: std::sync::LazyLock<std::sync::Mutex<Option<arboard::Clipboard>>> = + std::sync::LazyLock::new(|| std::sync::Mutex::new(arboard::Clipboard::new().ok())); #[cfg(not(any(target_os = "android", target_os = "emscripten")))] pub fn set_text(text: &str) -> anyhow::Result<()> { diff --git a/src/utils/command.rs b/src/utils/command.rs index 300a471..14a40a4 100644 --- a/src/utils/command.rs +++ b/src/utils/command.rs @@ -12,10 +12,9 @@ use std::{ use anyhow::{anyhow, bail, Context, Result}; use dirs::home_dir; +use std::sync::LazyLock; -lazy_static::lazy_static! { - pub static ref SHELL: Shell = detect_shell(); -} +pub static SHELL: LazyLock<Shell> = LazyLock::new(detect_shell); pub struct Shell { pub name: String, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 38617a6..98f2da1 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -27,15 +27,22 @@ use fancy_regex::Regex; use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; use is_terminal::IsTerminal; use std::borrow::Cow; +use std::sync::LazyLock; use std::{env, path::PathBuf, process}; use unicode_segmentation::UnicodeSegmentation; -lazy_static::lazy_static! { - pub static ref CODE_BLOCK_RE: Regex = Regex::new(r"(?ms)```\w*(.*)```").unwrap(); - pub static ref THINK_TAG_RE: Regex = Regex::new(r"(?s)^\s*<think>.*?</think>(\s*|$)").unwrap(); - pub static ref IS_STDOUT_TERMINAL: bool = std::io::stdout().is_terminal(); - pub static ref NO_COLOR: bool = env::var("NO_COLOR").ok().and_then(|v| parse_bool(&v)).unwrap_or_default() || !*IS_STDOUT_TERMINAL; -} +pub static CODE_BLOCK_RE: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"(?ms)```\w*(.*)```").unwrap()); +pub static THINK_TAG_RE: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"(?s)^\s*<think>.*?</think>(\s*|$)").unwrap()); +pub static IS_STDOUT_TERMINAL: LazyLock<bool> = LazyLock::new(|| std::io::stdout().is_terminal()); +pub static NO_COLOR: LazyLock<bool> = LazyLock::new(|| { + env::var("NO_COLOR") + .ok() + .and_then(|v| parse_bool(&v)) + .unwrap_or_default() + || !*IS_STDOUT_TERMINAL +}); pub fn now() -> String { chrono::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, false) 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<reqwest::Client> = { - let builder = reqwest::ClientBuilder::new().timeout(Duration::from_secs(16)); - let client = builder.build()?; - Ok(client) - }; +static CLIENT: LazyLock<Result<reqwest::Client>> = 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<Vec<(Regex, CrawlOptions)>> = 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<Regex> = LazyLock::new(|| Regex::new(r"\.[^.]+$").unwrap()); +static GITHUB_REPO_RE: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"^https://github\.com/([^/]+)/([^/]+)/tree/([^/]+)").unwrap()); pub async fn fetch(url: &str) -> Result<String> { let client = match *CLIENT { diff --git a/src/utils/variables.rs b/src/utils/variables.rs index 91c9912..bfec4ca 100644 --- a/src/utils/variables.rs +++ b/src/utils/variables.rs @@ -1,9 +1,8 @@ use super::*; use fancy_regex::{Captures, Regex}; +use std::sync::LazyLock; -lazy_static::lazy_static! { - pub static ref RE_VARIABLE: Regex = Regex::new(r"\{\{(\w+)\}\}").unwrap(); -} +pub static RE_VARIABLE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\{\{(\w+)\}\}").unwrap()); pub fn interpolate_variables(text: &mut String) { *text = RE_VARIABLE .replace_all(text, |caps: &Captures<'_>| { |
