Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lazy_static dependency in favor of std OnceLock #133

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading