1 use darling::{util::Flag, Error, FromDeriveInput, FromMeta};
2 use proc_macro2::Ident;
3 use syn::parse_quote;
4 
5 #[derive(FromMeta)]
6 #[darling(and_then = Self::validate)]
7 struct Vis {
8     public: Flag,
9     private: Flag,
10 }
11 
12 impl Vis {
validate(self) -> darling::Result<Self>13     fn validate(self) -> darling::Result<Self> {
14         if self.public.is_present() && self.private.is_present() {
15             return Err(Error::custom("Cannot be both public and private"));
16         }
17 
18         Ok(self)
19     }
20 }
21 
22 #[derive(FromDeriveInput)]
23 #[darling(attributes(sample))]
24 #[allow(dead_code)]
25 struct Example {
26     ident: Ident,
27     label: String,
28     volume: usize,
29     #[darling(flatten)]
30     visibility: Vis,
31 }
32 
33 #[test]
many_errors()34 fn many_errors() {
35     let e = Example::from_derive_input(&parse_quote! {
36         #[sample(volume = 10, public, private)]
37         struct Demo {}
38     })
39     .map(|_| "Should have failed")
40     .unwrap_err();
41 
42     // We are expecting an error from the Vis::validate method and an error for the
43     // missing `label` field.
44     assert_eq!(e.len(), 2);
45 }
46