dropshot_endpoint/
lib.rs

1// Copyright 2023 Oxide Computer Company
2
3//! Macro support for the Dropshot HTTP server.
4//!
5//! For more information about these macros, see the [Dropshot
6//! documentation](https://docs.rs/dropshot).
7
8#![forbid(unsafe_code)]
9
10use quote::quote;
11use serde_tokenstream::Error;
12
13mod api_trait;
14mod channel;
15mod doc;
16mod endpoint;
17mod error_store;
18mod metadata;
19mod params;
20mod syn_parsing;
21#[cfg(test)]
22mod test_util;
23mod util;
24
25// NOTE: We do not define documentation here -- only in the Dropshot crate while
26// re-exporting these items. This is so that doctests that depend on the
27// Dropshot crate work.
28
29#[proc_macro_attribute]
30pub fn endpoint(
31    attr: proc_macro::TokenStream,
32    item: proc_macro::TokenStream,
33) -> proc_macro::TokenStream {
34    do_output(endpoint::do_endpoint(attr.into(), item.into()))
35}
36
37#[proc_macro_attribute]
38pub fn channel(
39    attr: proc_macro::TokenStream,
40    item: proc_macro::TokenStream,
41) -> proc_macro::TokenStream {
42    do_output(channel::do_channel(attr.into(), item.into()))
43}
44
45#[proc_macro_attribute]
46pub fn api_description(
47    attr: proc_macro::TokenStream,
48    item: proc_macro::TokenStream,
49) -> proc_macro::TokenStream {
50    do_output(api_trait::do_trait(attr.into(), item.into()))
51}
52
53fn do_output(
54    (endpoint, errors): (proc_macro2::TokenStream, Vec<Error>),
55) -> proc_macro::TokenStream {
56    let compiler_errors = errors.iter().map(|err| err.to_compile_error());
57
58    let output = quote! {
59        #endpoint
60        #( #compiler_errors )*
61    };
62
63    output.into()
64}