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
use std::ops::Deref;

use proc_macro::TokenStream;
use quote::quote;
use syn::parse_macro_input;

#[proc_macro_attribute]
pub fn get(attr: TokenStream, item: TokenStream) -> TokenStream {
    let item_fn: syn::ItemFn = syn::parse(item).unwrap();
    //eprintln!("{:#?}\n{:#?}", attr, item);
    let args = parse_macro_input!(attr as syn::AttributeArgs);

    let sig = item_fn.sig;
    let name = sig.ident.clone();
    let body = item_fn.block.deref();
    let path_token = args[0].clone();
    let return_type = sig.output;

    let body_args = sig.inputs;
    let is_body_args = !body_args.is_empty();
    //eprintln!("LEN: {}", body_args.len());

    let mut path;
    match path_token {
        syn::NestedMeta::Meta(_) => panic!("IN TOKEN MATCH!"),
        syn::NestedMeta::Lit(e) => match e {
            syn::Lit::Str(e) => {
                path = e.value();
            }
            syn::Lit::ByteStr(_) => panic!("IN TOKEN MATCH!"),
            syn::Lit::Byte(_) => panic!("IN TOKEN MATCH!"),
            syn::Lit::Char(_) => panic!("IN TOKEN MATCH!"),
            syn::Lit::Int(_) => panic!("IN TOKEN MATCH!"),
            syn::Lit::Float(_) => panic!("IN TOKEN MATCH!"),
            syn::Lit::Bool(_) => panic!("IN TOKEN MATCH!"),
            syn::Lit::Verbatim(_) => panic!("IN TOKEN MATCH!"),
        },
    };

    let new_wildcard = if path.contains("/:") {
        let path_clone = path.clone();
        let mut iter = path_clone.split(':');
        path = iter.next().unwrap().to_string();
        let id = iter.next().unwrap().to_string();
        if path.len() != 1 {
            path.pop();
        };
        quote! {get_route = get_route.set_wildcard(#id.into());}
    } else {
        quote! {}
    };

    #[allow(unused_assignments)]
    let mut return_type_str = String::new();

    match return_type {
        syn::ReturnType::Default => return_type_str = "NO RETURN TYPE!".to_string(),
        syn::ReturnType::Type(_, value) => match *value {
            syn::Type::Path(stream) => {
                return_type_str = stream.path.segments.last().unwrap().ident.to_string()
            }
            _ => return_type_str = "NO RETURN TYPE!".to_string(),
        },
    };

    let is_ret_type_res = return_type_str == "Response";

    let new_get_body = if is_ret_type_res {
        quote! {
            let mut get_route = GetRouteWithReqAndRes::new()
                .set_path(#path.into());

            fn body(#body_args) -> Response {
                #body.into()
            }

            get_route = get_route.set_body(body);
        }
    } else if is_body_args {
        quote! {
            let mut get_route = GetRouteWithReq::new()
                .set_path(#path.into());

            fn body(#body_args) -> Vec<u8> {
                #body.into()
            }

            get_route = get_route.set_body(body);
        }
    } else {
        quote! {
            let mut get_route = BasicGetRoute::new()
                .set_path(#path.into());

            fn body() -> Vec<u8> {
                #body.into()
            }

            get_route = get_route.set_body(body);
        }
    };

    let output = quote! {
        fn #name() -> Box<dyn Route> {
            /*let mut get_route = GetRoute::new()
                .set_path(#path.into())
                .set_is_args(#is_body_args)
                .set_is_ret_res(#is_ret_type_res);*/


            #new_get_body
            #new_wildcard

            Box::new(get_route)
        }
    };

    output.into()
}

#[proc_macro_attribute]
pub fn post(attr: TokenStream, item: TokenStream) -> TokenStream {
    //eprintln!("{:#?}\n{:#?}", attr, item);
    let args = parse_macro_input!(attr as syn::AttributeArgs);
    let item: syn::ItemFn = syn::parse(item).unwrap();

    let fn_args = item.sig.inputs;
    let name = item.sig.ident.clone();
    let body = item.block.deref();
    let return_type = item.sig.output;

    let path_token = args[0].clone();
    let is_body_args = !fn_args.is_empty();

    let mut path;
    match path_token {
        syn::NestedMeta::Meta(_) => panic!("IN MATCH RETURN TYPE!"),
        syn::NestedMeta::Lit(e) => match e {
            syn::Lit::Str(e) => {
                path = e.value();
            }
            syn::Lit::ByteStr(_) => panic!("IN MATCH RETURN TYPE!"),
            syn::Lit::Byte(_) => panic!("IN MATCH RETURN TYPE!"),
            syn::Lit::Char(_) => panic!("IN MATCH RETURN TYPE!"),
            syn::Lit::Int(_) => panic!("IN MATCH RETURN TYPE!"),
            syn::Lit::Float(_) => panic!("IN MATCH RETURN TYPE!"),
            syn::Lit::Bool(_) => panic!("IN MATCH RETURN TYPE!"),
            syn::Lit::Verbatim(_) => panic!("IN MATCH RETURN TYPE!"),
        },
    };
    let new_wildcard = if path.contains("/:") {
        let path_clone = path.clone();
        let mut iter = path_clone.split(':');
        path = iter.next().unwrap().to_string();
        let id = iter.next().unwrap().to_string();
        if path.len() != 1 {
            path.pop();
        };
        quote! {post_route = post_route.set_wildcard(#id.into());}
    } else {
        quote! {}
    };

    #[allow(unused_assignments)]
    let mut return_type_str = String::new();

    match return_type {
        syn::ReturnType::Default => return_type_str = "NO RETURN TYPE!".to_string(),
        syn::ReturnType::Type(_, value) => match *value {
            syn::Type::Path(stream) => {
                return_type_str = stream.path.segments.last().unwrap().ident.to_string()
            }
            _ => return_type_str = "NO RETURN TYPE!".to_string(),
        },
    };

    let is_ret_type_res = return_type_str == "Response";

    let new_post_body = if is_ret_type_res {
        quote! {
            let mut post_route = PostRouteWithReqAndRes::new()
                .set_path(#path.into());

            fn body(#fn_args) -> Response {
                #body.into()
            }

            post_route = post_route.set_body(body);
        }
    } else if is_body_args {
        quote! {
            let mut post_route = PostRouteWithReq::new()
                .set_path(#path.into());

            fn body(#fn_args) -> Vec<u8> {
                #body.into()
            }

            post_route = post_route.set_body(body);
        }
    } else {
        quote! {
            let mut post_route = BasicPostRoute::new()
                .set_path(#path.into());

            fn body() -> Vec<u8> {
                #body.into()
            }

            post_route = post_route.set_body(body);
        }
    };

    let output = quote! {
        fn #name() -> Box<dyn Route> {
            /*let mut post_route = PostRoute::new()
                .set_path(#path.into())
                .set_is_args(#is_body_args)
                .set_is_ret_res(#is_ret_type_res);*/

            #new_post_body
            #new_wildcard

            Box::new(post_route)
        }
    };

    /*let output = quote! {
        fn #name() -> (String, Vec<u8>, Method) {
            fn body() #output {
                #body
            }

            (#path.into(), body().into(), Method::POST)
        }
    };*/

    output.into()
}