Skip to content

Commit d3dbf8c

Browse files
authored
Merge pull request #187 from graphql-rust/remove-quote-from-derive-crate
Move derive-specific code out of graphql_client_codegen
2 parents e7985fc + 935912a commit d3dbf8c

File tree

8 files changed

+117
-117
lines changed

8 files changed

+117
-117
lines changed

graphql_client_cli/src/generate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ pub fn generate_code(
5353

5454
if cfg!(feature = "rustfmt") && !no_formatting {
5555
let codes = format(&codes);
56-
write!(file, "{}", codes);
56+
write!(file, "{}", codes)?;
5757
} else {
58-
write!(file, "{}", codes);
58+
write!(file, "{}", codes)?;
5959
}
6060

6161
Ok(())

graphql_client_codegen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ failure = "0.1"
1111
itertools = "0.7"
1212
lazy_static = "1.0"
1313
quote = "0.6"
14-
syn = "0.15"
14+
syn = "0.15.20"
1515
proc-macro2 = { version = "0.4", features = [] }
1616
serde = "^1.0.78"
1717
serde_derive = "1.0"

graphql_client_codegen/src/attributes.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
use attributes;
2-
use failure;
3-
use syn;
4-
5-
static DEPRECATION_ERROR: &'static str = "deprecated must be one of 'allow', 'deny', or 'warn'";
6-
71
/// Whether an item is deprecated, with context.
82
#[derive(Debug, PartialEq, Hash, Clone)]
93
pub enum DeprecationStatus {
@@ -29,77 +23,3 @@ impl Default for DeprecationStrategy {
2923
DeprecationStrategy::Warn
3024
}
3125
}
32-
33-
/// Get the deprecation from a struct attribute in the derive case.
34-
pub fn extract_deprecation_strategy(
35-
ast: &syn::DeriveInput,
36-
) -> Result<DeprecationStrategy, failure::Error> {
37-
match attributes::extract_attr(&ast, "deprecated")?
38-
.to_lowercase()
39-
.as_str()
40-
{
41-
"allow" => Ok(DeprecationStrategy::Allow),
42-
"deny" => Ok(DeprecationStrategy::Deny),
43-
"warn" => Ok(DeprecationStrategy::Warn),
44-
_ => Err(format_err!("{}", DEPRECATION_ERROR))?,
45-
}
46-
}
47-
48-
#[cfg(test)]
49-
mod test {
50-
use super::*;
51-
52-
#[test]
53-
fn test_deprecation_strategy() {
54-
let input = "
55-
#[derive(GraphQLQuery)]
56-
#[graphql(
57-
schema_path = \"x\",
58-
query_path = \"x\",
59-
deprecated = \"warn\",
60-
)]
61-
struct MyQuery;
62-
";
63-
let parsed = syn::parse_str(input).unwrap();
64-
assert_eq!(
65-
extract_deprecation_strategy(&parsed).unwrap(),
66-
DeprecationStrategy::Warn
67-
);
68-
}
69-
70-
#[test]
71-
fn test_deprecation_strategy_is_case_insensitive() {
72-
let input = "
73-
#[derive(GraphQLQuery)]
74-
#[graphql(
75-
schema_path = \"x\",
76-
query_path = \"x\",
77-
deprecated = \"DeNy\",
78-
)]
79-
struct MyQuery;
80-
";
81-
let parsed = syn::parse_str(input).unwrap();
82-
assert_eq!(
83-
extract_deprecation_strategy(&parsed).unwrap(),
84-
DeprecationStrategy::Deny
85-
);
86-
}
87-
88-
#[test]
89-
fn test_invalid_deprecation_strategy() {
90-
let input = "
91-
#[derive(GraphQLQuery)]
92-
#[graphql(
93-
schema_path = \"x\",
94-
query_path = \"x\",
95-
deprecated = \"foo\",
96-
)]
97-
struct MyQuery;
98-
";
99-
let parsed = syn::parse_str(input).unwrap();
100-
match extract_deprecation_strategy(&parsed) {
101-
Ok(_) => panic!("parsed unexpectedly"),
102-
Err(e) => assert_eq!(&format!("{}", e), DEPRECATION_ERROR),
103-
};
104-
}
105-
}

graphql_client_codegen/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ extern crate quote;
2525
use proc_macro2::TokenStream;
2626
use syn::Visibility;
2727

28-
/// Derive-related code. This will be moved into graphql_query_derive.
29-
pub mod attributes;
3028
mod codegen;
3129
/// Deprecation-related code
3230
pub mod deprecation;

graphql_query_derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ proc-macro = true
1111

1212
[dependencies]
1313
failure = "0.1"
14-
syn = "0.15"
14+
syn = "0.15.20"
1515
proc-macro2 = { version = "0.4", features = [] }
1616
graphql_client_codegen = { path = "../graphql_client_codegen/", version = "0.5.1" }
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

graphql_query_derive/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
#[macro_use]
12
extern crate failure;
23
extern crate graphql_client_codegen;
34
extern crate proc_macro;
45
extern crate proc_macro2;
56
extern crate syn;
67

8+
/// Derive-related code. This will be moved into graphql_query_derive.
9+
mod attributes;
10+
711
use failure::ResultExt;
812
use graphql_client_codegen::*;
913

@@ -40,7 +44,7 @@ fn build_query_and_schema_path(
4044
fn build_graphql_client_derive_options(input: &syn::DeriveInput) -> GraphQLClientDeriveOptions {
4145
let response_derives = attributes::extract_attr(input, "response_derives").ok();
4246
// The user can determine what to do about deprecations.
43-
let deprecation_strategy = deprecation::extract_deprecation_strategy(input).unwrap_or_default();
47+
let deprecation_strategy = attributes::extract_deprecation_strategy(input).unwrap_or_default();
4448

4549
let selected_operation_name = attributes::extract_attr(input, "selected_operation")
4650
.context("Extracting selected operation name");

0 commit comments

Comments
 (0)