From c4f6b2f0d4ea125ecceb3f5190037d5a7431d321 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] 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 289545a..6cd9c96 100644 --- a/rrule/Cargo.toml +++ b/rrule/Cargo.toml @@ -15,7 +15,6 @@ edition.workspace = true [dependencies] chrono = "0.4.19" chrono-tz = "0.9.0" -lazy_static = "1.4.0" log = "0.4.16" regex = { version = "1.5.5", default-features = false, features = ["perf", "std"] } clap = { version = "4.1.9", 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()))