Debian Codegraph @

src/tokens/schema.rs

raw
1 use quote::quote;
2
3 use crate::types::Schema;
4 use crate::utils::quote_message;
5
6 pub fn schema_tokens(schema: Schema) -> proc_macro2::TokenStream {
7 let fn_call = schema.function;
8 let args = if let Some(args) = schema.use_context {
9 if args {
10 quote!(&self, args)
11 } else {
12 quote!(&self)
13 }
14 } else {
15 quote!(&self)
16 };
17
18 let skip_on_errors = schema.skip_on_field_errors.unwrap_or(true);
19
20 let message = quote_message(schema.message);
21
22 let code = if let Some(c) = schema.code {
23 quote!(
24 err.code = ::std::borrow::Cow::from(#c);
25 )
26 } else {
27 quote!()
28 };
29
30 let fn_call = quote! {
31 match #fn_call(#args) {
32 ::std::result::Result::Ok(()) => {}
33 ::std::result::Result::Err(mut err) => {
34 #code
35 #message
36 errors.add("__all__", err);
37 }
38 }
39 };
40
41 if skip_on_errors {
42 quote! {
43 if errors.is_empty() || ((errors.field_errors().len() == 1) && errors.field_errors().contains_key("__all__")) {
44 #fn_call
45 }
46 }
47 } else {
48 quote! {
49 #fn_call
50 }
51 }
52 }