use std LazyLock and simplify a .replace

This commit is contained in:
mat 2024-06-29 00:29:58 -05:00
parent ea256f007f
commit cb0edfd4fc
7 changed files with 16 additions and 18 deletions

1
Cargo.lock generated
View File

@ -1092,7 +1092,6 @@ dependencies = [
"futures",
"maud",
"numbat",
"once_cell",
"rand",
"regex",
"reqwest",

View File

@ -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!

View File

@ -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<EngineConfig> = Lazy::new(EngineConfig::default);
static DEFAULT_ENGINE_CONFIG_REF: LazyLock<EngineConfig> = LazyLock::new(EngineConfig::default);
impl EngineConfig {
pub fn new() -> Self {
Self::default()

View File

@ -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<PreEscaped<String>> {
Some(PreEscaped(result_html))
}
pub static FEND_CONTEXT: Lazy<fend_core::Context> = Lazy::new(|| {
pub static FEND_CTX: LazyLock<fend_core::Context> = 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<Span> {
}
}
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();

View File

@ -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<String> {
PreEscaped(html)
}
pub static NUMBAT_CTX: Lazy<numbat::Context> = Lazy::new(|| {
pub static NUMBAT_CTX: LazyLock<numbat::Context> = 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);

View File

@ -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<Vec<Stri
))
}
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(|| {
pub static CLIENT: LazyLock<reqwest::Client> = 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

View File

@ -115,8 +115,7 @@ pub fn parse_images_response(body: &str) -> eyre::Result<EngineImagesResponse> {
.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