Skip to content

Commit 75857fb

Browse files
authored
feat: use once_cell instead of lazy_static (#1894)
1 parent c8db0c8 commit 75857fb

File tree

8 files changed

+61
-70
lines changed

8 files changed

+61
-70
lines changed

Cargo.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ anyhow = "1.0.28"
2020
chrono = "0.4"
2121
clap = { version = "3.0", features = ["cargo"] }
2222
clap_complete = "3.0"
23+
once_cell = "1"
2324
env_logger = "0.9.0"
2425
handlebars = "4.0"
25-
lazy_static = "1.0"
2626
log = "0.4"
2727
memchr = "2.0"
2828
opener = "0.5"

src/preprocess/index.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::path::Path;
44
use super::{Preprocessor, PreprocessorContext};
55
use crate::book::{Book, BookItem};
66
use crate::errors::*;
7-
use lazy_static::lazy_static;
87
use log::warn;
8+
use once_cell::sync::Lazy;
99

1010
/// A preprocessor for converting file name `README.md` to `index.md` since
1111
/// `README.md` is the de facto index file in markdown-based documentation.
@@ -68,9 +68,8 @@ fn warn_readme_name_conflict<P: AsRef<Path>>(readme_path: P, index_path: P) {
6868
}
6969

7070
fn is_readme_file<P: AsRef<Path>>(path: P) -> bool {
71-
lazy_static! {
72-
static ref RE: Regex = Regex::new(r"(?i)^readme$").unwrap();
73-
}
71+
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)^readme$").unwrap());
72+
7473
RE.is_match(
7574
path.as_ref()
7675
.file_stem()

src/preprocess/links.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::path::{Path, PathBuf};
1010

1111
use super::{Preprocessor, PreprocessorContext};
1212
use crate::book::{Book, BookItem};
13-
use lazy_static::lazy_static;
1413
use log::{error, warn};
14+
use once_cell::sync::Lazy;
1515

1616
const ESCAPE_CHAR: char = '\\';
1717
const MAX_LINK_NESTED_DEPTH: usize = 10;
@@ -410,19 +410,20 @@ impl<'a> Iterator for LinkIter<'a> {
410410
fn find_links(contents: &str) -> LinkIter<'_> {
411411
// lazily compute following regex
412412
// r"\\\{\{#.*\}\}|\{\{#([a-zA-Z0-9]+)\s*([^}]+)\}\}")?;
413-
lazy_static! {
414-
static ref RE: Regex = Regex::new(
413+
static RE: Lazy<Regex> = Lazy::new(|| {
414+
Regex::new(
415415
r"(?x) # insignificant whitespace mode
416-
\\\{\{\#.*\}\} # match escaped link
417-
| # or
418-
\{\{\s* # link opening parens and whitespace
419-
\#([a-zA-Z0-9_]+) # link type
420-
\s+ # separating whitespace
421-
([^}]+) # link target path and space separated properties
422-
\}\} # link closing parens"
416+
\\\{\{\#.*\}\} # match escaped link
417+
| # or
418+
\{\{\s* # link opening parens and whitespace
419+
\#([a-zA-Z0-9_]+) # link type
420+
\s+ # separating whitespace
421+
([^}]+) # link target path and space separated properties
422+
\}\} # link closing parens",
423423
)
424-
.unwrap();
425-
}
424+
.unwrap()
425+
});
426+
426427
LinkIter(RE.captures_iter(contents))
427428
}
428429

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use std::path::{Path, PathBuf};
1414

1515
use crate::utils::fs::get_404_output_file;
1616
use handlebars::Handlebars;
17-
use lazy_static::lazy_static;
1817
use log::{debug, trace, warn};
18+
use once_cell::sync::Lazy;
1919
use regex::{Captures, Regex};
2020
use serde_json::json;
2121

@@ -767,9 +767,8 @@ fn make_data(
767767
/// Goes through the rendered HTML, making sure all header tags have
768768
/// an anchor respectively so people can link to sections directly.
769769
fn build_header_links(html: &str) -> String {
770-
lazy_static! {
771-
static ref BUILD_HEADER_LINKS: Regex = Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap();
772-
}
770+
static BUILD_HEADER_LINKS: Lazy<Regex> =
771+
Lazy::new(|| Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap());
773772

774773
let mut id_counter = HashMap::new();
775774

@@ -810,10 +809,8 @@ fn insert_link_into_header(
810809
// ```
811810
// This function replaces all commas by spaces in the code block classes
812811
fn fix_code_blocks(html: &str) -> String {
813-
lazy_static! {
814-
static ref FIX_CODE_BLOCKS: Regex =
815-
Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap();
816-
}
812+
static FIX_CODE_BLOCKS: Lazy<Regex> =
813+
Lazy::new(|| Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap());
817814

818815
FIX_CODE_BLOCKS
819816
.replace_all(html, |caps: &Captures<'_>| {
@@ -836,10 +833,9 @@ fn add_playground_pre(
836833
playground_config: &Playground,
837834
edition: Option<RustEdition>,
838835
) -> String {
839-
lazy_static! {
840-
static ref ADD_PLAYGROUND_PRE: Regex =
841-
Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap();
842-
}
836+
static ADD_PLAYGROUND_PRE: Lazy<Regex> =
837+
Lazy::new(|| Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap());
838+
843839
ADD_PLAYGROUND_PRE
844840
.replace_all(html, |caps: &Captures<'_>| {
845841
let text = &caps[1];
@@ -902,9 +898,7 @@ fn add_playground_pre(
902898
}
903899

904900
fn hide_lines(content: &str) -> String {
905-
lazy_static! {
906-
static ref BORING_LINES_REGEX: Regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap();
907-
}
901+
static BORING_LINES_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\s*)#(.?)(.*)$").unwrap());
908902

909903
let mut result = String::with_capacity(content.len());
910904
let mut lines = content.lines().peekable();

src/renderer/html_handlebars/search.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use std::collections::{HashMap, HashSet};
33
use std::path::Path;
44

55
use elasticlunr::{Index, IndexBuilder};
6+
use once_cell::sync::Lazy;
67
use pulldown_cmark::*;
78

89
use crate::book::{Book, BookItem};
910
use crate::config::Search;
1011
use crate::errors::*;
1112
use crate::theme::searcher;
1213
use crate::utils;
13-
use lazy_static::lazy_static;
1414
use log::{debug, warn};
1515
use serde::Serialize;
1616

@@ -267,21 +267,19 @@ fn write_to_json(index: Index, search_config: &Search, doc_urls: Vec<String>) ->
267267
}
268268

269269
fn clean_html(html: &str) -> String {
270-
lazy_static! {
271-
static ref AMMONIA: ammonia::Builder<'static> = {
272-
let mut clean_content = HashSet::new();
273-
clean_content.insert("script");
274-
clean_content.insert("style");
275-
let mut builder = ammonia::Builder::new();
276-
builder
277-
.tags(HashSet::new())
278-
.tag_attributes(HashMap::new())
279-
.generic_attributes(HashSet::new())
280-
.link_rel(None)
281-
.allowed_classes(HashMap::new())
282-
.clean_content_tags(clean_content);
283-
builder
284-
};
285-
}
270+
static AMMONIA: Lazy<ammonia::Builder<'static>> = Lazy::new(|| {
271+
let mut clean_content = HashSet::new();
272+
clean_content.insert("script");
273+
clean_content.insert("style");
274+
let mut builder = ammonia::Builder::new();
275+
builder
276+
.tags(HashSet::new())
277+
.tag_attributes(HashMap::new())
278+
.generic_attributes(HashSet::new())
279+
.link_rel(None)
280+
.allowed_classes(HashMap::new())
281+
.clean_content_tags(clean_content);
282+
builder
283+
});
286284
AMMONIA.clean(html).to_string()
287285
}

src/utils/mod.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ pub mod fs;
44
mod string;
55
pub(crate) mod toml_ext;
66
use crate::errors::Error;
7-
use lazy_static::lazy_static;
87
use log::error;
8+
use once_cell::sync::Lazy;
99
use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, Options, Parser, Tag};
1010
use regex::Regex;
1111

@@ -21,9 +21,7 @@ pub use self::string::{
2121

2222
/// Replaces multiple consecutive whitespace characters with a single space character.
2323
pub fn collapse_whitespace(text: &str) -> Cow<'_, str> {
24-
lazy_static! {
25-
static ref RE: Regex = Regex::new(r"\s\s+").unwrap();
26-
}
24+
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\s\s+").unwrap());
2725
RE.replace_all(text, " ")
2826
}
2927

@@ -52,9 +50,7 @@ pub fn id_from_content(content: &str) -> String {
5250
let mut content = content.to_string();
5351

5452
// Skip any tags or html-encoded stuff
55-
lazy_static! {
56-
static ref HTML: Regex = Regex::new(r"(<.*?>)").unwrap();
57-
}
53+
static HTML: Lazy<Regex> = Lazy::new(|| Regex::new(r"(<.*?>)").unwrap());
5854
content = HTML.replace_all(&content, "").into();
5955
const REPL_SUB: &[&str] = &["&lt;", "&gt;", "&amp;", "&#39;", "&quot;"];
6056
for sub in REPL_SUB {
@@ -97,10 +93,9 @@ pub fn unique_id_from_content(content: &str, id_counter: &mut HashMap<String, us
9793
/// None. Ideally, print page links would link to anchors on the print page,
9894
/// but that is very difficult.
9995
fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> {
100-
lazy_static! {
101-
static ref SCHEME_LINK: Regex = Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap();
102-
static ref MD_LINK: Regex = Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap();
103-
}
96+
static SCHEME_LINK: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap());
97+
static MD_LINK: Lazy<Regex> =
98+
Lazy::new(|| Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap());
10499

105100
fn fix<'a>(dest: CowStr<'a>, path: Option<&Path>) -> CowStr<'a> {
106101
if dest.starts_with('#') {
@@ -153,10 +148,8 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> {
153148
// There are dozens of HTML tags/attributes that contain paths, so
154149
// feel free to add more tags if desired; these are the only ones I
155150
// care about right now.
156-
lazy_static! {
157-
static ref HTML_LINK: Regex =
158-
Regex::new(r#"(<(?:a|img) [^>]*?(?:src|href)=")([^"]+?)""#).unwrap();
159-
}
151+
static HTML_LINK: Lazy<Regex> =
152+
Lazy::new(|| Regex::new(r#"(<(?:a|img) [^>]*?(?:src|href)=")([^"]+?)""#).unwrap());
160153

161154
HTML_LINK
162155
.replace_all(&html, |caps: &regex::Captures<'_>| {

src/utils/string.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lazy_static::lazy_static;
1+
use once_cell::sync::Lazy;
22
use regex::Regex;
33
use std::ops::Bound::{Excluded, Included, Unbounded};
44
use std::ops::RangeBounds;
@@ -24,10 +24,10 @@ pub fn take_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String {
2424
}
2525
}
2626

27-
lazy_static! {
28-
static ref ANCHOR_START: Regex = Regex::new(r"ANCHOR:\s*(?P<anchor_name>[\w_-]+)").unwrap();
29-
static ref ANCHOR_END: Regex = Regex::new(r"ANCHOR_END:\s*(?P<anchor_name>[\w_-]+)").unwrap();
30-
}
27+
static ANCHOR_START: Lazy<Regex> =
28+
Lazy::new(|| Regex::new(r"ANCHOR:\s*(?P<anchor_name>[\w_-]+)").unwrap());
29+
static ANCHOR_END: Lazy<Regex> =
30+
Lazy::new(|| Regex::new(r"ANCHOR_END:\s*(?P<anchor_name>[\w_-]+)").unwrap());
3131

3232
/// Take anchored lines from a string.
3333
/// Lines containing anchor are ignored.

0 commit comments

Comments
 (0)