Skip to content

Commit fd4a154

Browse files
committed
Drop anyhow dependency in graphql_query_derive
1 parent 499d824 commit fd4a154

File tree

6 files changed

+41
-27
lines changed

6 files changed

+41
-27
lines changed

graphql_client_cli/src/generate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<()> {
5959
options.set_deprecation_strategy(deprecation_strategy);
6060
}
6161

62-
let gen = generate_module_token_stream(query_path.clone(), &schema_path, options)?;
62+
let gen = generate_module_token_stream(query_path.clone(), &schema_path, options).unwrap();
6363

6464
let generated_code = gen.to_string();
6565
let generated_code = if cfg!(feature = "rustfmt") && !no_formatting {

graphql_client_cli/src/introspect_schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::*;
12
use graphql_client::GraphQLQuery;
23
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE};
34
use std::path::PathBuf;

graphql_client_codegen/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use thiserror::Error;
3636
#[error("{0}")]
3737
struct GeneralError(String);
3838

39-
type BoxError = Box<dyn std::error::Error + 'static>;
39+
type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
4040
type CacheMap<T> = std::sync::Mutex<HashMap<std::path::PathBuf, T>>;
4141

4242
lazy_static! {

graphql_query_derive/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ edition = "2018"
1111
proc-macro = true
1212

1313
[dependencies]
14-
anyhow = "1.0"
1514
syn = { version = "^1.0", features = ["extra-traits"] }
1615
proc-macro2 = { version = "^1.0", features = [] }
1716
graphql_client_codegen = { path = "../graphql_client_codegen/", version = "0.9.0" }

graphql_query_derive/src/attributes.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::*;
1+
use crate::{BoxError, GeneralError};
22
use graphql_client_codegen::deprecation::DeprecationStrategy;
33
use graphql_client_codegen::normalization::Normalization;
44

@@ -11,13 +11,13 @@ fn path_to_match() -> syn::Path {
1111
}
1212

1313
/// Extract an configuration parameter specified in the `graphql` attribute.
14-
pub fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result<String, anyhow::Error> {
14+
pub fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result<String, syn::Error> {
1515
let attributes = &ast.attrs;
1616
let graphql_path = path_to_match();
1717
let attribute = attributes
1818
.iter()
1919
.find(|attr| attr.path == graphql_path)
20-
.ok_or_else(|| format_err!("The graphql attribute is missing"))?;
20+
.ok_or_else(|| syn::Error::new_spanned(ast, "The graphql attribute is missing"))?;
2121
if let syn::Meta::List(items) = &attribute.parse_meta().expect("Attribute is well formatted") {
2222
for item in items.nested.iter() {
2323
if let syn::NestedMeta::Meta(syn::Meta::NameValue(name_value)) = item {
@@ -33,27 +33,27 @@ pub fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result<String, anyhow
3333
}
3434
}
3535

36-
Err(format_err!("attribute not found"))
36+
Err(syn::Error::new_spanned(ast, "Attribute not found"))
3737
}
3838

3939
/// Get the deprecation from a struct attribute in the derive case.
4040
pub fn extract_deprecation_strategy(
4141
ast: &syn::DeriveInput,
42-
) -> Result<DeprecationStrategy, anyhow::Error> {
42+
) -> Result<DeprecationStrategy, BoxError> {
4343
extract_attr(&ast, "deprecated")?
4444
.to_lowercase()
4545
.as_str()
4646
.parse()
47-
.map_err(|_| format_err!("{}", DEPRECATION_ERROR))
47+
.map_err(|_| GeneralError(DEPRECATION_ERROR.to_owned()).into())
4848
}
4949

5050
/// Get the deprecation from a struct attribute in the derive case.
51-
pub fn extract_normalization(ast: &syn::DeriveInput) -> Result<Normalization, anyhow::Error> {
51+
pub fn extract_normalization(ast: &syn::DeriveInput) -> Result<Normalization, BoxError> {
5252
extract_attr(&ast, "normalization")?
5353
.to_lowercase()
5454
.as_str()
5555
.parse()
56-
.map_err(|_| format_err!("{}", NORMALIZATION_ERROR))
56+
.map_err(|_| GeneralError(NORMALIZATION_ERROR.to_owned()).into())
5757
}
5858

5959
#[cfg(test)]

graphql_query_derive/src/lib.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,30 @@ extern crate proc_macro;
33
/// Derive-related code. This will be moved into graphql_query_derive.
44
mod attributes;
55

6-
use anyhow::Context;
76
use graphql_client_codegen::{
87
generate_module_token_stream, CodegenMode, GraphQLClientCodegenOptions,
98
};
10-
use std::path::{Path, PathBuf};
9+
use std::{
10+
env,
11+
fmt::Display,
12+
path::{Path, PathBuf},
13+
};
1114

1215
use proc_macro2::TokenStream;
1316

17+
type BoxError = Box<dyn std::error::Error + 'static>;
18+
19+
#[derive(Debug)]
20+
struct GeneralError(String);
21+
22+
impl Display for GeneralError {
23+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24+
f.write_str(&self.0)
25+
}
26+
}
27+
28+
impl std::error::Error for GeneralError {}
29+
1430
#[proc_macro_derive(GraphQLQuery, attributes(graphql))]
1531
pub fn derive_graphql_query(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
1632
match graphql_query_derive_inner(input) {
@@ -21,39 +37,37 @@ pub fn derive_graphql_query(input: proc_macro::TokenStream) -> proc_macro::Token
2137

2238
fn graphql_query_derive_inner(
2339
input: proc_macro::TokenStream,
24-
) -> Result<proc_macro::TokenStream, anyhow::Error> {
40+
) -> Result<proc_macro::TokenStream, BoxError> {
2541
let input = TokenStream::from(input);
26-
let ast = syn::parse2(input).expect("derive input parsing");
27-
// .context("Derive input parsing.")?;
42+
let ast = syn::parse2(input)?;
2843
let (query_path, schema_path) = build_query_and_schema_path(&ast)?;
2944
let options = build_graphql_client_derive_options(&ast, query_path.clone())?;
3045
Ok(
3146
generate_module_token_stream(query_path, &schema_path, options)
3247
.map(Into::into)
33-
.context("Code generation failed.")?,
48+
.map_err(|err| GeneralError(format!("Code generation failed: {}", err)))?,
3449
)
3550
}
3651

37-
fn build_query_and_schema_path(
38-
input: &syn::DeriveInput,
39-
) -> Result<(PathBuf, PathBuf), anyhow::Error> {
40-
let cargo_manifest_dir = ::std::env::var("CARGO_MANIFEST_DIR")
41-
.context("Checking that the CARGO_MANIFEST_DIR env variable is defined.")?;
52+
fn build_query_and_schema_path(input: &syn::DeriveInput) -> Result<(PathBuf, PathBuf), BoxError> {
53+
let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").map_err(|_err| {
54+
GeneralError("Checking that the CARGO_MANIFEST_DIR env variable is defined.".into())
55+
})?;
4256

43-
let query_path =
44-
attributes::extract_attr(input, "query_path").context("Extracting query path.")?;
57+
let query_path = attributes::extract_attr(input, "query_path")
58+
.map_err(|err| GeneralError(format!("Error extracting query path. {}", err)))?;
4559
let query_path = format!("{}/{}", cargo_manifest_dir, query_path);
4660
let query_path = Path::new(&query_path).to_path_buf();
47-
let schema_path =
48-
attributes::extract_attr(input, "schema_path").context("Extracting schema path.")?;
61+
let schema_path = attributes::extract_attr(input, "schema_path")
62+
.map_err(|err| GeneralError(format!("Error extracting schema path. {}", err)))?;
4963
let schema_path = Path::new(&cargo_manifest_dir).join(schema_path);
5064
Ok((query_path, schema_path))
5165
}
5266

5367
fn build_graphql_client_derive_options(
5468
input: &syn::DeriveInput,
5569
query_path: PathBuf,
56-
) -> Result<GraphQLClientCodegenOptions, anyhow::Error> {
70+
) -> Result<GraphQLClientCodegenOptions, BoxError> {
5771
let variables_derives = attributes::extract_attr(input, "variables_derives").ok();
5872
let response_derives = attributes::extract_attr(input, "response_derives").ok();
5973

0 commit comments

Comments
 (0)