conjure-codegen 0.5.0

Rust code generation for Conjure definitions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright 2019 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use proc_macro2::TokenStream;
use quote::quote;

use crate::context::Context;
use crate::types::{
    ArgumentDefinition, AuthType, EndpointDefinition, ParameterType, ServiceDefinition, Type,
};

#[derive(Copy, Clone)]
enum Style {
    Async,
    Sync,
}

pub fn generate(ctx: &Context, def: &ServiceDefinition) -> TokenStream {
    let async_ = generate_inner(ctx, def, Style::Async);
    let sync = generate_inner(ctx, def, Style::Sync);

    quote! {
        #async_

        #sync
    }
}

fn generate_inner(ctx: &Context, def: &ServiceDefinition, style: Style) -> TokenStream {
    let docs = ctx.docs(def.docs());
    let suffix = match style {
        Style::Async => "AsyncClient",
        Style::Sync => "Client",
    };
    let name = ctx.type_name(&format!("{}{}", def.service_name().name(), suffix));

    let client_bound = match style {
        Style::Async => quote!(AsyncClient),
        Style::Sync => quote!(Client),
    };

    let endpoints = def
        .endpoints()
        .iter()
        .map(|e| generate_endpoint(ctx, def, style, e));

    quote! {
        #docs
        #[derive(Clone, Debug)]
        pub struct #name<T>(T);

        impl<T> #name<T>
        where
            T: conjure_http::client::#client_bound,
        {
            /// Creates a new client.
            #[inline]
            pub fn new(client: T) -> #name<T> {
                #name(client)
            }

            #(#endpoints)*
        }
    }
}

fn generate_endpoint(
    ctx: &Context,
    def: &ServiceDefinition,
    style: Style,
    endpoint: &EndpointDefinition,
) -> TokenStream {
    let docs = ctx.docs(endpoint.docs());
    let deprecated = match endpoint.deprecated() {
        Some(docs) => {
            let docs = &**docs;
            quote! {
                #[deprecated(note = #docs)]
            }
        }
        None => quote!(),
    };

    let async_ = match style {
        Style::Async => quote!(async),
        Style::Sync => quote!(),
    };

    let name = ctx.field_name(endpoint.endpoint_name());

    let body_arg = body_arg(endpoint);
    let params = params(ctx, body_arg);

    let auth = quote!(auth_);
    let auth_arg = auth_arg(endpoint, &auth);
    let args = endpoint.args().iter().map(|a| {
        let name = ctx.field_name(a.arg_name());
        let ty = arg_type(ctx, def, a);
        quote!(#name: #ty)
    });

    let result = ctx.result_ident(def.service_name());
    let ret = return_type(ctx, endpoint);
    let ret_name = return_type_name(ctx, def, &ret);
    let where_ = where_(ctx, style, body_arg);

    let method = endpoint
        .http_method()
        .as_str()
        .parse::<TokenStream>()
        .unwrap();

    let path = &**endpoint.http_path();

    let path_params = quote!(path_params_);
    let setup_path_params = setup_path_params(ctx, endpoint, &path_params);

    let query_params = quote!(query_params_);
    let setup_query_params = setup_query_params(ctx, endpoint, &query_params);

    let headers = quote!(headers_);
    let setup_headers = setup_headers(ctx, endpoint, &headers, &auth);

    let body = quote!(body_);
    let setup_body = setup_body(ctx, body_arg, &body);

    let response_visitor = quote!(response_visitor_);
    let setup_response_visitor = setup_response_visitor(ctx, &ret, &response_visitor);

    let await_ = match style {
        Style::Async => quote!(.await),
        Style::Sync => quote!(),
    };

    quote! {
        #docs
        #deprecated
        pub #async_ fn #name #params(&self #auth_arg #(, #args)*) -> #result<#ret_name, conjure_http::private::Error>
        #where_
        {
            #setup_path_params
            #setup_query_params
            #setup_headers
            #setup_body
            #setup_response_visitor

            self.0.request(
                conjure_http::private::http::Method::#method,
                #path,
                #path_params,
                #query_params,
                #headers,
                #body,
                #response_visitor,
            )
            #await_
        }
    }
}

fn body_arg(endpoint: &EndpointDefinition) -> Option<&ArgumentDefinition> {
    endpoint.args().iter().find(|a| match a.param_type() {
        ParameterType::Body(_) => true,
        _ => false,
    })
}

fn params(ctx: &Context, body_arg: Option<&ArgumentDefinition>) -> TokenStream {
    match body_arg {
        Some(a) if ctx.is_binary(a.type_()) => quote!(<U>),
        _ => quote!(),
    }
}

fn where_(ctx: &Context, style: Style, body_arg: Option<&ArgumentDefinition>) -> TokenStream {
    match body_arg {
        Some(a) if ctx.is_binary(a.type_()) => {
            let bound = match style {
                Style::Async => {
                    quote!(conjure_http::client::AsyncWriteBody<T::BinaryWriter> + Sync + Send)
                }
                Style::Sync => quote!(conjure_http::client::WriteBody<T::BinaryWriter>),
            };
            quote!(where U: #bound,)
        }
        _ => quote!(),
    }
}

fn auth_arg(endpoint: &EndpointDefinition, auth: &TokenStream) -> TokenStream {
    match endpoint.auth() {
        Some(_) => quote!(, #auth: &conjure_object::BearerToken),
        None => quote!(),
    }
}

fn arg_type(ctx: &Context, def: &ServiceDefinition, arg: &ArgumentDefinition) -> TokenStream {
    if ctx.is_binary(arg.type_()) {
        quote!(U)
    } else {
        ctx.borrowed_rust_type(def.service_name(), arg.type_())
    }
}

fn return_type<'a>(ctx: &Context, endpoint: &'a EndpointDefinition) -> ReturnType<'a> {
    match endpoint.returns() {
        Some(ret) => match ctx.is_optional(ret) {
            Some(inner) if ctx.is_binary(inner) => ReturnType::OptionalBinary,
            _ if ctx.is_binary(ret) => ReturnType::Binary,
            _ => ReturnType::Json(ret),
        },
        None => ReturnType::None,
    }
}

fn return_type_name(ctx: &Context, def: &ServiceDefinition, ty: &ReturnType<'_>) -> TokenStream {
    match ty {
        ReturnType::None => quote!(()),
        ReturnType::Json(ty) => ctx.rust_type(def.service_name(), ty),
        ReturnType::Binary => quote!(T::BinaryBody),
        ReturnType::OptionalBinary => {
            let option = ctx.option_ident(def.service_name());
            quote!(#option<T::BinaryBody>)
        }
    }
}

fn setup_path_params(
    ctx: &Context,
    endpoint: &EndpointDefinition,
    path_params: &TokenStream,
) -> TokenStream {
    let mut parameters = vec![];

    for argument in endpoint.args() {
        match argument.param_type() {
            ParameterType::Path(_) => {}
            _ => continue,
        }

        let key = &**argument.arg_name();
        let name = ctx.field_name(key);

        let parameter = quote! {
            conjure_http::private::encode_path_param(&mut #path_params, #key, #name);
        };
        parameters.push(parameter);
    }

    let mutability = if parameters.is_empty() {
        quote!()
    } else {
        quote!(mut)
    };

    quote! {
        let #mutability #path_params = conjure_http::PathParams::new();
        #(#parameters)*
    }
}

fn setup_query_params(
    ctx: &Context,
    endpoint: &EndpointDefinition,
    query_params: &TokenStream,
) -> TokenStream {
    let mut parameters = vec![];

    for argument in endpoint.args() {
        let query = match argument.param_type() {
            ParameterType::Query(query) => query,
            _ => continue,
        };

        let key = &**query.param_id();
        let name = ctx.field_name(argument.arg_name());

        let parameter = if ctx.is_optional(argument.type_()).is_some() {
            quote! {
                conjure_http::private::encode_optional_query_param(
                    &mut #query_params,
                    #key,
                    &#name,
                );
            }
        } else if ctx.is_list(argument.type_()) {
            quote! {
                conjure_http::private::encode_list_query_param(
                    &mut #query_params,
                    #key,
                    &#name,
                );
            }
        } else if ctx.is_set(argument.type_()) {
            quote! {
                conjure_http::private::encode_set_query_param(
                    &mut #query_params,
                    #key,
                    &#name,
                );
            }
        } else {
            quote! {
                conjure_http::private::encode_query_param(
                    &mut #query_params,
                    #key,
                    #name,
                );
            }
        };
        parameters.push(parameter);
    }

    let mutability = if parameters.is_empty() {
        quote!()
    } else {
        quote!(mut)
    };

    quote! {
        let #mutability #query_params = conjure_http::QueryParams::new();
        #(#parameters)*
    }
}

fn setup_headers(
    ctx: &Context,
    endpoint: &EndpointDefinition,
    headers: &TokenStream,
    auth: &TokenStream,
) -> TokenStream {
    let mut parameters = vec![];

    if let Some(parameter) = auth_header(endpoint, headers, auth) {
        parameters.push(parameter);
    }

    for argument in endpoint.args() {
        let header = match argument.param_type() {
            ParameterType::Header(header) => header,
            _ => continue,
        };

        // HeaderName::from_static expects http2-style lowercased headers
        let header = header.param_id().to_lowercase();
        let param = &**argument.arg_name();
        let name = ctx.field_name(argument.arg_name());

        let parameter = if ctx.is_optional(argument.type_()).is_some() {
            quote! {
                conjure_http::private::encode_optional_header(
                    &mut #headers,
                    #param,
                    #header,
                    &#name,
                )?;
            }
        } else {
            quote! {
                conjure_http::private::encode_header(
                    &mut #headers,
                    #param,
                    #header,
                    #name,
                )?;
            }
        };

        parameters.push(parameter);
    }

    let mutability = if parameters.is_empty() {
        quote!()
    } else {
        quote!(mut)
    };

    quote! {
        let #mutability #headers = conjure_http::private::http::HeaderMap::new();
        #(#parameters)*
    }
}

fn auth_header(
    endpoint: &EndpointDefinition,
    headers: &TokenStream,
    auth: &TokenStream,
) -> Option<TokenStream> {
    match endpoint.auth() {
        Some(AuthType::Cookie(cookie)) => {
            let prefix = format!("{}=", cookie.cookie_name());
            Some(quote! {
                conjure_http::private::encode_cookie_auth(&mut #headers, #prefix, #auth);
            })
        }
        Some(AuthType::Header(_)) => Some(quote! {
            conjure_http::private::encode_header_auth(&mut #headers, #auth);
        }),
        None => None,
    }
}

fn setup_body(
    ctx: &Context,
    body_arg: Option<&ArgumentDefinition>,
    body: &TokenStream,
) -> TokenStream {
    let expr = match body_arg {
        Some(body_arg) => {
            let name = ctx.field_name(body_arg.arg_name());
            if ctx.is_binary(body_arg.type_()) {
                quote! {
                    conjure_http::private::BinaryRequestBody(#name)
                }
            } else {
                quote! {
                    conjure_http::private::SerializableRequestBody(#name)
                }
            }
        }
        None => {
            quote! {
                conjure_http::private::EmptyRequestBody
            }
        }
    };

    quote! {
        let #body = #expr;
    }
}

fn setup_response_visitor(
    ctx: &Context,
    ty: &ReturnType<'_>,
    response_visitor: &TokenStream,
) -> TokenStream {
    let visitor = match ty {
        ReturnType::None => quote!(EmptyResponseVisitor),
        ReturnType::Json(ty) => {
            if ctx.is_iterable(ty) {
                quote!(DefaultSerializableResponseVisitor::new())
            } else {
                quote!(SerializableResponseVisitor::new())
            }
        }
        ReturnType::Binary => quote!(BinaryResponseVisitor),
        ReturnType::OptionalBinary => quote!(OptionalBinaryResponseVisitor),
    };

    quote! {
        let #response_visitor = conjure_http::private::#visitor;
    }
}

enum ReturnType<'a> {
    None,
    Json(&'a Type),
    Binary,
    OptionalBinary,
}