| 1 | use proc_macro2::TokenStream; |
| 2 | use quote::{quote, ToTokens, TokenStreamExt}; |
| 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(iter:quote! { |
| 13 | let mut __errors = ::darling::Error::accumulator(); |
| 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: TokenStream = if let Some(ref s: &&'a str) = self.location { |
| 37 | quote!(.map_err(|e| e.at(#s))) |
| 38 | } else { |
| 39 | quote!() |
| 40 | }; |
| 41 | |
| 42 | tokens.append_all(iter:quote! { |
| 43 | __errors.finish() #at_call?; |
| 44 | }) |
| 45 | } |
| 46 | } |
| 47 | |