dropshot_endpoint 0.17.0

macro used by dropshot consumers for registering handlers
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
// Copyright 2023 Oxide Computer Company

//! Support for WebSocket `#[channel]` macros.

use crate::doc::ExtractedDoc;
use crate::error_store::ErrorSink;
use crate::error_store::ErrorStore;
use crate::metadata::ApiEndpointKind;
use crate::metadata::ChannelMetadata;
use crate::params::validate_fn_ast;
use crate::params::ParamValidator;
use crate::params::RqctxKind;
use crate::params::RqctxTy;
use crate::syn_parsing::ItemFnForSignature;
use crate::syn_parsing::TraitItemFnForSignature;
use crate::util::MacroKind;
use proc_macro2::TokenStream;
use quote::format_ident;
use quote::quote;
use quote::quote_spanned;
use serde_tokenstream::from_tokenstream;
use serde_tokenstream::Error;
use syn::parse_quote;
use syn::spanned::Spanned;

pub(crate) fn usage_str(context: &str) -> String {
    format!(
        "channel handlers must have the following signature:
    async fn(
        rqctx: dropshot::RequestContext<{context}>,
        [query_params: Query<Q>,]
        [path_params: Path<P>,]
        websocket_connection: dropshot::WebsocketConnection,
    ) -> dropshot::WebsocketChannelResult"
    )
}

pub(crate) fn do_channel(
    attr: proc_macro2::TokenStream,
    item: proc_macro2::TokenStream,
) -> (proc_macro2::TokenStream, Vec<Error>) {
    let mut error_store = ErrorStore::new();
    let errors = error_store.sink();

    // Attempt to parse the function as a trait function. If this is successful
    // and there's no block, then it's likely that the user has imported
    // `dropshot::endpoint` and is using that.
    if let Ok(trait_item_fn) =
        syn::parse2::<TraitItemFnForSignature>(item.clone())
    {
        if trait_item_fn.block.is_none() {
            let name = &trait_item_fn.sig.ident;
            errors.push(Error::new_spanned(
                &trait_item_fn.sig,
                format!(
                    "endpoint `{name}` appears to be a trait function\n\
                     note: did you mean to use `#[dropshot::api_description]` \
                     instead?",
                ),
            ));
            // Don't do any further validation -- just return the original item.
            return (quote! { #item }, error_store.into_inner());
        }
    }

    // Parse attributes. (Do this before parsing the function since that's the
    // order they're in, in source code.)
    let metadata = match from_tokenstream(&attr) {
        Ok(metadata) => Some(metadata),
        Err(e) => {
            // If there is an error while parsing the metadata, report it, but
            // continue to generate the original function.
            errors.push(e);
            None
        }
    };

    // Attempt to parse the function.
    let item_fn = match syn::parse2::<ItemFnForSignature>(item.clone()) {
        Ok(item_fn) => Some(item_fn),
        Err(e) => {
            errors.push(e);
            None
        }
    };

    let output = match (metadata, item_fn.as_ref()) {
        (Some(metadata), Some(item_fn)) => {
            // The happy path.
            do_channel_inner(metadata, attr, item, &item_fn, errors)
        }
        (None, Some(_)) => {
            // In this case, continue to generate the original function (but not
            // the attribute proc macro).
            ChannelOutput {
                output: quote! { #item },
                // We don't validate parameters, so we don't know if there are
                // errors in them.
                has_param_errors: false,
            }
        }
        (_, None) => {
            // Can't do anything here, just return errors.
            ChannelOutput { output: quote! {}, has_param_errors: false }
        }
    };

    let mut errors = error_store.into_inner();

    // If there are any errors, we also want to provide a usage message as an error.
    if output.has_param_errors {
        let item_fn = item_fn
            .as_ref()
            .expect("has_param_errors is true => item_fn is Some");
        errors.insert(
            0,
            Error::new_spanned(&item_fn.sig, usage_str("MyContext")),
        );
    }

    (output.output, errors)
}

/// The result of calling [`do_channel`].
struct ChannelOutput {
    /// The actual output.
    output: proc_macro2::TokenStream,

    /// Whether there were any parameter-related errors.
    has_param_errors: bool,
}

fn do_channel_inner(
    metadata: ChannelMetadata,
    attr: proc_macro2::TokenStream,
    item: proc_macro2::TokenStream,
    item_fn: &ItemFnForSignature,
    errors: ErrorSink<'_, Error>,
) -> ChannelOutput {
    let dropshot = metadata.dropshot_crate();

    let name = &item_fn.sig.ident;
    let name_str = name.to_string();

    // Perform validations first.
    let metadata =
        metadata.validate(&name_str, &attr, MacroKind::Function, &errors);
    let params = ChannelParams::new(
        &dropshot,
        &item_fn.sig,
        RqctxKind::Function,
        &errors,
    );

    let visibility = &item_fn.vis;

    let doc = ExtractedDoc::from_attrs(&item_fn.attrs);
    let comment_text = doc.comment_text(&name_str);

    let description_doc_comment = quote! {
        #[doc = #comment_text]
    };

    // If the params are valid, output the corresponding type checks and impl
    // statement.
    let (has_param_errors, type_checks, from_impl) =
        if let Some(params) = &params {
            let type_checks = params.to_type_checks();
            let impl_checks = params.to_impl_checks(name);
            let adapter_fn = params.to_adapter_fn();

            // If the metadata is valid, output the corresponding ApiEndpoint.
            let construct = if let Some(metadata) = metadata {
                metadata.to_api_endpoint_fn(
                    &dropshot,
                    &name_str,
                    &ApiEndpointKind::Regular(&params.adapter_name),
                    &doc,
                )
            } else {
                quote! {
                    unreachable!()
                }
            };

            let rqctx_context = params.rqctx_context();

            let from_impl = quote! {
                impl From<#name>
                    for #dropshot::ApiEndpoint< #rqctx_context >
                {
                    fn from(_: #name) -> Self {
                        #[allow(clippy::unused_async)]
                        #item

                        // The checks on the implementation require #name to be in
                        // scope, which is provided by #item, hence we place these
                        // checks here instead of above with the others.
                        #impl_checks

                        // Generate the adapter function after the checks so
                        // that errors from the checks show up first.
                        #adapter_fn

                        #construct
                    }
                }
            };

            (false, type_checks, from_impl)
        } else {
            (true, quote! {}, quote! {})
        };

    // For reasons that are not well understood unused constants that use the
    // (default) call_site() Span do not trigger the dead_code lint. Because
    // defining but not using an endpoint is likely a programming error, we
    // want to be sure to have the compiler flag this. We force this by using
    // the span from the name of the function to which this macro was applied.
    let span = item_fn.sig.ident.span();
    let const_struct = quote_spanned! {span=>
        #visibility const #name: #name = #name {};
    };

    // The final TokenStream returned will have a few components that reference
    // `#name`, the name of the function to which this macro was applied...
    let stream = quote! {
        // ... type validation for parameter and return types
        #type_checks

        // ... a struct type called `#name` that has no members
        #[allow(non_camel_case_types, missing_docs)]
        #description_doc_comment
        #visibility struct #name {}
        // ... a constant of type `#name` whose identifier is also #name
        #[allow(non_upper_case_globals, missing_docs)]
        #description_doc_comment
        #const_struct

        // ... an impl of `From<#name>` for ApiEndpoint that allows the constant
        // `#name` to be passed into `ApiDescription::register()`
        #from_impl
    };

    ChannelOutput { output: stream, has_param_errors }
}

pub(crate) struct ChannelParams<'ast> {
    dropshot: TokenStream,
    sig: &'ast syn::Signature,
    rqctx_ty: RqctxTy<'ast>,
    shared_extractors: Vec<&'ast syn::Type>,
    websocket_conn: &'ast syn::Type,
    pub(crate) ret_ty: &'ast syn::Type,
    pub(crate) adapter_name: syn::Ident,

    // Types used in the adapter function, generated at construction time.
    websocket_upgrade_ty: syn::Type,
    pub(crate) endpoint_result_ty: syn::Type,
}

impl<'ast> ChannelParams<'ast> {
    pub(crate) fn new(
        dropshot: &TokenStream,
        sig: &'ast syn::Signature,
        rqctx_kind: RqctxKind<'_>,
        errors: &ErrorSink<'_, Error>,
    ) -> Option<Self> {
        let name_str = sig.ident.to_string();
        let errors = errors.new();

        validate_fn_ast(sig, &name_str, &errors);

        let mut params = ParamValidator::new(sig, &name_str);
        params.maybe_discard_self_arg(&errors);

        let (rqctx_ty, websocket_conn_ty) = params
            .next_rqctx_and_last_websocket_args(
                rqctx_kind,
                &sig.paren_token.span,
                &errors,
            );

        // All other parameters must impl SharedExtractor.
        let shared_extractors = params.rest_extractor_args(&errors);

        // Validate the websocket connection type.
        let websocket_conn =
            websocket_conn_ty.and_then(|ty| ty.validate(&name_str, &errors));

        let ret_ty = params.return_type(&errors);

        let websocket_upgrade_ty = parse_quote! { #dropshot::WebsocketUpgrade };
        let endpoint_result_ty =
            parse_quote! { #dropshot::WebsocketEndpointResult };

        // Use the entire function signature as the span for the generated
        // identifier, to avoid confusing rust-analyzer (attributing multiple
        // items to a single function make makes ctrl-click worse).
        let adapter_name =
            format_ident!("{}_adapter", sig.ident, span = sig.span());

        // errors.has_errors() must be checked first, because it's possible for
        // the below variables to all be Some, but one of the extractors to have
        // errored out.
        if errors.has_errors() {
            None
        } else if let (Some(rqctx_ty), Some(websocket_conn), Some(ret_ty)) =
            (rqctx_ty, websocket_conn, ret_ty)
        {
            Some(Self {
                dropshot: dropshot.clone(),
                sig,
                rqctx_ty,
                ret_ty,
                shared_extractors,
                websocket_conn,
                adapter_name,
                websocket_upgrade_ty,
                endpoint_result_ty,
            })
        } else {
            unreachable!(
                "no param errors, but rqctx_ty, \
                 websocket_upgrade or ret_ty is None"
            );
        }
    }

    fn to_type_checks(&self) -> TokenStream {
        let dropshot = &self.dropshot;
        let rqctx_context = self.rqctx_context();
        let rqctx_check = quote_spanned! { self.rqctx_ty.orig_span()=>
            const _: fn() = || {
                #[allow(dead_code)]
                struct NeedRequestContext(#rqctx_context);
            };
        };

        let shared_extractor_checks = self.shared_extractors.iter().map(|ty| {
            quote_spanned! { ty.span()=>
                const _: fn() = || {
                    fn need_shared_extractor<T>()
                    where
                        T: ?Sized + #dropshot::SharedExtractor,
                    {
                    }
                    need_shared_extractor::<#ty>();
                };
            }
        });

        let trait_type_checks = self.to_trait_type_checks();

        quote! {
            #rqctx_check

            #(#shared_extractor_checks)*

            #trait_type_checks
        }
    }

    /// Generate type checks suitable for server traits.
    ///
    /// Unlike with endpoints, we do need to generate some of the type checks
    /// within traits. That's because we use an adapter function -- so types
    /// that are unique to the user function need to be checked.
    pub(crate) fn to_trait_type_checks(&self) -> TokenStream {
        let dropshot = &self.dropshot;

        let websocket_conn = self.websocket_conn;
        let websocket_conn_check = quote_spanned! { websocket_conn.span()=>
            const _: fn() = || {
                trait TypeEq {
                    type This: ?Sized;
                }
                impl<T: ?Sized> TypeEq for T {
                    type This = Self;
                }
                fn validate_websocket_connection_type<T>()
                where
                    T: ?Sized + TypeEq<This = #dropshot::WebsocketConnection>,
                {
                }
                validate_websocket_connection_type::<#websocket_conn>();
            };
        };

        // Checking the return type doesn't appear to be necessary, because the
        // `WebsocketUpgrade::handle` function produces a decent error message
        // if it's wrong. (In any case, an explicit check doesn't do a better
        // job and just produces noise.)

        quote! {
            #websocket_conn_check
        }
    }

    /// Returns a token stream that obtains the rqctx context type.
    fn rqctx_context(&self) -> TokenStream {
        self.rqctx_ty.to_context(&self.dropshot)
    }

    /// Returns a list of generated argument names.
    fn arg_names(&self) -> impl Iterator<Item = syn::Ident> + '_ {
        // The total number of arguments is 1 (rqctx) + the number of shared
        // extractors + 1 (websocket_upgrade). The last argument we'll name
        // specially so it can be referred to below.
        let arg_count = 1 + self.shared_extractors.len();

        (0..arg_count)
            .map(|i| format_ident!("arg{}", i))
            .chain(std::iter::once(format_ident!("__dropshot_websocket")))
    }

    /// Returns a list of all argument types, including the request context.
    fn arg_types(&self) -> impl Iterator<Item = &syn::Type> + '_ {
        std::iter::once(self.rqctx_ty.transformed_unit_type())
            .chain(self.shared_extractors.iter().copied())
            .chain(std::iter::once(self.websocket_conn))
    }

    /// Returns a list of all the argument types as they should show up in the
    /// adapter function.
    fn adapter_arg_types(&self) -> impl Iterator<Item = &syn::Type> + '_ {
        std::iter::once(self.rqctx_ty.transformed_server_impl_type())
            .chain(self.extractor_types())
    }

    /// Returns a list of the extractor types.
    ///
    /// The exclusive extractor in this situation is `WebsocketUpgrade`.
    pub(crate) fn extractor_types(
        &self,
    ) -> impl Iterator<Item = &syn::Type> + '_ {
        self.shared_extractors
            .iter()
            .copied()
            .chain(std::iter::once(&self.websocket_upgrade_ty))
    }

    /// Constructs implementation checks for the endpoint.
    ///
    /// These checks are placed in the same scope as the definition of the
    /// endpoint.
    fn to_impl_checks(&self, name: &syn::Ident) -> TokenStream {
        // We want to construct a function that will call the user's endpoint,
        // so we can check the future it returns for bounds that otherwise
        // produce inscrutable error messages (like returning a non-`Send`
        // future). We produce a wrapper function that takes all the same
        // argument types, which requires building up a list of argument names:
        // we can't use the original definition's argument names since they
        // could have multiple args named `_`, so we use "arg0", "arg1", etc.
        let arg_names: Vec<_> = self.arg_names().collect();
        let arg_types = self.arg_types();

        quote! {
            const _: fn() = || {
                fn future_endpoint_must_be_send<T: ::std::marker::Send>(_t: T) {}
                fn check_future_bounds(#( #arg_names: #arg_types ),*) {
                    future_endpoint_must_be_send(#name(#(#arg_names),*));
                }
            };
        }
    }

    /// Constructs the adapter function that will call the user's endpoint.
    ///
    /// Currently, channels are implemented as adapters over endpoints. This
    /// function translates channel functions into endpoint functions.
    fn to_adapter_fn(&self) -> TokenStream {
        let arg_names = self.arg_names();
        let arg_names_2 = self.arg_names();
        let adapter_arg_types = self.adapter_arg_types();
        let websocket_conn = self.websocket_conn;
        let name = &self.sig.ident;
        let adapter_name = &self.adapter_name;
        let endpoint_result_ty = &self.endpoint_result_ty;

        quote_spanned! {self.sig.span()=>
            async fn #adapter_name(
                #( #arg_names: #adapter_arg_types ),*
            ) -> #endpoint_result_ty {
                __dropshot_websocket.handle(
                    move | __dropshot_websocket: #websocket_conn | async move {
                        #name(#(#arg_names_2),*).await
                    },
                )
            }
        }
    }

    /// Constructs the adapter function that calls the user's endpoint on a
    /// server trait.
    ///
    /// This is a bit more complicated than the regular adapter function, since
    /// the server trait must be referred to carefully.
    pub(crate) fn to_trait_adapter_fn(
        &self,
        trait_ident: &syn::Ident,
    ) -> TokenStream {
        let arg_names = self.arg_names();
        let arg_names_2 = self.arg_names();
        let adapter_arg_types = self.adapter_arg_types();
        let websocket_conn = self.websocket_conn;
        let name = &self.sig.ident;
        let adapter_name = &self.adapter_name;
        let endpoint_result_ty = &self.endpoint_result_ty;

        quote_spanned! {self.sig.span()=>
            async fn #adapter_name<ServerImpl: #trait_ident>(
                #( #arg_names: #adapter_arg_types ),*
            ) -> #endpoint_result_ty {
                __dropshot_websocket.handle(
                    move | __dropshot_websocket: #websocket_conn | async move {
                        <ServerImpl as #trait_ident>::#name(#(#arg_names_2),*).await
                    },
                )
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use expectorate::assert_contents;
    use syn::parse_quote;

    use crate::{
        test_util::{assert_banned_idents, find_idents},
        util::DROPSHOT,
    };

    use super::*;

    #[test]
    fn test_channel_with_custom_params() {
        let input = quote! {
            async fn my_channel(
                rqctx: RequestContext<()>,
                query: Query<Q>,
                conn: WebsocketConnection,
            ) -> WebsocketChannelResult {
                Ok(())
            }
        };

        let (item, errors) = do_channel(
            quote! {
                protocol = WEBSOCKETS,
                path = "/my/ws/channel",
                _dropshot_crate = "topspin",
            },
            input.clone(),
        );

        assert!(errors.is_empty());

        let file = parse_quote! { #item };
        // Write out the file before checking it for banned idents, so that we
        // can see what it looks like.
        assert_contents(
            "tests/output/channel_with_custom_params.rs",
            &prettyplease::unparse(&file),
        );

        // Check banned identifiers.
        let banned = [DROPSHOT];
        assert_banned_idents(&file, banned);

        // Without _dropshot_crate, the generated output must contain
        // "dropshot".
        let (item, errors) = do_channel(
            quote! {
                protocol = WEBSOCKETS,
                path = "/my/ws/channel",
            },
            input,
        );

        assert!(errors.is_empty());
        let file = parse_quote! { #item };
        assert_eq!(
            find_idents(&file, banned).into_iter().collect::<Vec<_>>(),
            banned
        );
    }

    #[test]
    fn test_channel_with_unnamed_params() {
        let (item, errors) = do_channel(
            quote! {
                protocol = WEBSOCKETS,
                path = "/my/ws/channel",
            },
            quote! {
                async fn handler_xyz(
                    _: RequestContext<()>,
                    _: Query<Q>,
                    _: Path<P>,
                    _: WebsocketConnection,
                ) -> WebsocketChannelResult {
                    Ok(())
                }
            },
        );

        println!("{:?}", errors);

        assert!(errors.is_empty());
        assert_contents(
            "tests/output/channel_with_unnamed_params.rs",
            &prettyplease::unparse(&parse_quote! { #item }),
        );
    }

    #[test]
    fn test_channel_operation_id() {
        let (item, errors) = do_channel(
            quote! {
                protocol = WEBSOCKETS,
                path = "/my/ws/channel",
                operation_id = "vzeroupper"
            },
            quote! {
                async fn handler_xyz(
                    _rqctx: RequestContext<()>,
                    _ws: WebsocketConnection,
                ) -> Result<HttpResponseOk<()>, HttpError> {
                    Ok(())
                }
            },
        );

        assert!(errors.is_empty());
        assert_contents(
            "tests/output/channel_operation_id.rs",
            &prettyplease::unparse(&parse_quote! { #item }),
        );
    }

    #[test]
    fn test_channel_with_versions() {
        let input = quote! {
            async fn my_channel(
                _rqctx: RequestContext<()>,
                _conn: WebsocketConnection,
            ) -> WebsocketChannelResult {
                Ok(())
            }
        };

        let (item, errors) = do_channel(
            quote! {
                protocol = WEBSOCKETS,
                path = "/my/ws/channel",
                versions = .."1.2.3",
            },
            input.clone(),
        );

        assert!(errors.is_empty());

        let file = parse_quote! { #item };
        assert_contents(
            "tests/output/channel_with_versions.rs",
            &prettyplease::unparse(&file),
        );
    }
}