garde_fr/rules/
pattern.rs1use super::AsStr;
36use crate::error::Error;
37
38pub fn apply<T: Pattern, M: Matcher>(v: &T, (pat,): (&M,)) -> Result<(), Error> {
39 if !v.validate_pattern(pat) {
40 return Err(Error::new(format!(
41 "ne correspond pas au pattern /{}/",
42 pat.as_str()
43 )));
44 }
45 Ok(())
46}
47
48pub trait Matcher: AsStr {
49 fn is_match(&self, haystack: &str) -> bool;
51}
52
53pub trait Pattern {
54 fn validate_pattern<M: Matcher>(&self, matcher: &M) -> bool;
55}
56
57impl<T: AsStr> Pattern for T {
58 fn validate_pattern<M: Matcher>(&self, matcher: &M) -> bool {
59 matcher.is_match(self.as_str())
60 }
61}
62
63impl<T: Pattern> Pattern for Option<T> {
64 fn validate_pattern<M: Matcher>(&self, matcher: &M) -> bool {
65 match self {
66 Some(value) => value.validate_pattern(matcher),
67 None => true,
68 }
69 }
70}
71
72#[cfg(all(
73 feature = "regex",
74 feature = "js-sys",
75 target_arch = "wasm32",
76 target_os = "unknown"
77))]
78#[doc(hidden)]
79pub mod regex_js_sys {
80 pub use ::js_sys::RegExp;
81
82 use super::*;
83
84 impl Matcher for RegExp {
85 fn is_match(&self, haystack: &str) -> bool {
86 self.test(haystack)
87 }
88 }
89
90 impl AsStr for RegExp {
91 fn as_str(&self) -> &str {
92 "[Not supported in JS]"
93 }
94 }
95
96 pub struct SyncWrapper<T>(T);
97
98 impl<T> SyncWrapper<T> {
99 pub const unsafe fn new(inner: T) -> Self {
101 Self(inner)
102 }
103 }
104
105 impl<T: AsStr> AsStr for SyncWrapper<T> {
106 fn as_str(&self) -> &str {
107 self.0.as_str()
108 }
109 }
110
111 impl<T: Matcher> Matcher for SyncWrapper<T> {
112 fn is_match(&self, haystack: &str) -> bool {
113 self.0.is_match(haystack)
114 }
115 }
116
117 unsafe impl<T> Send for SyncWrapper<T> {}
118 unsafe impl<T> Sync for SyncWrapper<T> {}
119
120 pub type StaticPattern = once_cell::sync::Lazy<SyncWrapper<RegExp>>;
121
122 #[macro_export]
123 macro_rules! __init_js_sys_pattern {
124 ($pat:literal) => {
125 $crate::rules::pattern::regex_js_sys::StaticPattern::new(|| {
126 unsafe { $crate::rules::pattern::regex_js_sys::SyncWrapper::new(::js_sys::RegExp::new($pat, "u")) }
128 })
129 };
130 }
131 pub use crate::__init_js_sys_pattern as init_pattern;
132}
133
134#[cfg(feature = "regex")]
135#[doc(hidden)]
136pub mod regex {
137 pub use ::regex::Regex;
138
139 use super::*;
140
141 impl Matcher for Regex {
142 fn is_match(&self, haystack: &str) -> bool {
143 self.is_match(haystack)
144 }
145 }
146
147 impl<T: Matcher> Matcher for once_cell::sync::Lazy<T> {
148 fn is_match(&self, haystack: &str) -> bool {
149 once_cell::sync::Lazy::force(self).is_match(haystack)
150 }
151 }
152
153 impl AsStr for Regex {
154 fn as_str(&self) -> &str {
155 self.as_str()
156 }
157 }
158
159 impl<T: AsStr> AsStr for once_cell::sync::Lazy<T> {
160 fn as_str(&self) -> &str {
161 once_cell::sync::Lazy::force(self).as_str()
162 }
163 }
164
165 pub type StaticPattern = once_cell::sync::Lazy<Regex>;
166
167 #[macro_export]
168 macro_rules! __init_pattern {
169 ($pat:literal) => {
170 $crate::rules::pattern::regex::StaticPattern::new(|| {
171 $crate::rules::pattern::regex::Regex::new($pat).unwrap()
172 })
173 };
174 }
175 pub use crate::__init_pattern as init_pattern;
176}