Skip to content

Commit 874be99

Browse files
authored
Remove the once_cell dependency (#373)
* Remove once_cell crate * Remove once_cell in tests as well * Remove remaining references * Apply default lints
1 parent f94a803 commit 874be99

File tree

12 files changed

+38
-57
lines changed

12 files changed

+38
-57
lines changed

validator/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ rust-version = { workspace = true }
1414
[dependencies]
1515
url = "2"
1616
regex = { version = "1", default-features = false, features = ["std"] }
17-
once_cell = "1.18.0"
1817
idna = "1"
1918
serde = "1"
2019
serde_derive = "1"
@@ -26,4 +25,4 @@ indexmap = { version = "2.0.0", features = ["serde"], optional = true }
2625
[features]
2726
card = ["card-validate"]
2827
derive = ["validator_derive"]
29-
derive_nightly_features = ["derive","validator_derive/nightly_features"]
28+
derive_nightly_features = ["derive", "validator_derive/nightly_features"]

validator/src/validation/contains.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ where
3434
}
3535
}
3636

37-
impl<'cow, T> ValidateContains for Cow<'cow, T>
37+
impl<T> ValidateContains for Cow<'_, T>
3838
where
3939
T: ToOwned + ?Sized,
4040
for<'a> &'a T: ValidateContains,
@@ -44,7 +44,7 @@ where
4444
}
4545
}
4646

47-
impl<'a> ValidateContains for &'a str {
47+
impl ValidateContains for &str {
4848
fn validate_contains(&self, needle: &str) -> bool {
4949
self.contains(needle)
5050
}

validator/src/validation/email.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
use idna::domain_to_ascii;
2-
use once_cell::sync::Lazy;
32
use regex::Regex;
4-
use std::borrow::Cow;
3+
use std::{borrow::Cow, sync::LazyLock};
54

65
use crate::ValidateIp;
76

87
// Regex from the specs
98
// https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
109
// It will mark esoteric email addresses like quoted string as invalid
11-
static EMAIL_USER_RE: Lazy<Regex> =
12-
Lazy::new(|| Regex::new(r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+\z").unwrap());
13-
static EMAIL_DOMAIN_RE: Lazy<Regex> = Lazy::new(|| {
10+
static EMAIL_USER_RE: LazyLock<Regex> =
11+
LazyLock::new(|| Regex::new(r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+\z").unwrap());
12+
static EMAIL_DOMAIN_RE: LazyLock<Regex> = LazyLock::new(|| {
1413
Regex::new(
1514
r"^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
1615
).unwrap()
1716
});
1817
// literal form, ipv4 or ipv6 address (SMTP 4.1.3)
19-
static EMAIL_LITERAL_RE: Lazy<Regex> =
20-
Lazy::new(|| Regex::new(r"\[([a-fA-F0-9:\.]+)\]\z").unwrap());
18+
static EMAIL_LITERAL_RE: LazyLock<Regex> =
19+
LazyLock::new(|| Regex::new(r"\[([a-fA-F0-9:\.]+)\]\z").unwrap());
2120

2221
/// Checks if the domain is a valid domain and if not, check whether it's an IP
2322
#[must_use]
@@ -109,7 +108,7 @@ where
109108
}
110109
}
111110

112-
impl<'a> ValidateEmail for &'a str {
111+
impl ValidateEmail for &str {
113112
fn as_email_string(&self) -> Option<Cow<'_, str>> {
114113
Some(Cow::from(*self))
115114
}
@@ -209,9 +208,9 @@ mod tests {
209208
fn test_validate_email_rfc5321() {
210209
// 65 character local part
211210
let test = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@mail.com";
212-
assert_eq!(test.validate_email(), false);
211+
assert!(!test.validate_email());
213212
// 256 character domain part
214213
let test = "a@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com";
215-
assert_eq!(test.validate_email(), false);
214+
assert!(!test.validate_email());
216215
}
217216
}

validator/src/validation/ip.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ where
1515
T: ToString,
1616
{
1717
fn validate_ipv4(&self) -> bool {
18-
IpAddr::from_str(&self.to_string()).map_or(false, |i| i.is_ipv4())
18+
IpAddr::from_str(&self.to_string()).is_ok_and(|i| i.is_ipv4())
1919
}
2020

2121
fn validate_ipv6(&self) -> bool {
22-
IpAddr::from_str(&self.to_string()).map_or(false, |i| i.is_ipv6())
22+
IpAddr::from_str(&self.to_string()).is_ok_and(|i| i.is_ipv6())
2323
}
2424

2525
fn validate_ip(&self) -> bool {

validator/src/validation/regex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ where
8686
}
8787
}
8888

89-
impl<'cow, T> ValidateRegex for Cow<'cow, T>
89+
impl<T> ValidateRegex for Cow<'_, T>
9090
where
9191
T: ToOwned + ?Sized,
9292
for<'a> &'a T: ValidateRegex,

validator_derive/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ quote = "1"
2020
proc-macro2 = "1"
2121
proc-macro-error2 = "2"
2222
darling = { version = "0.20", features = ["suggestions"] }
23-
once_cell = "1.18.0"
2423

2524
[features]
26-
nightly_features = ["proc-macro-error2/nightly"]
25+
nightly_features = ["proc-macro-error2/nightly"]

validator_derive/src/lib.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -263,37 +263,28 @@ impl ValidationData {
263263
if let Some(context) = &self.context {
264264
// Check if context lifetime is not `'v_a`
265265
for segment in &context.segments {
266-
match &segment.arguments {
267-
PathArguments::AngleBracketed(args) => {
268-
for arg in &args.args {
269-
match arg {
270-
syn::GenericArgument::Lifetime(lt) => {
271-
if lt.ident != "v_a" {
272-
abort! {
273-
lt.ident, "Invalid argument reference";
274-
note = "The lifetime `'{}` is not supported.", lt.ident;
275-
help = "Please use the validator lifetime `'v_a`";
276-
}
277-
}
266+
if let PathArguments::AngleBracketed(args) = &segment.arguments {
267+
for arg in &args.args {
268+
if let syn::GenericArgument::Lifetime(lt) = arg {
269+
if lt.ident != "v_a" {
270+
abort! {
271+
lt.ident, "Invalid argument reference";
272+
note = "The lifetime `'{}` is not supported.", lt.ident;
273+
help = "Please use the validator lifetime `'v_a`";
278274
}
279-
_ => (),
280275
}
281276
}
282277
}
283-
_ => (),
284278
}
285279
}
286280
}
287281

288-
match &self.data {
289-
Data::Struct(fields) => {
290-
let original_fields: Vec<&Field> =
291-
fields.fields.iter().map(|f| &f.original).collect();
292-
for f in &fields.fields {
293-
f.parsed.validate(&self.ident, &original_fields, &f.original);
294-
}
282+
if let Data::Struct(fields) = &self.data {
283+
let original_fields: Vec<&Field> =
284+
fields.fields.iter().map(|f| &f.original).collect();
285+
for f in &fields.fields {
286+
f.parsed.validate(&self.ident, &original_fields, &f.original);
295287
}
296-
_ => (),
297288
}
298289

299290
Ok(self)

validator_derive/src/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use once_cell::sync::Lazy;
1+
use std::sync::LazyLock;
22

33
use darling::util::Override;
44
use darling::{FromField, FromMeta};
@@ -12,7 +12,7 @@ use crate::utils::{get_attr, CrateName};
1212

1313
static OPTIONS_TYPE: [&str; 3] = ["Option|", "std|option|Option|", "core|option|Option|"];
1414

15-
pub(crate) static NUMBER_TYPES: Lazy<Vec<String>> = Lazy::new(|| {
15+
pub(crate) static NUMBER_TYPES: LazyLock<Vec<String>> = LazyLock::new(|| {
1616
let number_types = [
1717
quote!(usize),
1818
quote!(u8),
@@ -154,7 +154,7 @@ impl ValidateField {
154154
fn find_option(mut count: u8, ty: &syn::Type) -> u8 {
155155
if let syn::Type::Path(p) = ty {
156156
let idents_of_path =
157-
p.path.segments.iter().into_iter().fold(String::new(), |mut acc, v| {
157+
p.path.segments.iter().fold(String::new(), |mut acc, v| {
158158
acc.push_str(&v.ident.to_string());
159159
acc.push('|');
160160
acc

validator_derive_tests/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ serde = { version = "1.0", features = ["derive"] }
2020
serde_json = "1.0"
2121
trybuild = "1.0"
2222
regex = "1"
23-
once_cell = "1.18.0"
2423

2524
[dependencies]
2625
indexmap = { version = "2", features = ["serde"], optional = true }

validator_derive_tests/tests/complex.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::{borrow::Cow, collections::HashMap};
1+
use std::{borrow::Cow, collections::HashMap, sync::LazyLock};
22

3-
use once_cell::sync::Lazy;
43
use regex::Regex;
54
use serde::Deserialize;
65

@@ -73,7 +72,7 @@ fn is_fine_with_many_valid_validations() {
7372

7473
#[test]
7574
fn test_can_validate_option_fields_with_lifetime() {
76-
static RE2: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-z]{2}$").unwrap());
75+
static RE2: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[a-z]{2}$").unwrap());
7776

7877
#[derive(Debug, Validate)]
7978
struct PutStruct<'a> {
@@ -117,7 +116,7 @@ fn test_can_validate_option_fields_with_lifetime() {
117116

118117
#[test]
119118
fn test_can_validate_option_fields_without_lifetime() {
120-
static RE2: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-z]{2}$").unwrap());
119+
static RE2: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[a-z]{2}$").unwrap());
121120

122121
#[derive(Debug, Validate)]
123122
struct PutStruct {

0 commit comments

Comments
 (0)