Skip to content

Commit

Permalink
move all regex gated stuff into regex submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Aug 1, 2023
1 parent e85b410 commit 8a5f858
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 52 deletions.
97 changes: 47 additions & 50 deletions garde/src/rules/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
//!
//! This trait has a blanket implementation for all `T: garde::rules::AsStr`.
#[cfg(feature = "regex")]
#[doc(hidden)]
pub use regex::Regex;

use super::AsStr;
use crate::error::Error;

Expand All @@ -54,56 +50,10 @@ pub trait Matcher: AsStr {
fn is_match(&self, haystack: &str) -> bool;
}

#[cfg(feature = "regex")]
impl Matcher for Regex {
fn is_match(&self, haystack: &str) -> bool {
self.is_match(haystack)
}
}

#[cfg(feature = "regex")]
impl<T: Matcher> Matcher for once_cell::sync::Lazy<T> {
fn is_match(&self, haystack: &str) -> bool {
once_cell::sync::Lazy::force(self).is_match(haystack)
}
}

#[cfg(feature = "regex")]
impl AsStr for Regex {
fn as_str(&self) -> &str {
self.as_str()
}
}

#[cfg(feature = "regex")]
impl<T: AsStr> AsStr for once_cell::sync::Lazy<T> {
fn as_str(&self) -> &str {
once_cell::sync::Lazy::force(self).as_str()
}
}

pub trait Pattern {
fn validate_pattern<M: Matcher>(&self, matcher: &M) -> bool;
}

#[cfg(feature = "regex")]
#[doc(hidden)]
pub type StaticPattern = once_cell::sync::Lazy<Regex>;

#[cfg(feature = "regex")]
#[doc(hidden)]
#[macro_export]
macro_rules! __init_pattern {
($pat:literal) => {
$crate::rules::pattern::StaticPattern::new(|| {
$crate::rules::pattern::Regex::new($pat).unwrap()
})
};
}
#[cfg(feature = "regex")]
#[doc(hidden)]
pub use crate::__init_pattern as init_pattern;

impl<T: AsStr> Pattern for T {
fn validate_pattern<M: Matcher>(&self, matcher: &M) -> bool {
matcher.is_match(self.as_str())
Expand All @@ -118,3 +68,50 @@ impl<T: Pattern> Pattern for Option<T> {
}
}
}

#[cfg(feature = "regex")]
pub mod regex {
#[doc(hidden)]
pub use ::regex::Regex;

use super::*;

impl Matcher for Regex {
fn is_match(&self, haystack: &str) -> bool {
self.is_match(haystack)
}
}

impl<T: Matcher> Matcher for once_cell::sync::Lazy<T> {
fn is_match(&self, haystack: &str) -> bool {
once_cell::sync::Lazy::force(self).is_match(haystack)
}
}

impl AsStr for Regex {
fn as_str(&self) -> &str {
self.as_str()
}
}

impl<T: AsStr> AsStr for once_cell::sync::Lazy<T> {
fn as_str(&self) -> &str {
once_cell::sync::Lazy::force(self).as_str()
}
}

#[doc(hidden)]
pub type StaticPattern = once_cell::sync::Lazy<Regex>;

#[doc(hidden)]
#[macro_export]
macro_rules! __init_pattern {
($pat:literal) => {
$crate::rules::pattern::regex::StaticPattern::new(|| {
$crate::rules::pattern::regex::Regex::new($pat).unwrap()
})
};
}
#[doc(hidden)]
pub use crate::__init_pattern as init_pattern;
}
4 changes: 2 additions & 2 deletions garde_derive/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ impl<'a> ToTokens for Rules<'a> {
Pattern(pat) => match pat {
model::ValidatePattern::Expr(expr) => quote_spanned!(expr.span() => (&#expr,)),
model::ValidatePattern::Lit(s) => quote!({
static PATTERN: ::garde::rules::pattern::StaticPattern =
::garde::rules::pattern::init_pattern!(#s);
static PATTERN: ::garde::rules::pattern::regex::StaticPattern =
::garde::rules::pattern::regex::init_pattern!(#s);
(&PATTERN,)
}),
},
Expand Down

0 comments on commit 8a5f858

Please sign in to comment.