Skip to content

Commit 4896bb7

Browse files
authored
feat: Replace OnceCell with static OnceLock for regex compilation (#250)
1 parent 161493a commit 4896bb7

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Diff for: atrium-api/src/types/string.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ipld_core::cid;
77
use langtag::{LanguageTag, LanguageTagBuf};
88
use regex::Regex;
99
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
10-
use std::{cell::OnceCell, cmp, ops::Deref, str::FromStr};
10+
use std::{cmp, ops::Deref, str::FromStr, sync::OnceLock};
1111

1212
/// Common trait implementations for Lexicon string formats that are newtype wrappers
1313
/// around `String`.
@@ -213,7 +213,7 @@ impl FromStr for Datetime {
213213
// datetimes to the subset that is also valid under ISO 8601. Apply a regex that
214214
// validates enough of the relevant ISO 8601 format that the RFC 3339 parser can
215215
// do the rest.
216-
const RE_ISO_8601: OnceCell<Regex> = OnceCell::new();
216+
static RE_ISO_8601: OnceLock<Regex> = OnceLock::new();
217217
if RE_ISO_8601
218218
.get_or_init(|| Regex::new(r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?(Z|(\+[0-9]{2}|\-[0-9][1-9]):[0-9]{2})$").unwrap())
219219
.is_match(s)
@@ -267,7 +267,7 @@ impl Did {
267267
#[allow(clippy::borrow_interior_mutable_const, clippy::declare_interior_mutable_const)]
268268
/// Parses a `Did` from the given string.
269269
pub fn new(did: String) -> Result<Self, &'static str> {
270-
const RE_DID: OnceCell<Regex> = OnceCell::new();
270+
static RE_DID: OnceLock<Regex> = OnceLock::new();
271271

272272
// https://atproto.com/specs/did#at-protocol-did-identifier-syntax
273273
if did.len() > 2048 {
@@ -305,7 +305,7 @@ impl Handle {
305305
#[allow(clippy::borrow_interior_mutable_const, clippy::declare_interior_mutable_const)]
306306
/// Parses a `Handle` from the given string.
307307
pub fn new(handle: String) -> Result<Self, &'static str> {
308-
const RE_HANDLE: OnceCell<Regex> = OnceCell::new();
308+
static RE_HANDLE: OnceLock<Regex> = OnceLock::new();
309309

310310
// https://atproto.com/specs/handle#handle-identifier-syntax
311311
if handle.len() > 253 {
@@ -338,7 +338,7 @@ impl Nsid {
338338
#[allow(clippy::borrow_interior_mutable_const, clippy::declare_interior_mutable_const)]
339339
/// Parses an NSID from the given string.
340340
pub fn new(nsid: String) -> Result<Self, &'static str> {
341-
const RE_NSID: OnceCell<Regex> = OnceCell::new();
341+
static RE_NSID: OnceLock<Regex> = OnceLock::new();
342342

343343
// https://atproto.com/specs/handle#handle-identifier-syntax
344344
if nsid.len() > 317 {
@@ -420,7 +420,7 @@ impl Tid {
420420
#[allow(clippy::borrow_interior_mutable_const, clippy::declare_interior_mutable_const)]
421421
/// Parses a `TID` from the given string.
422422
pub fn new(tid: String) -> Result<Self, &'static str> {
423-
const RE_TID: OnceCell<Regex> = OnceCell::new();
423+
static RE_TID: OnceLock<Regex> = OnceLock::new();
424424

425425
if tid.len() != 13 {
426426
Err("TID must be 13 characters")
@@ -452,7 +452,7 @@ impl RecordKey {
452452
#[allow(clippy::borrow_interior_mutable_const, clippy::declare_interior_mutable_const)]
453453
/// Parses a `Record Key` from the given string.
454454
pub fn new(s: String) -> Result<Self, &'static str> {
455-
const RE_RKEY: OnceCell<Regex> = OnceCell::new();
455+
static RE_RKEY: OnceLock<Regex> = OnceLock::new();
456456

457457
if [".", ".."].contains(&s.as_str()) {
458458
Err("Disallowed rkey")

0 commit comments

Comments
 (0)