@@ -3,14 +3,30 @@ extern crate proc_macro;
3
3
/// Derive-related code. This will be moved into graphql_query_derive.
4
4
mod attributes;
5
5
6
- use anyhow:: Context ;
7
6
use graphql_client_codegen:: {
8
7
generate_module_token_stream, CodegenMode , GraphQLClientCodegenOptions ,
9
8
} ;
10
- use std:: path:: { Path , PathBuf } ;
9
+ use std:: {
10
+ env,
11
+ fmt:: Display ,
12
+ path:: { Path , PathBuf } ,
13
+ } ;
11
14
12
15
use proc_macro2:: TokenStream ;
13
16
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
+
14
30
#[ proc_macro_derive( GraphQLQuery , attributes( graphql) ) ]
15
31
pub fn derive_graphql_query ( input : proc_macro:: TokenStream ) -> proc_macro:: TokenStream {
16
32
match graphql_query_derive_inner ( input) {
@@ -21,39 +37,37 @@ pub fn derive_graphql_query(input: proc_macro::TokenStream) -> proc_macro::Token
21
37
22
38
fn graphql_query_derive_inner (
23
39
input : proc_macro:: TokenStream ,
24
- ) -> Result < proc_macro:: TokenStream , anyhow :: Error > {
40
+ ) -> Result < proc_macro:: TokenStream , BoxError > {
25
41
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) ?;
28
43
let ( query_path, schema_path) = build_query_and_schema_path ( & ast) ?;
29
44
let options = build_graphql_client_derive_options ( & ast, query_path. clone ( ) ) ?;
30
45
Ok (
31
46
generate_module_token_stream ( query_path, & schema_path, options)
32
47
. map ( Into :: into)
33
- . context ( "Code generation failed." ) ?,
48
+ . map_err ( |err| GeneralError ( format ! ( "Code generation failed: {}" , err ) ) ) ?,
34
49
)
35
50
}
36
51
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
+ } ) ?;
42
56
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 ) ) ) ?;
45
59
let query_path = format ! ( "{}/{}" , cargo_manifest_dir, query_path) ;
46
60
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 ) ) ) ?;
49
63
let schema_path = Path :: new ( & cargo_manifest_dir) . join ( schema_path) ;
50
64
Ok ( ( query_path, schema_path) )
51
65
}
52
66
53
67
fn build_graphql_client_derive_options (
54
68
input : & syn:: DeriveInput ,
55
69
query_path : PathBuf ,
56
- ) -> Result < GraphQLClientCodegenOptions , anyhow :: Error > {
70
+ ) -> Result < GraphQLClientCodegenOptions , BoxError > {
57
71
let variables_derives = attributes:: extract_attr ( input, "variables_derives" ) . ok ( ) ;
58
72
let response_derives = attributes:: extract_attr ( input, "response_derives" ) . ok ( ) ;
59
73
0 commit comments