|
| 1 | +use failure; |
| 2 | +use graphql_client_codegen::deprecation::DeprecationStrategy; |
| 3 | +use syn; |
| 4 | + |
| 5 | +const DEPRECATION_ERROR: &'static str = "deprecated must be one of 'allow', 'deny', or 'warn'"; |
| 6 | + |
| 7 | +/// The `graphql` attribute as a `syn::Path`. |
| 8 | +fn path_to_match() -> syn::Path { |
| 9 | + syn::parse_str("graphql").expect("`graphql` is a valid path") |
| 10 | +} |
| 11 | + |
| 12 | +/// Extract an configuration parameter specified in the `graphql` attribute. |
| 13 | +pub fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result<String, failure::Error> { |
| 14 | + let attributes = &ast.attrs; |
| 15 | + let graphql_path = path_to_match(); |
| 16 | + let attribute = attributes |
| 17 | + .iter() |
| 18 | + .find(|attr| attr.path == graphql_path) |
| 19 | + .ok_or_else(|| format_err!("The graphql attribute is missing"))?; |
| 20 | + if let syn::Meta::List(items) = &attribute |
| 21 | + .interpret_meta() |
| 22 | + .expect("Attribute is well formatted") |
| 23 | + { |
| 24 | + for item in items.nested.iter() { |
| 25 | + if let syn::NestedMeta::Meta(syn::Meta::NameValue(name_value)) = item { |
| 26 | + let syn::MetaNameValue { ident, lit, .. } = name_value; |
| 27 | + if ident == attr { |
| 28 | + if let syn::Lit::Str(lit) = lit { |
| 29 | + return Ok(lit.value()); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + Err(format_err!("attribute not found"))? |
| 37 | +} |
| 38 | + |
| 39 | +/// Get the deprecation from a struct attribute in the derive case. |
| 40 | +pub fn extract_deprecation_strategy( |
| 41 | + ast: &syn::DeriveInput, |
| 42 | +) -> Result<DeprecationStrategy, failure::Error> { |
| 43 | + match extract_attr(&ast, "deprecated")?.to_lowercase().as_str() { |
| 44 | + "allow" => Ok(DeprecationStrategy::Allow), |
| 45 | + "deny" => Ok(DeprecationStrategy::Deny), |
| 46 | + "warn" => Ok(DeprecationStrategy::Warn), |
| 47 | + _ => Err(format_err!("{}", DEPRECATION_ERROR))?, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[cfg(test)] |
| 52 | +mod test { |
| 53 | + use super::*; |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn test_deprecation_strategy() { |
| 57 | + let input = " |
| 58 | + #[derive(GraphQLQuery)] |
| 59 | + #[graphql( |
| 60 | + schema_path = \"x\", |
| 61 | + query_path = \"x\", |
| 62 | + deprecated = \"warn\", |
| 63 | + )] |
| 64 | + struct MyQuery; |
| 65 | + "; |
| 66 | + let parsed = syn::parse_str(input).unwrap(); |
| 67 | + assert_eq!( |
| 68 | + extract_deprecation_strategy(&parsed).unwrap(), |
| 69 | + DeprecationStrategy::Warn |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn test_deprecation_strategy_is_case_insensitive() { |
| 75 | + let input = " |
| 76 | + #[derive(GraphQLQuery)] |
| 77 | + #[graphql( |
| 78 | + schema_path = \"x\", |
| 79 | + query_path = \"x\", |
| 80 | + deprecated = \"DeNy\", |
| 81 | + )] |
| 82 | + struct MyQuery; |
| 83 | + "; |
| 84 | + let parsed = syn::parse_str(input).unwrap(); |
| 85 | + assert_eq!( |
| 86 | + extract_deprecation_strategy(&parsed).unwrap(), |
| 87 | + DeprecationStrategy::Deny |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + #[test] |
| 92 | + fn test_invalid_deprecation_strategy() { |
| 93 | + let input = " |
| 94 | + #[derive(GraphQLQuery)] |
| 95 | + #[graphql( |
| 96 | + schema_path = \"x\", |
| 97 | + query_path = \"x\", |
| 98 | + deprecated = \"foo\", |
| 99 | + )] |
| 100 | + struct MyQuery; |
| 101 | + "; |
| 102 | + let parsed = syn::parse_str(input).unwrap(); |
| 103 | + match extract_deprecation_strategy(&parsed) { |
| 104 | + Ok(_) => panic!("parsed unexpectedly"), |
| 105 | + Err(e) => assert_eq!(&format!("{}", e), DEPRECATION_ERROR), |
| 106 | + }; |
| 107 | + } |
| 108 | +} |
0 commit comments