]> git.proxmox.com Git - rustc.git/blob - vendor/darling_core/src/codegen/error.rs
New upstream version 1.37.0+dfsg1
[rustc.git] / vendor / darling_core / src / codegen / error.rs
1 use proc_macro2::TokenStream;
2 use quote::{TokenStreamExt, ToTokens};
3
4 /// Declares the local variable into which errors will be accumulated.
5 #[derive(Default)]
6 pub struct ErrorDeclaration {
7 __hidden: (),
8 }
9
10 impl ToTokens for ErrorDeclaration {
11 fn to_tokens(&self, tokens: &mut TokenStream) {
12 tokens.append_all(quote! {
13 let mut __errors = ::darling::export::Vec::new();
14 })
15 }
16 }
17
18 /// Returns early if attribute or body parsing has caused any errors.
19 #[derive(Default)]
20 pub struct ErrorCheck<'a> {
21 location: Option<&'a str>,
22 __hidden: (),
23 }
24
25 impl<'a> ErrorCheck<'a> {
26 pub fn with_location(location: &'a str) -> Self {
27 ErrorCheck {
28 location: Some(location),
29 __hidden: (),
30 }
31 }
32 }
33
34 impl<'a> ToTokens for ErrorCheck<'a> {
35 fn to_tokens(&self, tokens: &mut TokenStream) {
36 let at_call = if let Some(ref s) = self.location {
37 quote!(.at(#s))
38 } else {
39 quote!()
40 };
41
42 tokens.append_all(quote! {
43 if !__errors.is_empty() {
44 return ::darling::export::Err(::darling::Error::multiple(__errors) #at_call);
45 }
46 })
47 }
48 }