-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make derive macro implement Constraints trait
Signed-off-by: Johannes Löthberg <johannes.loethberg@elokon.com>
- Loading branch information
Showing
19 changed files
with
615 additions
and
419 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,32 @@ | ||
use quote::quote; | ||
|
||
use crate::types::Card; | ||
use crate::utils::{quote_code, quote_message}; | ||
use crate::utils::quote_message; | ||
|
||
pub fn credit_card_tokens( | ||
credit_card: Card, | ||
field_name: &proc_macro2::TokenStream, | ||
field_name_str: &str, | ||
) -> proc_macro2::TokenStream { | ||
) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) { | ||
let message = quote_message(credit_card.message); | ||
let code = quote_code(credit_card.code, "credit_card"); | ||
let code = credit_card.code.as_deref().unwrap_or("credit_card"); | ||
|
||
quote! { | ||
if !#field_name.validate_credit_card() { | ||
#code | ||
#message | ||
err.add_param(::std::borrow::Cow::from("value"), &#field_name); | ||
errors.add(#field_name_str, err); | ||
} | ||
} | ||
( | ||
quote! { | ||
if !#field_name.validate_credit_card() { | ||
let mut err = ::validator::ValidationError::new(#code); | ||
#message | ||
err.add_param(::std::borrow::Cow::from("value"), &#field_name); | ||
errors.add(#field_name_str, err); | ||
} | ||
}, | ||
quote! { | ||
constraints.add( | ||
#field_name_str, | ||
::validator::ValidationConstraint::CreditCard { | ||
code: #code.into(), | ||
}, | ||
); | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,38 @@ | ||
use quote::quote; | ||
|
||
use crate::types::Contains; | ||
use crate::utils::{quote_code, quote_message}; | ||
use crate::utils::quote_message; | ||
|
||
pub fn contains_tokens( | ||
contains: Contains, | ||
field_name: &proc_macro2::TokenStream, | ||
field_name_str: &str, | ||
) -> proc_macro2::TokenStream { | ||
) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) { | ||
let p = contains.pattern; | ||
let (needle, needle_err) = | ||
(quote!(#p), quote!(err.add_param(::std::borrow::Cow::from("needle"), &#p);)); | ||
|
||
let message = quote_message(contains.message); | ||
let code = quote_code(contains.code, "contains"); | ||
let code = contains.code.as_deref().unwrap_or("contains"); | ||
|
||
quote! { | ||
if !#field_name.validate_contains(#needle) { | ||
#code | ||
#message | ||
#needle_err | ||
err.add_param(::std::borrow::Cow::from("value"), &#field_name); | ||
errors.add(#field_name_str, err); | ||
} | ||
} | ||
( | ||
quote! { | ||
if !#field_name.validate_contains(#needle) { | ||
let mut err = ::validator::ValidationError::new(#code); | ||
#message | ||
#needle_err | ||
err.add_param(::std::borrow::Cow::from("value"), &#field_name); | ||
errors.add(#field_name_str, err); | ||
} | ||
}, | ||
quote! { | ||
constraints.add( | ||
#field_name_str, | ||
::validator::ValidationConstraint::Contains { | ||
code: #code.into(), | ||
pattern: #p.into(), | ||
}, | ||
); | ||
}, | ||
) | ||
} |
Oops, something went wrong.