From 3132c202677bf7b98e1bc7cb79a428d68acfaadb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Gonz=C3=A1lez?= Date: Sat, 18 Jan 2025 01:12:54 +0100 Subject: [PATCH 1/3] Remove `lazy_static` dependency in favor of `std` `OnceLock` --- rrule/Cargo.toml | 1 - rrule/src/parser/regex.rs | 28 +++++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/rrule/Cargo.toml b/rrule/Cargo.toml index 1a0b40d..4df7537 100644 --- a/rrule/Cargo.toml +++ b/rrule/Cargo.toml @@ -15,7 +15,6 @@ edition.workspace = true [dependencies] chrono = "0.4.39" chrono-tz = "0.10.1" -lazy_static = "1.4.0" log = "0.4.25" regex = { version = "1.11.1", default-features = false, features = ["perf", "std"] } clap = { version = "4.5.26", optional = true, features = ["derive"] } diff --git a/rrule/src/parser/regex.rs b/rrule/src/parser/regex.rs index cc252c6..6662e93 100644 --- a/rrule/src/parser/regex.rs +++ b/rrule/src/parser/regex.rs @@ -1,17 +1,11 @@ //! Utility functions around the regexes we use for parsing rrule strings. -use std::str::FromStr; -use lazy_static::lazy_static; +use std::{str::FromStr, sync::OnceLock}; + use regex::{Captures, Regex}; use super::{content_line::PropertyName, ParseError}; -lazy_static! { - static ref DATESTR_RE: Regex = - Regex::new(r"(?m)^([0-9]{4})([0-9]{2})([0-9]{2})(T([0-9]{2})([0-9]{2})([0-9]{2})(Z?))?$") - .expect("DATESTR_RE regex failed"); -} - #[derive(Debug, PartialEq)] pub(crate) struct ParsedDateString { pub year: i32, @@ -50,7 +44,15 @@ impl ParsedDateString { /// Parses a date string with format `YYYYMMDD(THHMMSSZ)` where the part in parentheses /// is optional. It returns [`ParsedDateString`]. pub(crate) fn from_ical_datetime(val: &str) -> Result { + static DATESTR_RE: OnceLock = OnceLock::new(); + let captures = DATESTR_RE + .get_or_init(|| { + Regex::new( + r"(?m)^([0-9]{4})([0-9]{2})([0-9]{2})(T([0-9]{2})([0-9]{2})([0-9]{2})(Z?))?$", + ) + .expect("DATESTR_RE regex failed") + }) .captures(val) .ok_or_else(|| ParseError::InvalidDateTimeFormat(val.into()))?; @@ -84,14 +86,14 @@ impl ParsedDateString { } } -lazy_static! { - static ref PARSE_PROPERTY_NAME_RE: Regex = - Regex::new(r"(?m)^([A-Z]+?)[:;]").expect("PARSE_PROPERTY_NAME_RE regex failed"); -} - /// Get the line property name, the `RRULE:`, `EXRULE:` etc part. pub(crate) fn get_property_name(val: &str) -> Result, ParseError> { + static PARSE_PROPERTY_NAME_RE: OnceLock = OnceLock::new(); + PARSE_PROPERTY_NAME_RE + .get_or_init(|| { + Regex::new(r"(?m)^([A-Z]+?)[:;]").expect("PARSE_PROPERTY_NAME_RE regex failed") + }) .captures(val) .and_then(|captures| captures.get(1)) .map(|name| PropertyName::from_str(name.as_str())) From 21e4e41dab3bd819aba53c150fb3e49762611959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Gonz=C3=A1lez?= <7822554+AlexTMjugador@users.noreply.github.com> Date: Tue, 28 Jan 2025 10:50:39 +0100 Subject: [PATCH 2/3] Apply @WhyNotHugo's suggestion Co-authored-by: Hugo --- rrule/src/parser/regex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rrule/src/parser/regex.rs b/rrule/src/parser/regex.rs index 6662e93..735da69 100644 --- a/rrule/src/parser/regex.rs +++ b/rrule/src/parser/regex.rs @@ -51,7 +51,7 @@ impl ParsedDateString { Regex::new( r"(?m)^([0-9]{4})([0-9]{2})([0-9]{2})(T([0-9]{2})([0-9]{2})([0-9]{2})(Z?))?$", ) - .expect("DATESTR_RE regex failed") + .expect("DATESTR_RE must compile") }) .captures(val) .ok_or_else(|| ParseError::InvalidDateTimeFormat(val.into()))?; From 8858addee284f8d352f08a1d2e405afa260b4bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Gonz=C3=A1lez?= <7822554+AlexTMjugador@users.noreply.github.com> Date: Tue, 28 Jan 2025 10:50:50 +0100 Subject: [PATCH 3/3] Apply @WhyNotHugo's suggestion Co-authored-by: Hugo --- rrule/src/parser/regex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rrule/src/parser/regex.rs b/rrule/src/parser/regex.rs index 735da69..53ba5f2 100644 --- a/rrule/src/parser/regex.rs +++ b/rrule/src/parser/regex.rs @@ -92,7 +92,7 @@ pub(crate) fn get_property_name(val: &str) -> Result, Parse PARSE_PROPERTY_NAME_RE .get_or_init(|| { - Regex::new(r"(?m)^([A-Z]+?)[:;]").expect("PARSE_PROPERTY_NAME_RE regex failed") + Regex::new(r"(?m)^([A-Z]+?)[:;]").expect("PARSE_PROPERTY_NAME_RE regex must compile") }) .captures(val) .and_then(|captures| captures.get(1))