@@ -3,14 +3,30 @@ extern crate proc_macro;
33/// Derive-related code. This will be moved into graphql_query_derive.
44mod attributes;
55
6- use anyhow:: Context ;
76use 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
1215use 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) ) ]
1531pub 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
2238fn 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
5367fn 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