Library for Derive Macros and impl IntoResponse trait

This commit is contained in:
Martin Berg Alstad
2024-06-17 00:47:07 +02:00
parent 6b6f4b4779
commit 9226060397
10 changed files with 104 additions and 25 deletions

25
derive/src/lib.rs Normal file
View File

@ -0,0 +1,25 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{DeriveInput, parse_macro_input};
#[proc_macro_derive(IntoResponse)]
pub fn into_response_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
into_response_derive_impl(input)
}
fn into_response_derive_impl(input: DeriveInput) -> TokenStream {
let name = &input.ident;
let expanded = quote! {
impl IntoResponse for #name {
fn into_response(self) -> Response {
BaseResponse::create(self)
}
}
};
TokenStream::from(expanded)
}