1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
use darling::util::SpannedValue; use crate::{type_validation::CheckMode, Errors}; #[derive(darling::FromDeriveInput)] #[darling(attributes(cynic), supports(struct_named))] pub struct FragmentDeriveInput { pub(super) ident: proc_macro2::Ident, pub(super) data: darling::ast::Data<(), FragmentDeriveField>, pub schema_path: SpannedValue<String>, pub query_module: SpannedValue<String>, pub graphql_type: SpannedValue<String>, #[darling(default)] pub argument_struct: Option<syn::Ident>, } impl FragmentDeriveInput { pub fn validate(&self) -> Result<(), Errors> { let errors = self .data .clone() .map_struct_fields(|field| field.validate().err()) .take_struct() .unwrap() .into_iter() .flatten() .collect::<Errors>(); if !errors.is_empty() { return Err(errors); } Ok(()) } } #[derive(darling::FromField, Clone)] #[darling(attributes(cynic), forward_attrs(arguments))] pub struct FragmentDeriveField { pub(super) ident: Option<proc_macro2::Ident>, pub(super) ty: syn::Type, pub(super) attrs: Vec<syn::Attribute>, #[darling(default)] pub(super) flatten: bool, #[darling(default)] pub(super) recurse: Option<SpannedValue<u8>>, } impl FragmentDeriveField { pub fn validate(&self) -> Result<(), Errors> { if self.flatten && self.recurse.is_some() { return Err(syn::Error::new( self.recurse.as_ref().unwrap().span(), "A field can't be recurse if it's being flattened", ) .into()); } Ok(()) } pub fn type_check_mode(&self) -> CheckMode { if self.flatten { CheckMode::Flattening } else if self.recurse.is_some() { CheckMode::Recursing } else { CheckMode::Normal } } } #[cfg(test)] mod tests { use super::*; use assert_matches::assert_matches; use quote::format_ident; #[test] fn test_fragment_derive_validate_pass() { let input = FragmentDeriveInput { ident: format_ident!("TestInput"), data: darling::ast::Data::Struct(darling::ast::Fields { style: darling::ast::Style::Struct, fields: vec![ FragmentDeriveField { ident: Some(format_ident!("field_one")), ty: syn::parse_quote! { String }, attrs: vec![], flatten: false, recurse: None, }, FragmentDeriveField { ident: Some(format_ident!("field_two")), ty: syn::parse_quote! { String }, attrs: vec![], flatten: true, recurse: None, }, FragmentDeriveField { ident: Some(format_ident!("field_three")), ty: syn::parse_quote! { String }, attrs: vec![], flatten: false, recurse: Some(8.into()), }, ], }), schema_path: "abcd".to_string().into(), query_module: "abcd".to_string().into(), graphql_type: "abcd".to_string().into(), argument_struct: None, }; assert_matches!(input.validate(), Ok(())); } #[test] fn test_fragment_derive_validate_fails() { let input = FragmentDeriveInput { ident: format_ident!("TestInput"), data: darling::ast::Data::Struct(darling::ast::Fields { style: darling::ast::Style::Struct, fields: vec![ FragmentDeriveField { ident: Some(format_ident!("field_one")), ty: syn::parse_quote! { String }, attrs: vec![], flatten: false, recurse: None, }, FragmentDeriveField { ident: Some(format_ident!("field_two")), ty: syn::parse_quote! { String }, attrs: vec![], flatten: true, recurse: Some(8.into()), }, FragmentDeriveField { ident: Some(format_ident!("field_three")), ty: syn::parse_quote! { String }, attrs: vec![], flatten: true, recurse: Some(8.into()), }, ], }), schema_path: "abcd".to_string().into(), query_module: "abcd".to_string().into(), graphql_type: "abcd".to_string().into(), argument_struct: None, }; let errors = input.validate().unwrap_err(); assert_eq!(errors.len(), 2); } }