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
extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro2::{Ident, Span as Span2};
use quote::quote;

#[proc_macro_attribute]
pub fn socketio_handler(attr: TokenStream, item: TokenStream) -> TokenStream {
    if let syn::Item::Fn(mut function_item) = syn::parse(item.clone()).unwrap() {
        let name = function_item.ident.clone();
        let new_name = Ident::new(&format!("__async_{}", name.clone()), Span2::call_site());
        function_item.ident = new_name.clone();

        let visibility = function_item.vis.clone();
        let arguments = function_item.decl.inputs.clone();
        let generics = function_item.decl.generics.clone();

        let context_type = match &arguments[0] {
            syn::FnArg::Captured(cap) => &cap.ty,
            _ => panic!("Expected the first argument to be a context type"),
        };
        let socket_io_type = Ident::new(&format!("__SocketIO_{}", name.clone()), Span2::call_site());
        let crate_path = match attr.to_string().as_str() {
            "_internal" => quote! {
                crate::core::{ IOSocketWrapper as #socket_io_type }
            },
            _ => quote! {
                thruster_socketio::{ SocketIO as #socket_io_type }
            },
        };

        let gen = quote! {
            #function_item

            use #crate_path;
            #visibility fn #name#generics(socket: #context_type) -> Pin<Box<dyn Future<Output = Result<#context_type, ()>> + Send>> {
                Box::pin(#new_name(socket))
            }
        };

        // proc_macro::Span::call_site()
        //     .note("Thruster code output")
        //     .note(gen.to_string())
        //     .emit();

        gen.into()
    } else {
        item
    }
}

#[proc_macro_attribute]
pub fn socketio_listener(attr: TokenStream, item: TokenStream) -> TokenStream {
    if let syn::Item::Fn(mut function_item) = syn::parse(item.clone()).unwrap() {
        let name = function_item.ident.clone();
        let new_name = Ident::new(&format!("__async_{}", name.clone()), Span2::call_site());
        function_item.ident = new_name.clone();

        let visibility = function_item.vis.clone();
        let arguments = function_item.decl.inputs.clone();
        let generics = function_item.decl.generics.clone();

        let socket_type = match &arguments[0] {
            syn::FnArg::Captured(cap) => &cap.ty,
            _ => panic!("Expected the first argument to be a socket type"),
        };

        let socket_io_type = Ident::new(&format!("__SocketIOListener_{}", name.clone()), Span2::call_site());
        let crate_path = match attr.to_string().as_str() {
            "_internal" => quote! {
                crate::core::{ IOSocketWrapper as #socket_io_type }
            },
            _ => quote! {
                thruster_socketio::{ SocketIO as #socket_io_type }
            },
        };

        let gen = quote! {
            #function_item

            use #crate_path;
            #visibility fn #name#generics(socket: #socket_type, value: String) -> Pin<Box<dyn Future<Output = Result<(), ()>> + Send>> {
                Box::pin(#new_name(socket, value))
            }
        };

        // proc_macro::Span::call_site()
        //     .note("Thruster code output")
        //     .note(gen.to_string())
        //     .emit();

        gen.into()
    } else {
        item
    }
}