shuttle-codegen 0.40.0

Proc-macro code generator for the shuttle.rs service
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
use std::collections::HashMap;

use proc_macro_error::emit_error;
use quote::{quote, ToTokens};
use syn::{
    parse::Parse, punctuated::Punctuated, Expr, ExprLit, File, Ident, Item, ItemFn, Lit, LitStr,
    Token,
};

#[derive(Debug, Eq, PartialEq)]
struct Endpoint {
    route: LitStr,
    method: Ident,
    function: Ident,
}

#[derive(Debug, Eq, PartialEq)]
struct Parameter {
    key: Ident,
    equals: Token![=],
    value: Expr,
}

#[derive(Debug, Eq, PartialEq)]
struct Params {
    params: Punctuated<Parameter, Token![,]>,
}

impl Parse for Parameter {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        Ok(Self {
            key: input.parse()?,
            equals: input.parse()?,
            value: input.parse()?,
        })
    }
}

impl Parse for Params {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        Ok(Self {
            params: input.parse_terminated(Parameter::parse, Token![,])?,
        })
    }
}

impl Endpoint {
    fn from_item_fn(item: &mut ItemFn) -> Option<Self> {
        let function = item.sig.ident.clone();

        let mut endpoint_index = None;

        // Find the index of an attribute that is an endpoint
        for index in 0..item.attrs.len() {
            // The endpoint ident should be the last segment in the path
            if let Some(segment) = item.attrs[index].path().segments.last() {
                if segment.ident.to_string().as_str() == "endpoint" {
                    // TODO: we should allow multiple endpoint attributes per handler.
                    // We could refactor this to return a Vec<Endpoint> and then check
                    // that the combination of endpoints is valid.
                    if endpoint_index.is_some() {
                        emit_error!(
                            item,
                            "extra endpoint attribute";
                            hint = "There should only be one endpoint annotation per handler function."
                        );
                        return None;
                    }
                    endpoint_index = Some(index);
                }
            } else {
                return None;
            }
        }

        // Strip the endpoint attribute if it exists
        let endpoint = if let Some(index) = endpoint_index {
            item.attrs.remove(index)
        } else {
            // This item does not have an endpoint attribute
            return None;
        };

        // Parse the endpoint's parameters
        let params: Params = match endpoint.parse_args() {
            Ok(params) => params,
            Err(err) => {
                // This will error on invalid parameter syntax
                emit_error!(err.span(), err);
                return None;
            }
        };

        // We'll use the paren span for errors later
        let endpoint_delim_span = &endpoint
            .meta
            .require_list()
            .expect("Endpoint meta should be a list")
            .delimiter
            .span()
            .join();

        if params.params.is_empty() {
            emit_error!(
                endpoint_delim_span,
                "missing endpoint arguments";
                hint = "The endpoint takes two arguments: `endpoint(method = get, route = \"/hello\")`"
            );
            return None;
        }

        // At this point an endpoint with params and valid syntax exists, so we will check for
        // all errors before returning
        let mut has_err = false;

        let mut route = None;
        let mut method = None;

        for Parameter { key, value, .. } in params.params {
            let key_ident = key.clone();
            match key.to_string().as_str() {
                "method" => {
                    if method.is_some() {
                        emit_error!(
                            key_ident,
                            "duplicate endpoint method";
                            hint = "The endpoint `method` should only be set once."
                        );
                        has_err = true;
                    }
                    if let Expr::Path(path) = value {
                        let method_ident = path.path.segments[0].ident.clone();

                        match method_ident.to_string().as_str() {
                            "get" | "post" | "delete" | "put" | "options" | "head" | "trace"
                            | "patch" => {
                                method = Some(method_ident);
                            }
                            _ => {
                                emit_error!(
                                    method_ident,
                                    "method is not supported";
                                    hint = "Try one of the following: `get`, `post`, `delete`, `put`, `options`, `head`, `trace` or `patch`"
                                );
                                has_err = true;
                            }
                        };
                    };
                }
                "route" => {
                    if route.is_some() {
                        emit_error!(
                            key_ident,
                            "duplicate endpoint route";
                            hint = "The endpoint `route` should only be set once."
                        );
                        has_err = true;
                    }

                    if let Expr::Lit(ExprLit {
                        lit: Lit::Str(literal),
                        ..
                    }) = value
                    {
                        route = Some(literal);
                    }
                }
                _ => {
                    emit_error!(
                        key_ident,
                        "invalid endpoint argument";
                        hint = "Only `method` and `route` are valid endpoint arguments."
                    );
                    has_err = true;
                }
            }
        }

        if route.is_none() {
            emit_error!(
                endpoint_delim_span,
                "no route provided";
                hint = "Add a route to your endpoint: `route = \"/hello\")`"
            );
            has_err = true;
        };

        if method.is_none() {
            emit_error!(
                endpoint_delim_span,
                "no method provided";
                hint = "Add a method to your endpoint: `method = get`"
            );
            has_err = true;
        };

        if has_err {
            None
        } else {
            // Safe to unwrap because `has_err` is true if `route` or `method` is `None`
            Some(Endpoint {
                route: route.unwrap(),
                method: method.unwrap(),
                function,
            })
        }
    }
}

#[derive(Debug, Eq, PartialEq)]
pub struct EndpointChain<'a> {
    route: &'a LitStr,
    handlers: Vec<Handler>,
}

#[derive(Debug, Eq, PartialEq)]
struct Handler {
    method: Ident,
    function: Ident,
}

impl ToTokens for Endpoint {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let Self {
            route,
            method,
            function,
        } = self;

        let route = quote!(.route(#route, #method(#function)));

        route.to_tokens(tokens);
    }
}

impl ToTokens for Handler {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let Self { method, function } = self;

        let handler = quote!(#method(#function));

        handler.to_tokens(tokens);
    }
}

impl<'a> ToTokens for EndpointChain<'a> {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let Self { route, handlers } = self;

        let route = quote!(.route(#route, shuttle_next::routing::#(#handlers).*));

        route.to_tokens(tokens);
    }
}

#[derive(Debug, Eq, PartialEq)]
pub(crate) struct App {
    endpoints: Vec<Endpoint>,
}

impl App {
    pub(crate) fn from_file(file: &mut File) -> Self {
        let endpoints = file
            .items
            .iter_mut()
            .filter_map(|item| {
                if let Item::Fn(item_fn) = item {
                    Some(item_fn)
                } else {
                    None
                }
            })
            .filter_map(Endpoint::from_item_fn)
            .collect();

        Self { endpoints }
    }
}

impl ToTokens for App {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let Self { endpoints } = self;

        let mut endpoint_chains = endpoints
            .iter()
            .fold(HashMap::new(), |mut chain, endpoint| {
                let entry: &mut Vec<Handler> = chain
                    .entry(&endpoint.route)
                    .or_default();

                let method = endpoint.method.clone();
                let function = endpoint.function.clone();

                if entry.iter().any(|handler| handler.method == method) {
                    emit_error!(
                        method,
                        "only one method of each type is allowed per route";
                        hint = format!("Remove one of the {} methods on the \"{}\" route.", method, endpoint.route.value())
                    );
                } else {
                    entry.push(Handler { method, function });
                }

                chain
            })
            .into_iter()
            .map(|(key, value)| EndpointChain {
                route: key,
                handlers: value,
            })
            .collect::<Vec<EndpointChain>>();

        // syn::LitStr does not implement Ord, so rather than using a BTreeMap to build the chains, we
        // use a HashMap and then sort the endpoint chains to ensure the output is deterministic.
        endpoint_chains.sort_by(|a, b| a.route.value().cmp(&b.route.value()));

        let app = quote!(
            async fn __app(request: shuttle_next::Request<shuttle_next::body::BoxBody>,) -> shuttle_next::response::Response
            {
                use shuttle_next::Service;

                let mut router = shuttle_next::Router::new()
                    #(#endpoint_chains)*;

                let response = router.call(request).await.unwrap();

                response
            }
        );

        app.to_tokens(tokens);
    }
}

pub(crate) fn wasi_bindings(app: App) -> proc_macro2::TokenStream {
    quote!(
        #app

        #[cfg(not(test))]
        #[no_mangle]
        #[allow(non_snake_case)]
        pub extern "C" fn __SHUTTLE_Axum_call(
            parts_fd: std::os::wasi::prelude::RawFd,
            body_fd: std::os::wasi::prelude::RawFd,
        ) {
            use shuttle_next::body::{Body, HttpBody};
            use std::io::{Read, Write};
            use std::os::wasi::io::FromRawFd;

            use shuttle_next::tracing_prelude::*;

            shuttle_next::tracing_registry()
                .with(shuttle_next::tracing_fmt::layer().without_time())
                .init();

            // file descriptor 3 for reading and writing http parts
            let mut parts_fd = unsafe { std::fs::File::from_raw_fd(parts_fd) };

            let reader = std::io::BufReader::new(&mut parts_fd);

            // deserialize request parts from rust messagepack
            let wrapper: shuttle_next::RequestWrapper = shuttle_next::from_read(reader).unwrap();

            // file descriptor 4 for reading and writing http body
            let mut body_stream = unsafe { std::fs::File::from_raw_fd(body_fd) };

            let mut reader = std::io::BufReader::new(&mut body_stream);
            let mut body_buf = Vec::new();
            reader.read_to_end(&mut body_buf).unwrap();

            let body = Body::from(body_buf);

            let request = wrapper
                .into_request_builder()
                .body(shuttle_next::body::boxed(body))
                .unwrap();

            let res = shuttle_next::block_on(__app(request));

            let (parts, mut body) = res.into_parts();

            // wrap and serialize response parts as rmp
            let response_parts = shuttle_next::ResponseWrapper::from(parts)
                .into_rmp()
                .expect("failed to serialize response parts");

            // write response parts
            parts_fd.write_all(&response_parts).unwrap();

            // write body if there is one
            if let Some(body) = shuttle_next::block_on(body.data()) {
                body_stream.write_all(body.unwrap().as_ref()).unwrap();
            }
        }
    )
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;
    use quote::quote;
    use syn::parse_quote;

    use crate::next::{App, Parameter};

    use super::{Endpoint, Params};

    #[test]
    fn endpoint_to_token() {
        let endpoint = Endpoint {
            route: parse_quote!("/hello"),
            method: parse_quote!(get),
            function: parse_quote!(hello),
        };

        let actual = quote!(#endpoint);
        let expected = quote!(.route("/hello", get(hello)));

        assert_eq!(actual.to_string(), expected.to_string());
    }

    #[test]
    #[rustfmt::skip::macros(quote)]
    fn app_to_token() {
        let cases = vec![
            (
                App {
                    endpoints: vec![
                        Endpoint {
                            route: parse_quote!("/hello"),
                            method: parse_quote!(get),
                            function: parse_quote!(hello),
                        },
                        Endpoint {
                            route: parse_quote!("/goodbye"),
                            method: parse_quote!(post),
                            function: parse_quote!(goodbye),
                        },
                    ],
                },
                quote!(
                    async fn __app(
                        request: shuttle_next::Request<shuttle_next::body::BoxBody>,
                    ) -> shuttle_next::response::Response {
                        use shuttle_next::Service;

                        let mut router = shuttle_next::Router::new()
                            .route("/goodbye", shuttle_next::routing::post(goodbye))
                            .route("/hello", shuttle_next::routing::get(hello));

                        let response = router.call(request).await.unwrap();

                        response
                    }
                ),
            ),
            (
                App {
                    endpoints: vec![
                        Endpoint {
                            route: parse_quote!("/hello"),
                            method: parse_quote!(get),
                            function: parse_quote!(hello),
                        },
                        Endpoint {
                            route: parse_quote!("/goodbye"),
                            method: parse_quote!(get),
                            function: parse_quote!(get_goodbye),
                        },
                        Endpoint {
                            route: parse_quote!("/goodbye"),
                            method: parse_quote!(post),
                            function: parse_quote!(post_goodbye),
                        },
                    ],
                },
                quote!(
                    async fn __app(
                        request: shuttle_next::Request<shuttle_next::body::BoxBody>,
                    ) -> shuttle_next::response::Response {
                        use shuttle_next::Service;

                        let mut router = shuttle_next::Router::new()
                            .route(
                                "/goodbye",
                                shuttle_next::routing::get(get_goodbye).post(post_goodbye)
                            )
                            .route("/hello", shuttle_next::routing::get(hello));

                        let response = router.call(request).await.unwrap();

                        response
                    }
                ),
            ),
        ];

        for (app, expected) in cases {
            let actual = quote!(#app);
            assert_eq!(actual.to_string(), expected.to_string());
        }
    }

    #[test]
    fn parse_endpoint() {
        let cases = vec![
            (
                parse_quote! {
                #[shuttle_codegen::endpoint(method = get, route = "/hello")]
                async fn hello() -> &'static str {
                    "Hello, World!"
                }},
                Some(Endpoint {
                    route: parse_quote!("/hello"),
                    method: parse_quote!(get),
                    function: parse_quote!(hello),
                }),
                0,
            ),
            (
                parse_quote! {
                #[doc = r" This attribute is not an endpoint so keep it"]
                #[shuttle_codegen::endpoint(method = get, route = "/hello")]
                async fn hello() -> &'static str {
                    "Hello, World!"
                }},
                Some(Endpoint {
                    route: parse_quote!("/hello"),
                    method: parse_quote!(get),
                    function: parse_quote!(hello),
                }),
                1,
            ),
            (
                parse_quote! {
                    /// This attribute is not an endpoint so keep it
                    async fn say_hello() -> &'static str {
                        "Hello, World!"
                    }
                },
                None,
                1,
            ),
        ];

        for (mut input, expected, remaining_attributes) in cases {
            let actual = Endpoint::from_item_fn(&mut input);

            assert_eq!(actual, expected);

            // Verify that only endpoint attributes have been stripped
            assert_eq!(input.attrs.len(), remaining_attributes);
        }
    }

    #[test]
    fn parse_parameter() {
        // test method param
        let cases: Vec<(Parameter, Parameter)> = vec![
            (
                // parsing an identifier
                parse_quote! {
                    method = get
                },
                Parameter {
                    key: parse_quote!(method),
                    equals: parse_quote!(=),
                    value: parse_quote!(get),
                },
            ),
            (
                // parsing a string literal
                parse_quote! {
                    route = "/hello"
                },
                Parameter {
                    key: parse_quote!(route),
                    equals: parse_quote!(=),
                    value: parse_quote!("/hello"),
                },
            ),
        ];
        for (actual, expected) in cases {
            assert_eq!(actual, expected);
        }
    }

    #[test]
    fn parse_params() {
        let actual: Params = parse_quote![method = get, route = "/hello"];

        let mut expected = Params {
            params: Default::default(),
        };
        expected.params.push(parse_quote!(method = get));
        expected.params.push(parse_quote!(route = "/hello"));

        assert_eq!(actual, expected);
    }

    #[test]
    fn parse_app() {
        let mut input = parse_quote! {
            #[shuttle_codegen::endpoint(method = get, route = "/hello")]
            async fn hello() -> &'static str {
                "Hello, World!"
            }

            #[shuttle_codegen::endpoint(method = post, route = "/goodbye")]
            async fn goodbye() -> &'static str {
                "Goodbye, World!"
            }
        };

        let actual = App::from_file(&mut input);
        let expected = App {
            endpoints: vec![
                Endpoint {
                    route: parse_quote!("/hello"),
                    method: parse_quote!(get),
                    function: parse_quote!(hello),
                },
                Endpoint {
                    route: parse_quote!("/goodbye"),
                    method: parse_quote!(post),
                    function: parse_quote!(goodbye),
                },
            ],
        };

        assert_eq!(actual, expected);
    }

    #[test]
    fn ui() {
        let t = trybuild::TestCases::new();
        t.compile_fail("tests/ui/next/*.rs");
    }
}