From cb0edfd4fc8863b1a29741725fb3e0e3ca2bc3cd Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 29 Jun 2024 00:29:58 -0500 Subject: [PATCH] use std LazyLock and simplify a .replace --- Cargo.lock | 1 - Cargo.toml | 8 ++++++-- src/config.rs | 5 ++--- src/engines/answer/fend.rs | 7 +++---- src/engines/answer/numbat.rs | 5 ++--- src/engines/mod.rs | 5 ++--- src/engines/search/bing.rs | 3 +-- 7 files changed, 16 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a5ddd58..40aa129 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1092,7 +1092,6 @@ dependencies = [ "futures", "maud", "numbat", - "once_cell", "rand", "regex", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 1189bf4..ee5a4df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,10 +25,14 @@ fend-core = "1.4.5" futures = "0.3.30" maud = "0.26.0" numbat = "1.11.0" -once_cell = "1.19.0" rand = "0.8.5" regex = "1.10.3" -reqwest = { version = "0.11.26", default-features = false, features = ["rustls-tls", "gzip", "deflate", "brotli"] } +reqwest = { version = "0.11.26", default-features = false, features = [ + "rustls-tls", + "gzip", + "deflate", + "brotli", +] } scraper = "0.19.0" serde = { version = "1.0.197", features = ["derive"] } # preserve_order is needed for google images. yippee! diff --git a/src/config.rs b/src/config.rs index 87454ec..187c22f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,5 @@ -use std::{collections::HashMap, fs, net::SocketAddr, path::Path}; +use std::{collections::HashMap, fs, net::SocketAddr, path::Path, sync::LazyLock}; -use once_cell::sync::Lazy; use serde::Deserialize; use tracing::info; @@ -221,7 +220,7 @@ impl Default for EngineConfig { } } } -static DEFAULT_ENGINE_CONFIG_REF: Lazy = Lazy::new(EngineConfig::default); +static DEFAULT_ENGINE_CONFIG_REF: LazyLock = LazyLock::new(EngineConfig::default); impl EngineConfig { pub fn new() -> Self { Self::default() diff --git a/src/engines/answer/fend.rs b/src/engines/answer/fend.rs index 99e8e33..e170147 100644 --- a/src/engines/answer/fend.rs +++ b/src/engines/answer/fend.rs @@ -1,8 +1,7 @@ -use std::cell::Cell; +use std::{cell::Cell, sync::LazyLock}; use fend_core::SpanKind; use maud::{html, PreEscaped}; -use once_cell::sync::Lazy; use crate::engines::EngineResponse; @@ -106,7 +105,7 @@ fn evaluate_to_html(query: &str, html: bool) -> Option> { Some(PreEscaped(result_html)) } -pub static FEND_CONTEXT: Lazy = Lazy::new(|| { +pub static FEND_CTX: LazyLock = LazyLock::new(|| { let mut context = fend_core::Context::new(); // make lowercase f and c work @@ -185,7 +184,7 @@ fn evaluate_into_spans(query: &str, multiline: bool) -> Vec { } } - let mut context = FEND_CONTEXT.clone(); + let mut context = FEND_CTX.clone(); if multiline { // this makes it generate slightly nicer outputs for some queries like 2d6 context.set_output_mode_terminal(); diff --git a/src/engines/answer/numbat.rs b/src/engines/answer/numbat.rs index c429b51..bb6184e 100644 --- a/src/engines/answer/numbat.rs +++ b/src/engines/answer/numbat.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use std::{collections::HashSet, sync::LazyLock}; use fend_core::SpanKind; use maud::{html, PreEscaped}; @@ -8,7 +8,6 @@ use numbat::{ resolver::CodeSource, InterpreterResult, InterpreterSettings, Statement, }; -use once_cell::sync::Lazy; use tracing::debug; use crate::engines::EngineResponse; @@ -179,7 +178,7 @@ fn markup_to_html(markup: Markup) -> PreEscaped { PreEscaped(html) } -pub static NUMBAT_CTX: Lazy = Lazy::new(|| { +pub static NUMBAT_CTX: LazyLock = LazyLock::new(|| { let mut ctx = numbat::Context::new(numbat::module_importer::BuiltinModuleImporter {}); let _ = ctx.interpret("use prelude", CodeSource::Internal); let _ = ctx.interpret("use units::currencies", CodeSource::Internal); diff --git a/src/engines/mod.rs b/src/engines/mod.rs index 028dccf..3e8cb72 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -4,14 +4,13 @@ use std::{ net::IpAddr, ops::Deref, str::FromStr, - sync::Arc, + sync::{Arc, LazyLock}, time::{Duration, Instant}, }; use eyre::bail; use futures::future::join_all; use maud::PreEscaped; -use once_cell::sync::Lazy; use reqwest::{header::HeaderMap, RequestBuilder}; use serde::{Deserialize, Deserializer}; use tokio::sync::mpsc; @@ -587,7 +586,7 @@ pub async fn autocomplete(config: &Config, query: &str) -> eyre::Result = Lazy::new(|| { +pub static CLIENT: LazyLock = LazyLock::new(|| { reqwest::ClientBuilder::new() .local_address(IpAddr::from_str("0.0.0.0").unwrap()) // we pretend to be a normal browser so websites don't block us diff --git a/src/engines/search/bing.rs b/src/engines/search/bing.rs index c0a9535..4097611 100644 --- a/src/engines/search/bing.rs +++ b/src/engines/search/bing.rs @@ -115,8 +115,7 @@ pub fn parse_images_response(body: &str) -> eyre::Result { .and_then(|v| v.as_str()) .unwrap_or_default() // bing adds these unicode characters around matches - .replace('', "") - .replace('', ""); + .replace(['', ''], ""); // the text looks like "1200 x 1600 · jpegWikipedia" // (the last part is incorrectly parsed since the actual text is inside another