adding error type capability
This commit is contained in:
22
src/lib.rs
22
src/lib.rs
@ -3,13 +3,13 @@ use quote::quote;
|
|||||||
use syn::{parse_macro_input, Lit, ItemEnum, DeriveInput, Fields, Data};
|
use syn::{parse_macro_input, Lit, ItemEnum, DeriveInput, Fields, Data};
|
||||||
use quote::format_ident;
|
use quote::format_ident;
|
||||||
|
|
||||||
#[proc_macro_derive(HttpRequest, attributes(http_get, http_response))]
|
#[proc_macro_derive(HttpRequest, attributes(http_get, http_response, http_error_type))]
|
||||||
pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
let query_name = &input.ident;
|
let query_name = &input.ident;
|
||||||
let query_name_str = query_name.to_string();
|
let query_name_str = query_name.to_string();
|
||||||
|
|
||||||
// Parse optional #[http_response = "..."] attribute via parse_nested_meta
|
// Parse optional #[http_response = "..."] attribute
|
||||||
let mut response_name_opt: Option<String> = None;
|
let mut response_name_opt: Option<String> = None;
|
||||||
for attr in &input.attrs {
|
for attr in &input.attrs {
|
||||||
if attr.path().is_ident("http_response") {
|
if attr.path().is_ident("http_response") {
|
||||||
@ -37,6 +37,22 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
|||||||
};
|
};
|
||||||
let response_name = format_ident!("{}", response_name_str);
|
let response_name = format_ident!("{}", response_name_str);
|
||||||
|
|
||||||
|
// Parse optional #[http_error_type = "..."] attribute (default to `current_mod::E`)
|
||||||
|
let mut error_type = syn::Path::from(syn::Ident::new("E", proc_macro2::Span::call_site()));
|
||||||
|
for attr in &input.attrs {
|
||||||
|
if attr.path().is_ident("http_error_type") {
|
||||||
|
attr.parse_nested_meta(|meta| {
|
||||||
|
if meta.path.is_ident("http_error_type") {
|
||||||
|
let lit: Lit = meta.value()?.parse()?;
|
||||||
|
if let Lit::Str(litstr) = lit {
|
||||||
|
error_type = syn::parse_str(&litstr.value()).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}).unwrap_or_else(|e| panic!("Error parsing http_error_type attribute: {}", e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Extract base URL from #[http_get(url = "...")]
|
// Extract base URL from #[http_get(url = "...")]
|
||||||
let mut base_url = None;
|
let mut base_url = None;
|
||||||
for attr in &input.attrs {
|
for attr in &input.attrs {
|
||||||
@ -85,7 +101,7 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
|||||||
async fn send(
|
async fn send(
|
||||||
&self,
|
&self,
|
||||||
headers: Option<Vec<(&str, &str)>>,
|
headers: Option<Vec<(&str, &str)>>,
|
||||||
) -> Result<Self::R, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<Self::R, #error_type> { // Use the error type here
|
||||||
use awc::Client;
|
use awc::Client;
|
||||||
use urlencoding::encode;
|
use urlencoding::encode;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user