MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / handler_impl

Function handler_impl

crates/bindings-macro/src/http.rs:7–77  ·  view source on GitHub ↗
(args: TokenStream, original_function: &ItemFn)

Source from the content-addressed store, hash-verified

5use syn::{ItemFn, ReturnType};
6
7pub(crate) fn handler_impl(args: TokenStream, original_function: &ItemFn) -> syn::Result<TokenStream> {
8 if !args.is_empty() {
9 return Err(syn::Error::new_spanned(
10 args,
11 "The `handler` attribute does not accept arguments",
12 ));
13 }
14
15 let func_name = &original_function.sig.ident;
16 let vis = &original_function.vis;
17 let handler_name = ident_to_litstr(func_name);
18
19 assert_only_lifetime_generics(original_function, "http handlers")?;
20
21 let typed_args = extract_typed_args(original_function)?;
22 if typed_args.len() != 2 {
23 return Err(syn::Error::new_spanned(
24 original_function.sig.clone(),
25 "HTTP handlers must take exactly two arguments",
26 ));
27 }
28
29 let arg_tys = typed_args.iter().map(|arg| arg.ty.as_ref()).collect::<Vec<_>>();
30 let first_arg_ty = &arg_tys[0];
31 let second_arg_ty = &arg_tys[1];
32
33 let ret_ty = match &original_function.sig.output {
34 ReturnType::Type(_, t) => t.as_ref(),
35 ReturnType::Default => {
36 return Err(syn::Error::new_spanned(
37 original_function.sig.clone(),
38 "HTTP handlers must return `spacetimedb::http::Response`",
39 ));
40 }
41 };
42
43 let internal_ident = syn::Ident::new(&format!("__spacetimedb_http_handler_{func_name}"), func_name.span());
44 let mut inner_fn = original_function.clone();
45 inner_fn.sig.ident = internal_ident.clone();
46
47 let register_describer_symbol = format!("__preinit__20_register_http_handler_{}", handler_name.value());
48
49 let lifetime_params = &original_function.sig.generics;
50 let lifetime_where_clause = &lifetime_params.where_clause;
51
52 let generated_describe_function = quote! {
53 #[unsafe(export_name = #register_describer_symbol)]
54 pub extern "C" fn __register_describer() {
55 spacetimedb::rt::register_http_handler(#handler_name, #internal_ident)
56 }
57 };
58
59 Ok(quote! {
60 #inner_fn
61
62 #[allow(non_upper_case_globals)]
63 #vis const #func_name: spacetimedb::http::Handler = spacetimedb::http::Handler::new(#handler_name);
64

Callers 1

http_handlerFunction · 0.85

Calls 13

ident_to_litstrFunction · 0.85
extract_typed_argsFunction · 0.85
spanMethod · 0.80
iterMethod · 0.65
ErrFunction · 0.50
newFunction · 0.50
OkFunction · 0.50
is_emptyMethod · 0.45
lenMethod · 0.45
cloneMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…