| 1 |
use quote::quote; |
| 2 |
|
| 3 |
use crate::types::Custom; |
| 4 |
use crate::utils::quote_message; |
| 5 |
|
| 6 |
pub fn custom_tokens( |
| 7 |
custom: Custom, |
| 8 |
field_name: &proc_macro2::TokenStream, |
| 9 |
field_name_str: &str, |
| 10 |
) -> proc_macro2::TokenStream { |
| 11 |
let fn_call = custom.function.unwrap(); |
| 12 |
|
| 13 |
let args = if let Some(arg) = custom.use_context { |
| 14 |
if arg { |
| 15 |
quote!(#field_name, args) |
| 16 |
} else { |
| 17 |
quote!(#field_name) |
| 18 |
} |
| 19 |
} else { |
| 20 |
quote!(#field_name) |
| 21 |
}; |
| 22 |
|
| 23 |
let message = quote_message(custom.message); |
| 24 |
|
| 25 |
let code = if let Some(c) = custom.code { |
| 26 |
quote!( |
| 27 |
err.code = ::std::borrow::Cow::from(#c); |
| 28 |
) |
| 29 |
} else { |
| 30 |
quote!() |
| 31 |
}; |
| 32 |
|
| 33 |
quote! { |
| 34 |
match #fn_call(#args) { |
| 35 |
::std::result::Result::Ok(()) => {} |
| 36 |
::std::result::Result::Err(mut err) => { |
| 37 |
#code |
| 38 |
#message |
| 39 |
err.add_param(::std::borrow::Cow::from("value"), &#field_name); |
| 40 |
errors.add(#field_name_str, err); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
} |