From e88106cef41c640a7e0f1edd560ac22f600f935d Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 22 Nov 2021 19:58:31 +0100 Subject: [PATCH] Use once_cell instead of lazy_static --- Cargo.lock | 12 +++++------- crates/api/Cargo.toml | 1 - crates/api_crud/Cargo.toml | 1 - crates/apub/Cargo.toml | 2 +- crates/apub/src/context.rs | 10 +++++----- crates/apub/src/lib.rs | 3 --- crates/apub_lib/Cargo.toml | 2 +- crates/apub_lib/src/lib.rs | 3 --- crates/apub_lib/src/object_id.rs | 9 +++++---- crates/apub_lib/src/signatures.rs | 7 +++---- crates/db_schema/Cargo.toml | 2 +- crates/db_schema/src/lib.rs | 12 +++++------- crates/routes/Cargo.toml | 2 +- crates/routes/src/feeds.rs | 19 +++++++++---------- crates/routes/src/lib.rs | 3 --- crates/utils/Cargo.toml | 2 +- crates/utils/src/lib.rs | 2 -- crates/utils/src/settings/mod.rs | 13 +++++++------ crates/utils/src/utils.rs | 29 ++++++++++++++++------------- 19 files changed, 60 insertions(+), 74 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index abb50135..aa8d170b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1760,7 +1760,6 @@ dependencies = [ "http", "http-signature-normalization-actix", "itertools", - "lazy_static", "lemmy_api_common", "lemmy_apub", "lemmy_apub_lib", @@ -1822,7 +1821,6 @@ dependencies = [ "http", "http-signature-normalization-actix", "itertools", - "lazy_static", "lemmy_api_common", "lemmy_apub", "lemmy_apub_lib", @@ -1869,7 +1867,6 @@ dependencies = [ "http", "http-signature-normalization-actix", "itertools", - "lazy_static", "lemmy_api_common", "lemmy_apub_lib", "lemmy_db_schema", @@ -1878,6 +1875,7 @@ dependencies = [ "lemmy_utils", "lemmy_websocket", "log", + "once_cell", "percent-encoding", "rand 0.8.4", "reqwest", @@ -1908,10 +1906,10 @@ dependencies = [ "http", "http-signature-normalization-actix", "http-signature-normalization-reqwest", - "lazy_static", "lemmy_apub_lib_derive", "lemmy_utils", "log", + "once_cell", "openssl", "reqwest", "serde", @@ -1939,10 +1937,10 @@ dependencies = [ "diesel", "diesel-derive-newtype", "diesel_migrations", - "lazy_static", "lemmy_apub_lib", "lemmy_utils", "log", + "once_cell", "regex", "serde", "serde_json", @@ -1995,7 +1993,6 @@ dependencies = [ "awc", "chrono", "diesel", - "lazy_static", "lemmy_api_common", "lemmy_apub", "lemmy_db_schema", @@ -2004,6 +2001,7 @@ dependencies = [ "lemmy_utils", "lemmy_websocket", "log", + "once_cell", "rss", "serde", "sha2", @@ -2066,9 +2064,9 @@ dependencies = [ "http", "itertools", "jsonwebtoken", - "lazy_static", "lettre", "log", + "once_cell", "openssl", "percent-encoding", "rand 0.8.4", diff --git a/crates/api/Cargo.toml b/crates/api/Cargo.toml index 8d88f8db..7d53cb33 100644 --- a/crates/api/Cargo.toml +++ b/crates/api/Cargo.toml @@ -35,7 +35,6 @@ log = "0.4.14" rand = "0.8.4" strum = "0.21.0" strum_macros = "0.21.1" -lazy_static = "1.4.0" url = { version = "2.2.2", features = ["serde"] } openssl = "0.10.36" http = "0.2.5" diff --git a/crates/api_crud/Cargo.toml b/crates/api_crud/Cargo.toml index a5f1216f..6372ebf4 100644 --- a/crates/api_crud/Cargo.toml +++ b/crates/api_crud/Cargo.toml @@ -30,7 +30,6 @@ log = "0.4.14" rand = "0.8.4" strum = "0.21.0" strum_macros = "0.21.1" -lazy_static = "1.4.0" url = { version = "2.2.2", features = ["serde"] } openssl = "0.10.36" http = "0.2.5" diff --git a/crates/apub/Cargo.toml b/crates/apub/Cargo.toml index b4ddff28..8e5c312d 100644 --- a/crates/apub/Cargo.toml +++ b/crates/apub/Cargo.toml @@ -50,7 +50,7 @@ thiserror = "1.0.29" background-jobs = "0.9.1" reqwest = { version = "0.11.4", features = ["json"] } html2md = "0.2.13" -lazy_static = "1.4.0" +once_cell = "1.8.0" [dev-dependencies] serial_test = "0.5.1" diff --git a/crates/apub/src/context.rs b/crates/apub/src/context.rs index 0a6e220a..3ec444e3 100644 --- a/crates/apub/src/context.rs +++ b/crates/apub/src/context.rs @@ -1,9 +1,9 @@ +use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; -lazy_static! { - static ref CONTEXT: Vec = - serde_json::from_str(include_str!("../assets/lemmy/context.json")).expect("parse context"); -} +static CONTEXT: Lazy> = Lazy::new(|| { + serde_json::from_str(include_str!("../assets/lemmy/context.json")).expect("parse context") +}); #[derive(Serialize, Deserialize, Debug)] pub(crate) struct WithContext { @@ -16,7 +16,7 @@ pub(crate) struct WithContext { impl WithContext { pub(crate) fn new(inner: T) -> WithContext { WithContext { - context: CONTEXT.clone(), + context: (*CONTEXT).clone(), inner, } } diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index e400f81e..984d722e 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -17,9 +17,6 @@ pub mod migrations; pub mod objects; pub mod protocol; -#[macro_use] -extern crate lazy_static; - /// Checks if the ID is allowed for sending or receiving. /// /// In particular, it checks for: diff --git a/crates/apub_lib/Cargo.toml b/crates/apub_lib/Cargo.toml index ef9d0b2d..1e30cd8d 100644 --- a/crates/apub_lib/Cargo.toml +++ b/crates/apub_lib/Cargo.toml @@ -20,7 +20,7 @@ reqwest = { version = "0.11.4", features = ["json"] } log = "0.4.14" base64 = "0.13.0" openssl = "0.10.36" -lazy_static = "1.4.0" +once_cell = "1.8.0" http = "0.2.5" sha2 = "0.9.8" actix-web = { version = "4.0.0-beta.9", default-features = false } diff --git a/crates/apub_lib/src/lib.rs b/crates/apub_lib/src/lib.rs index 82c19005..3c11fcea 100644 --- a/crates/apub_lib/src/lib.rs +++ b/crates/apub_lib/src/lib.rs @@ -1,6 +1,3 @@ -#[macro_use] -extern crate lazy_static; - pub mod activity_queue; pub mod data; pub mod object_id; diff --git a/crates/apub_lib/src/object_id.rs b/crates/apub_lib/src/object_id.rs index cf5dfaa1..7d3be9e5 100644 --- a/crates/apub_lib/src/object_id.rs +++ b/crates/apub_lib/src/object_id.rs @@ -8,6 +8,7 @@ use lemmy_utils::{ LemmyError, }; use log::info; +use once_cell::sync::Lazy; use reqwest::{Client, StatusCode}; use serde::{Deserialize, Serialize}; use std::{ @@ -21,12 +22,12 @@ use url::Url; /// fetch through the search). This should be configurable. static REQUEST_LIMIT: i32 = 25; -lazy_static! { - static ref CLIENT: Client = Client::builder() +static CLIENT: Lazy = Lazy::new(|| { + Client::builder() .user_agent(build_user_agent(&Settings::get())) .build() - .expect("Couldn't build client"); -} + .expect("Couldn't build client") +}); /// We store Url on the heap because it is quite large (88 bytes). #[derive(Clone, PartialEq, Serialize, Deserialize, Debug)] diff --git a/crates/apub_lib/src/signatures.rs b/crates/apub_lib/src/signatures.rs index d05f997b..2ba7edd4 100644 --- a/crates/apub_lib/src/signatures.rs +++ b/crates/apub_lib/src/signatures.rs @@ -6,6 +6,7 @@ use http_signature_normalization_actix::Config as ConfigActix; use http_signature_normalization_reqwest::prelude::{Config, SignExt}; use lemmy_utils::LemmyError; use log::debug; +use once_cell::sync::Lazy; use openssl::{ hash::MessageDigest, pkey::PKey, @@ -17,10 +18,8 @@ use sha2::{Digest, Sha256}; use std::str::FromStr; use url::Url; -lazy_static! { - static ref CONFIG2: ConfigActix = ConfigActix::new(); - static ref HTTP_SIG_CONFIG: Config = Config::new(); -} +static CONFIG2: Lazy = Lazy::new(ConfigActix::new); +static HTTP_SIG_CONFIG: Lazy = Lazy::new(Config::new); /// Creates an HTTP post request to `inbox_url`, with the given `client` and `headers`, and /// `activity` as request body. The request is signed with `private_key` and then sent. diff --git a/crates/db_schema/Cargo.toml b/crates/db_schema/Cargo.toml index 45655d6a..b7e48691 100644 --- a/crates/db_schema/Cargo.toml +++ b/crates/db_schema/Cargo.toml @@ -22,7 +22,7 @@ log = "0.4.14" url = { version = "2.2.2", features = ["serde"] } diesel-derive-newtype = "0.1.2" regex = "1.5.4" -lazy_static = "1.4.0" +once_cell = "1.8.0" strum = "0.21.0" strum_macros = "0.21.1" sha2 = "0.9.8" diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index a2a19290..ddff1375 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -2,8 +2,6 @@ extern crate diesel; #[macro_use] extern crate diesel_derive_newtype; -#[macro_use] -extern crate lazy_static; // this is used in tests #[allow(unused_imports)] #[macro_use] @@ -24,6 +22,7 @@ use crate::newtypes::DbUrl; use chrono::NaiveDateTime; use diesel::{Connection, PgConnection}; use lemmy_utils::ApiError; +use once_cell::sync::Lazy; use regex::Regex; use serde::{Deserialize, Serialize}; use std::{env, env::VarError}; @@ -133,11 +132,10 @@ pub fn naive_now() -> NaiveDateTime { chrono::prelude::Utc::now().naive_utc() } -lazy_static! { - static ref EMAIL_REGEX: Regex = - Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$") - .expect("compile email regex"); -} +static EMAIL_REGEX: Lazy = Lazy::new(|| { + Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$") + .expect("compile email regex") +}); pub mod functions { use diesel::sql_types::*; diff --git a/crates/routes/Cargo.toml b/crates/routes/Cargo.toml index 8c459afa..00b17133 100644 --- a/crates/routes/Cargo.toml +++ b/crates/routes/Cargo.toml @@ -32,4 +32,4 @@ serde = { version = "1.0.130", features = ["derive"] } awc = { version = "3.0.0-beta.8", default-features = false } url = { version = "2.2.2", features = ["serde"] } strum = "0.21.0" -lazy_static = "1.4.0" +once_cell = "1.8.0" diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index 1a85a10a..534ec9fb 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -18,6 +18,7 @@ use lemmy_db_views::{ use lemmy_db_views_actor::person_mention_view::{PersonMentionQueryBuilder, PersonMentionView}; use lemmy_utils::{claims::Claims, utils::markdown_to_html, LemmyError}; use lemmy_websocket::LemmyContext; +use once_cell::sync::Lazy; use rss::{ extension::dublincore::DublinCoreExtensionBuilder, ChannelBuilder, @@ -48,16 +49,14 @@ pub fn config(cfg: &mut web::ServiceConfig) { .route("/feeds/local.xml", web::get().to(get_local_feed)); } -lazy_static! { - static ref RSS_NAMESPACE: HashMap = { - let mut h = HashMap::new(); - h.insert( - "dc".to_string(), - rss::extension::dublincore::NAMESPACE.to_string(), - ); - h - }; -} +static RSS_NAMESPACE: Lazy> = Lazy::new(|| { + let mut h = HashMap::new(); + h.insert( + "dc".to_string(), + rss::extension::dublincore::NAMESPACE.to_string(), + ); + h +}); async fn get_all_feed( info: web::Query, diff --git a/crates/routes/src/lib.rs b/crates/routes/src/lib.rs index b966b766..e158fa55 100644 --- a/crates/routes/src/lib.rs +++ b/crates/routes/src/lib.rs @@ -1,6 +1,3 @@ -#[macro_use] -extern crate lazy_static; - pub mod feeds; pub mod images; pub mod nodeinfo; diff --git a/crates/utils/Cargo.toml b/crates/utils/Cargo.toml index 2c2482b5..fa75be69 100644 --- a/crates/utils/Cargo.toml +++ b/crates/utils/Cargo.toml @@ -24,7 +24,7 @@ serde = { version = "1.0.130", features = ["derive"] } serde_json = { version = "1.0.68", features = ["preserve_order"] } thiserror = "1.0.29" comrak = { version = "0.12.1", default-features = false } -lazy_static = "1.4.0" +once_cell = "1.8.0" openssl = "0.10.36" url = { version = "2.2.2", features = ["serde"] } actix-web = { version = "4.0.0-beta.9", default-features = false, features = ["rustls"] } diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index 057e067e..e30150e3 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -1,6 +1,4 @@ #[macro_use] -extern crate lazy_static; -#[macro_use] extern crate strum_macros; #[macro_use] extern crate smart_default; diff --git a/crates/utils/src/settings/mod.rs b/crates/utils/src/settings/mod.rs index 260acbd9..de194090 100644 --- a/crates/utils/src/settings/mod.rs +++ b/crates/utils/src/settings/mod.rs @@ -1,6 +1,7 @@ use crate::{location_info, settings::structs::Settings, LemmyError}; use anyhow::{anyhow, Context}; use deser_hjson::from_str; +use once_cell::sync::Lazy; use regex::{Regex, RegexBuilder}; use std::{env, fs, io::Error, sync::RwLock}; @@ -8,15 +9,15 @@ pub mod structs; static DEFAULT_CONFIG_FILE: &str = "config/config.hjson"; -lazy_static! { - static ref SETTINGS: RwLock = - RwLock::new(Settings::init().expect("Failed to load settings file")); - static ref WEBFINGER_REGEX: Regex = Regex::new(&format!( +static SETTINGS: Lazy> = + Lazy::new(|| RwLock::new(Settings::init().expect("Failed to load settings file"))); +static WEBFINGER_REGEX: Lazy = Lazy::new(|| { + Regex::new(&format!( "^acct:([a-z0-9_]{{3,}})@{}$", Settings::get().hostname )) - .expect("compile webfinger regex"); -} + .expect("compile webfinger regex") +}); impl Settings { /// Reads config from configuration file. diff --git a/crates/utils/src/utils.rs b/crates/utils/src/utils.rs index 3039a1cd..1f3252ff 100644 --- a/crates/utils/src/utils.rs +++ b/crates/utils/src/utils.rs @@ -2,23 +2,26 @@ use crate::{ApiError, IpAddr}; use actix_web::dev::ConnectionInfo; use chrono::{DateTime, FixedOffset, NaiveDateTime}; use itertools::Itertools; +use once_cell::sync::Lazy; use rand::{distributions::Alphanumeric, thread_rng, Rng}; use regex::Regex; use url::Url; -lazy_static! { - static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").expect("compile regex"); - - static ref USERNAME_MATCHES_REGEX: Regex = Regex::new(r"/u/[a-zA-Z][0-9a-zA-Z_]*").expect("compile regex"); - // TODO keep this old one, it didn't work with port well tho - // static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P[\w.]+)@(?P[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)").expect("compile regex"); - static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P[\w.]+)@(?P[a-zA-Z0-9._:-]+)").expect("compile regex"); - static ref VALID_ACTOR_NAME_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_]{3,}$").expect("compile regex"); - static ref VALID_POST_TITLE_REGEX: Regex = Regex::new(r".*\S.*").expect("compile regex"); - static ref VALID_MATRIX_ID_REGEX: Regex = Regex::new(r"^@[A-Za-z0-9._=-]+:[A-Za-z0-9.-]+\.[A-Za-z]{2,}$").expect("compile regex"); - // taken from https://en.wikipedia.org/wiki/UTM_parameters - static ref CLEAN_URL_PARAMS_REGEX: Regex = Regex::new(r"^utm_source|utm_medium|utm_campaign|utm_term|utm_content|gclid|gclsrc|dclid|fbclid$").expect("compile regex"); -} +static MENTIONS_REGEX: Lazy = Lazy::new(|| { + Regex::new(r"@(?P[\w.]+)@(?P[a-zA-Z0-9._:-]+)").expect("compile regex") +}); +static VALID_ACTOR_NAME_REGEX: Lazy = + Lazy::new(|| Regex::new(r"^[a-zA-Z0-9_]{3,}$").expect("compile regex")); +static VALID_POST_TITLE_REGEX: Lazy = + Lazy::new(|| Regex::new(r".*\S.*").expect("compile regex")); +static VALID_MATRIX_ID_REGEX: Lazy = Lazy::new(|| { + Regex::new(r"^@[A-Za-z0-9._=-]+:[A-Za-z0-9.-]+\.[A-Za-z]{2,}$").expect("compile regex") +}); +// taken from https://en.wikipedia.org/wiki/UTM_parameters +static CLEAN_URL_PARAMS_REGEX: Lazy = Lazy::new(|| { + Regex::new(r"^utm_source|utm_medium|utm_campaign|utm_term|utm_content|gclid|gclsrc|dclid|fbclid$") + .expect("compile regex") +}); pub fn naive_from_unix(time: i64) -> NaiveDateTime { NaiveDateTime::from_timestamp(time, 0) -- 2.44.1