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
use std::ops::Deref;
use proc_macro::TokenStream;
use quote::quote;
use syn::spanned::Spanned;
#[proc_macro_attribute]
pub fn get(attr: TokenStream, item: TokenStream) -> TokenStream {
let item_fn: syn::ItemFn = syn::parse(item).unwrap();
let value: syn::LitStr = syn::parse(attr).unwrap();
let sig = item_fn.sig;
let name = sig.ident.clone();
let body = item_fn.block.deref();
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 = value.value();
/*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! {}
};
let span = return_type.span();
let return_error = match return_type {
syn::ReturnType::Default => Some(
syn::Error::new(span, "You're forgetting to return something...").into_compile_error(),
),
_ => None,
};
if let Some(e) = return_error {
return e.into();
}
// 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);
// }
let new_get_body = if is_body_args {
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 {
quote! {
let mut get_route = BasicGetRoute::new()
.set_path(#path.into());
fn body() -> Response {
#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 item: syn::ItemFn = syn::parse(item).unwrap();
let value: syn::LitStr = syn::parse(attr).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 is_body_args = !fn_args.is_empty();
let mut path = value.value();
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! {}
};
let return_error = match return_type {
syn::ReturnType::Default => Some(
syn::Error::new(
return_type.span(),
"You're forgetting to return something...",
)
.into_compile_error(),
),
_ => None,
};
if let Some(e) = return_error {
return e.into();
}
let new_post_body = if is_body_args {
let first_arg_name = fn_args.first().unwrap();
let arg_type = match first_arg_name {
syn::FnArg::Typed(i) => i.to_owned(),
_ => todo!(),
};
// NOTE: Gets arg name and type
// let arg_name_pat = match &arg_type.pat.deref() {
// syn::Pat::Ident(i) => i.to_owned(),
// _ => todo!(),
// };
// let arg_name_type = match &arg_type.ty.deref() {
// syn::Type::Path(i) => i.to_owned(),
// _ => todo!(),
// };
//
// let arg_name_type = &arg_name_type.path.segments.first().unwrap().ident;
quote! {
let mut post_route = PostRouteWithReqAndRes::new()
.set_path(#path.into());
fn body<'b>(try_from_req: &'b mut Request) -> Response {
let #arg_type = try_from_req.into();
#body.into()
}
post_route = post_route.set_body(body);
}
} else {
quote! {
let mut post_route = BasicPostRoute::new()
.set_path(#path.into());
fn body() -> Response {
#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()
}