Skip to content

Commit

Permalink
Remove HasLen
Browse files Browse the repository at this point in the history
Plus a couple of clippy suggestions
  • Loading branch information
future-highway committed Oct 7, 2023
1 parent 65cbe24 commit 29c81ac
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 95 deletions.
2 changes: 1 addition & 1 deletion validator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub use validation::regex::ValidateRegex;
pub use validation::required::ValidateRequired;
pub use validation::urls::ValidateUrl;

pub use traits::{HasLen, Validate, ValidateArgs};
pub use traits::{Validate, ValidateArgs};
pub use types::{ValidationError, ValidationErrors, ValidationErrorsKind};

#[cfg(feature = "derive")]
Expand Down
90 changes: 1 addition & 89 deletions validator/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,93 +1,5 @@
use std::{
borrow::Cow,
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
rc::Rc,
sync::Arc,
};
use std::cell::{Ref, RefMut};
use std::collections::VecDeque;

#[cfg(feature = "indexmap")]
use indexmap::{IndexMap, IndexSet};

use crate::types::ValidationErrors;

/// Trait to implement if one wants to make the `length` validator
/// work for more types
///
/// A bit sad it's not there by default in Rust
pub trait HasLen {
fn length(&self) -> u64;
}

macro_rules! impl_type_that_derefs {
($type_:ty) => {
impl<T> HasLen for $type_
where T: HasLen {
fn length(&self) -> u64 {
T::length(self)
}
}
};
}

impl_type_that_derefs!(&T);
impl_type_that_derefs!(Arc<T>);
impl_type_that_derefs!(Box<T>);
impl_type_that_derefs!(Rc<T>);
impl_type_that_derefs!(Ref<'_, T>);
impl_type_that_derefs!(RefMut<'_, T>);

macro_rules! impl_type_with_chars {
($type_:ty) => {
impl HasLen for $type_ {
fn length(&self) -> u64 {
self.chars().count() as u64
}
}
};
}

impl_type_with_chars!(str);
impl_type_with_chars!(&str);
impl_type_with_chars!(String);

macro_rules! impl_type_with_len {
($type_:ty, $($generic:ident),*$(,)*) => {
impl<$($generic),*> HasLen for $type_ {
fn length(&self) -> u64 {
self.len() as u64
}
}
};
}

impl_type_with_len!([T], T);
impl_type_with_len!(BTreeSet<T>, T);
impl_type_with_len!(BTreeMap<K, V>, K, V);
impl_type_with_len!(HashSet<T, S>, T, S);
impl_type_with_len!(HashMap<K, V, S>, K, V, S);
impl_type_with_len!(Vec<T>, T);
impl_type_with_len!(VecDeque<T>, T);
#[cfg(feature = "indexmap")]
impl_type_with_len!(IndexSet<T>, T);
#[cfg(feature = "indexmap")]
impl_type_with_len!(IndexMap<K, V>, K, V);

impl<'cow, T> HasLen for Cow<'cow, T>
where T: ToOwned + ?Sized,
for<'a> &'a T: HasLen {
fn length(&self) -> u64 {
self.as_ref().length()
}
}

impl<T, const N: usize> HasLen for [T; N] {
fn length(&self) -> u64 {
N as u64
}
}

/// This is the original trait that was implemented by deriving `Validate`. It will still be
/// implemented for struct validations that don't take custom arguments. The call is being
/// forwarded to the `ValidateArgs<'v_a>` trait.
Expand All @@ -97,7 +9,7 @@ pub trait Validate {

impl<T: Validate> Validate for &T {
fn validate(&self) -> Result<(), ValidationErrors> {
T::validate(*self)
T::validate(self)
}
}

Expand Down
4 changes: 2 additions & 2 deletions validator/src/validation/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use lazy_static::lazy_static;
use regex::Regex;
use std::borrow::Cow;

use crate::{HasLen, ValidateIp};
use crate::{ValidateIp};

lazy_static! {
// Regex from the specs
Expand Down Expand Up @@ -53,7 +53,7 @@ pub trait ValidateEmail {
// according to RFC5321 the max length of the local part is 64 characters
// and the max length of the domain part is 255 characters
// https://datatracker.ietf.org/doc/html/rfc5321#section-4.5.3.1.1
if user_part.length() > 64 || domain_part.length() > 255 {
if user_part.chars().count() > 64 || domain_part.chars().count() > 255 {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion validator_derive/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct ValidateField {
}

impl ValidateField {
pub fn validate(&self, struct_ident: &Ident, all_fields: &Vec<&Field>, current_field: &Field) {
pub fn validate(&self, struct_ident: &Ident, all_fields: &[&Field], current_field: &Field) {
let field_name = self.ident.clone().expect("Field is not a named field").to_string();
let field_attrs = &current_field.attrs;

Expand Down
4 changes: 2 additions & 2 deletions validator_derive/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ pub fn quote_use_stmts(fields: &Vec<ValidateField>) -> proc_macro2::TokenStream
)
}

pub fn get_attr<'a>(attrs: &'a Vec<Attribute>, name: &str) -> Option<&'a Attribute> {
pub fn get_attr<'a>(attrs: &'a [Attribute], name: &str) -> Option<&'a Attribute> {
attrs.iter().find(|a| match &a.meta {
syn::Meta::List(list) => list.tokens.clone().into_iter().any(|t| match t {
proc_macro2::TokenTree::Ident(i) => &i.to_string() == name,
proc_macro2::TokenTree::Ident(i) => i == name,
_ => false,
}),
_ => false,
Expand Down

0 comments on commit 29c81ac

Please sign in to comment.