From 0a0bcd15bf369de68b27a151e43793e95394552f Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 1 Dec 2024 18:17:53 -0500 Subject: [PATCH] Use lazy for domain set --- native/philomena/Cargo.lock | 5 +++-- native/philomena/Cargo.toml | 1 + native/philomena/src/domains.rs | 28 ++++++++++++++++------------ native/philomena/src/markdown.rs | 4 ++-- native/philomena/src/tests.rs | 4 ++-- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/native/philomena/Cargo.lock b/native/philomena/Cargo.lock index d2481f12..54aa5826 100644 --- a/native/philomena/Cargo.lock +++ b/native/philomena/Cargo.lock @@ -357,9 +357,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.19.0" +version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" [[package]] name = "percent-encoding" @@ -375,6 +375,7 @@ dependencies = [ "comrak", "http", "jemallocator", + "once_cell", "regex", "ring", "rustler", diff --git a/native/philomena/Cargo.toml b/native/philomena/Cargo.toml index 4ff4467d..e6bba775 100644 --- a/native/philomena/Cargo.toml +++ b/native/philomena/Cargo.toml @@ -14,6 +14,7 @@ base64 = "0.21" comrak = { git = "https://github.com/philomena-dev/comrak", branch = "philomena-0.29.2", default-features = false } http = "0.2" jemallocator = { version = "0.5.0", features = ["disable_initial_exec_tls"] } +once_cell = "1.20" regex = "1" ring = "0.16" rustler = "0.35" diff --git a/native/philomena/src/domains.rs b/native/philomena/src/domains.rs index c5626c12..9bcacfc9 100644 --- a/native/philomena/src/domains.rs +++ b/native/philomena/src/domains.rs @@ -1,25 +1,29 @@ -use http::Uri; -use regex::Regex; +use std::collections::BTreeSet; use std::env; -pub fn get() -> Option> { +use http::Uri; +use once_cell::sync::Lazy; +use regex::Regex; + +pub type DomainSet = BTreeSet; + +static DOMAINS: Lazy> = Lazy::new(|| { if let Ok(domains) = env::var("SITE_DOMAINS") { - return Some( - domains - .split(',') - .map(|s| s.to_string()) - .collect::>(), - ); + return Some(domains.split(',').map(|s| s.to_string()).collect()); } None +}); + +pub fn get() -> &'static Option { + &DOMAINS } -pub fn relativize(domains: &[String], url: &str) -> Option { +pub fn relativize(domains: &DomainSet, url: &str) -> Option { let uri = url.parse::().ok()?; if let Some(a) = uri.authority() { - if domains.contains(&a.host().to_string()) { + if domains.contains(a.host()) { if let Ok(re) = Regex::new(&format!(r#"^http(s)?://({})"#, regex::escape(a.host()))) { return Some(re.replace(url, "").into()); } @@ -29,6 +33,6 @@ pub fn relativize(domains: &[String], url: &str) -> Option { Some(url.into()) } -pub fn relativize_careful(domains: &[String], url: &str) -> String { +pub fn relativize_careful(domains: &DomainSet, url: &str) -> String { relativize(domains, url).unwrap_or_else(|| url.into()) } diff --git a/native/philomena/src/markdown.rs b/native/philomena/src/markdown.rs index 778deb95..fbd846ac 100644 --- a/native/philomena/src/markdown.rs +++ b/native/philomena/src/markdown.rs @@ -29,8 +29,8 @@ pub fn common_options() -> Options { options.extension.image_url_rewriter = Some(Arc::new(|url: &str| camo::image_url_careful(url))); if let Some(domains) = domains::get() { - options.extension.link_url_rewriter = Some(Arc::new(move |url: &str| { - domains::relativize_careful(&domains, url) + options.extension.link_url_rewriter = Some(Arc::new(|url: &str| { + domains::relativize_careful(domains, url) })); } diff --git a/native/philomena/src/tests.rs b/native/philomena/src/tests.rs index 84418ed4..547f0f86 100644 --- a/native/philomena/src/tests.rs +++ b/native/philomena/src/tests.rs @@ -236,8 +236,8 @@ fn image_mention_line_start() { #[test] fn auto_relative_links() { - let domains: Vec = vec!["example.com".into()]; - let f = Arc::new(move |url: &str| domains::relativize_careful(&domains, url)); + let domains = Arc::new(vec!["example.com".into()].into_iter().collect()); + let f = Arc::new(move |url: &str| domains::relativize_careful(&*domains, url)); html_opts_i( "[some link text](https://example.com/some/path)",