Skip to content

Commit

Permalink
Remove lazy_static dependency in favor of std OnceLock
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTMjugador committed Jan 27, 2025
1 parent 2d27a27 commit 812d794
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
1 change: 0 additions & 1 deletion rrule/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
28 changes: 15 additions & 13 deletions rrule/src/parser/regex.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<Self, ParseError> {
static DATESTR_RE: OnceLock<Regex> = 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()))?;

Expand Down Expand Up @@ -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<Option<PropertyName>, ParseError> {
static PARSE_PROPERTY_NAME_RE: OnceLock<Regex> = 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()))
Expand Down

0 comments on commit 812d794

Please sign in to comment.