added the receive macro to auto impl serde deserialize/await
This commit is contained in:
24
src/lib.rs
24
src/lib.rs
@ -2,7 +2,7 @@ use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, DeriveInput, Data, Fields, Lit};
|
||||
|
||||
#[proc_macro_derive(HttpGetRequest, attributes(http_get))]
|
||||
#[proc_macro_derive(HttpRequest, attributes(http_get))]
|
||||
pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let name = &input.ident;
|
||||
@ -29,7 +29,7 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
||||
Data::Struct(data_struct) => {
|
||||
let fields = match &data_struct.fields {
|
||||
Fields::Named(named) => &named.named,
|
||||
_ => panic!("#[derive(HttpGetRequest)] only supports structs with named fields"),
|
||||
_ => panic!("#[derive(HttpRequest)] only supports structs with named fields"),
|
||||
};
|
||||
|
||||
let mut query_param_code = Vec::new();
|
||||
@ -95,7 +95,7 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
||||
#name::#vname(inner) => inner.send(client.clone(), headers.clone(), api_key).await,
|
||||
});
|
||||
}
|
||||
_ => panic!("#[derive(HttpGetRequest)] enum variants must have a single unnamed field"),
|
||||
_ => panic!("#[derive(HttpRequest)] enum variants must have a single unnamed field"),
|
||||
}
|
||||
}
|
||||
|
||||
@ -115,7 +115,23 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
||||
}
|
||||
}
|
||||
|
||||
_ => panic!("#[derive(HttpGetRequest)] only supports structs and enums"),
|
||||
_ => panic!("#[derive(HttpRequest)] only supports structs and enums"),
|
||||
};
|
||||
|
||||
TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
#[proc_macro_derive(HttpResponse)]
|
||||
pub fn derive_http_response(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let name = &input.ident;
|
||||
|
||||
let expanded = quote! {
|
||||
impl #name {
|
||||
pub async fn receive(resp: awc::ClientResponse) -> Result<Self, awc::error::JsonPayloadError> {
|
||||
resp.json().await
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TokenStream::from(expanded)
|
||||
|
Reference in New Issue
Block a user