@@ -19,14 +19,40 @@ use crate::types::{
19
19
ArgumentDefinition , AuthType , EndpointDefinition , ParameterType , ServiceDefinition , Type ,
20
20
} ;
21
21
22
+ #[ derive( Copy , Clone ) ]
23
+ enum Style {
24
+ Async ,
25
+ Sync ,
26
+ }
27
+
22
28
pub fn generate ( ctx : & Context , def : & ServiceDefinition ) -> TokenStream {
29
+ let async_ = generate_inner ( ctx, def, Style :: Async ) ;
30
+ let sync = generate_inner ( ctx, def, Style :: Sync ) ;
31
+
32
+ quote ! {
33
+ #async_
34
+
35
+ #sync
36
+ }
37
+ }
38
+
39
+ fn generate_inner ( ctx : & Context , def : & ServiceDefinition , style : Style ) -> TokenStream {
23
40
let docs = ctx. docs ( def. docs ( ) ) ;
24
- let name = ctx. type_name ( & format ! ( "{}Client" , def. service_name( ) . name( ) ) ) ;
41
+ let suffix = match style {
42
+ Style :: Async => "AsyncClient" ,
43
+ Style :: Sync => "Client" ,
44
+ } ;
45
+ let name = ctx. type_name ( & format ! ( "{}{}" , def. service_name( ) . name( ) , suffix) ) ;
46
+
47
+ let client_bound = match style {
48
+ Style :: Async => quote ! ( AsyncClient ) ,
49
+ Style :: Sync => quote ! ( Client ) ,
50
+ } ;
25
51
26
52
let endpoints = def
27
53
. endpoints ( )
28
54
. iter ( )
29
- . map ( |e| generate_endpoint ( ctx, def, e) ) ;
55
+ . map ( |e| generate_endpoint ( ctx, def, style , e) ) ;
30
56
31
57
quote ! {
32
58
#docs
@@ -35,7 +61,7 @@ pub fn generate(ctx: &Context, def: &ServiceDefinition) -> TokenStream {
35
61
36
62
impl <T > #name<T >
37
63
where
38
- T : conjure_http:: client:: Client
64
+ T : conjure_http:: client:: #client_bound ,
39
65
{
40
66
/// Creates a new client.
41
67
#[ inline]
@@ -51,6 +77,7 @@ pub fn generate(ctx: &Context, def: &ServiceDefinition) -> TokenStream {
51
77
fn generate_endpoint (
52
78
ctx : & Context ,
53
79
def : & ServiceDefinition ,
80
+ style : Style ,
54
81
endpoint : & EndpointDefinition ,
55
82
) -> TokenStream {
56
83
let docs = ctx. docs ( endpoint. docs ( ) ) ;
@@ -63,6 +90,12 @@ fn generate_endpoint(
63
90
}
64
91
None => quote ! ( ) ,
65
92
} ;
93
+
94
+ let async_ = match style {
95
+ Style :: Async => quote ! ( async ) ,
96
+ Style :: Sync => quote ! ( ) ,
97
+ } ;
98
+
66
99
let name = ctx. field_name ( endpoint. endpoint_name ( ) ) ;
67
100
68
101
let body_arg = body_arg ( endpoint) ;
@@ -79,7 +112,7 @@ fn generate_endpoint(
79
112
let result = ctx. result_ident ( def. service_name ( ) ) ;
80
113
let ret = return_type ( ctx, endpoint) ;
81
114
let ret_name = return_type_name ( ctx, def, & ret) ;
82
- let where_ = where_ ( ctx, body_arg) ;
115
+ let where_ = where_ ( ctx, style , body_arg) ;
83
116
84
117
let method = endpoint
85
118
. http_method ( )
@@ -104,10 +137,15 @@ fn generate_endpoint(
104
137
let response_visitor = quote ! ( response_visitor_) ;
105
138
let setup_response_visitor = setup_response_visitor ( ctx, & ret, & response_visitor) ;
106
139
140
+ let await_ = match style {
141
+ Style :: Async => quote ! ( . await ) ,
142
+ Style :: Sync => quote ! ( ) ,
143
+ } ;
144
+
107
145
quote ! {
108
146
#docs
109
147
#deprecated
110
- pub fn #name #params( & self #auth_arg #( , #args) * ) -> #result<#ret_name, conjure_http:: private:: Error >
148
+ pub #async_ fn #name #params( & self #auth_arg #( , #args) * ) -> #result<#ret_name, conjure_http:: private:: Error >
111
149
#where_
112
150
{
113
151
#setup_path_params
@@ -125,6 +163,7 @@ fn generate_endpoint(
125
163
#body,
126
164
#response_visitor,
127
165
)
166
+ #await_
128
167
}
129
168
}
130
169
}
@@ -143,10 +182,16 @@ fn params(ctx: &Context, body_arg: Option<&ArgumentDefinition>) -> TokenStream {
143
182
}
144
183
}
145
184
146
- fn where_ ( ctx : & Context , body_arg : Option < & ArgumentDefinition > ) -> TokenStream {
185
+ fn where_ ( ctx : & Context , style : Style , body_arg : Option < & ArgumentDefinition > ) -> TokenStream {
147
186
match body_arg {
148
187
Some ( a) if ctx. is_binary ( a. type_ ( ) ) => {
149
- quote ! ( where U : conjure_http:: client:: WriteBody <T :: BinaryWriter >)
188
+ let bound = match style {
189
+ Style :: Async => {
190
+ quote ! ( conjure_http:: client:: AsyncWriteBody <T :: BinaryWriter > + Sync + Send )
191
+ }
192
+ Style :: Sync => quote ! ( conjure_http:: client:: WriteBody <T :: BinaryWriter >) ,
193
+ } ;
194
+ quote ! ( where U : #bound, )
150
195
}
151
196
_ => quote ! ( ) ,
152
197
}
0 commit comments