fixed macro?
This commit is contained in:
57
src/lib.rs
57
src/lib.rs
@ -1,16 +1,33 @@
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, Data, DeriveInput, Fields};
|
||||
use syn::{parse_macro_input, Attribute, Data, DeriveInput, Fields, Meta, NestedMeta};
|
||||
|
||||
/// Derives a `.send()` method for API query structs and enums using `awc::Client`.
|
||||
///
|
||||
/// - Structs: sends GET requests using fields prefixed with `lnk_p_` as query parameters.
|
||||
/// - Enums: delegates `.send()` to inner struct that implements `Queryable`.
|
||||
#[proc_macro_derive(HttpGetRequest)]
|
||||
#[proc_macro_derive(HttpGetRequest, attributes(http_get))]
|
||||
pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let name = input.ident;
|
||||
|
||||
// Extract the #[http_get(url = "...")] attribute
|
||||
let mut base_url = None;
|
||||
for attr in &input.attrs {
|
||||
if attr.path().is_ident("http_get") {
|
||||
let meta = attr.parse_meta().expect("Invalid #[http_get] syntax");
|
||||
if let Meta::List(meta_list) = meta {
|
||||
for nested in meta_list.nested {
|
||||
if let NestedMeta::Meta(Meta::NameValue(nv)) = nested {
|
||||
if nv.path.is_ident("url") {
|
||||
if let syn::Lit::Str(lit_str) = nv.lit {
|
||||
base_url = Some(lit_str.value());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let base_url = base_url.expect("Missing #[http_get(url = \"...\")] attribute");
|
||||
|
||||
let expanded = match &input.data {
|
||||
Data::Struct(data_struct) => {
|
||||
let fields = match &data_struct.fields {
|
||||
@ -18,7 +35,6 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
||||
_ => panic!("#[derive(HttpGetRequest)] supports only named fields for structs"),
|
||||
};
|
||||
|
||||
// Gather query parameters from fields prefixed with lnk_p_
|
||||
let mut query_param_code = Vec::new();
|
||||
for field in fields {
|
||||
let ident = field.ident.clone().unwrap();
|
||||
@ -33,36 +49,30 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
||||
|
||||
quote! {
|
||||
impl #name {
|
||||
/// Sends a GET request using `awc::Client`.
|
||||
pub async fn send(
|
||||
&self,
|
||||
base_url: &str,
|
||||
client: std::sync::Arc<awc::Client>,
|
||||
headers: Option<Vec<(&str, &str)>>,
|
||||
pgp_key: Option<&str>,
|
||||
api_key: Option<&str>,
|
||||
) -> Result<awc::ClientResponse, awc::error::SendRequestError> {
|
||||
use awc::Client;
|
||||
use urlencoding::encode;
|
||||
|
||||
let mut query_params: Vec<(String, String)> = Vec::new();
|
||||
#(#query_param_code)*
|
||||
|
||||
// Add optional api_key to query string
|
||||
if let Some(key) = api_key {
|
||||
query_params.push(("api_key".to_string(), key.to_string()));
|
||||
}
|
||||
|
||||
let mut url = base_url.to_string();
|
||||
let mut url = #base_url.to_string();
|
||||
if !query_params.is_empty() {
|
||||
let mut query_parts = Vec::new();
|
||||
for (k, v) in &query_params {
|
||||
query_parts.push(format!("{}={}", k, encode(v)));
|
||||
}
|
||||
let query_parts: Vec<String> = query_params.iter()
|
||||
.map(|(k, v)| format!("{}={}", k, encode(v)))
|
||||
.collect();
|
||||
url.push('?');
|
||||
url.push_str(&query_parts.join("&"));
|
||||
}
|
||||
|
||||
let client = Client::default();
|
||||
let mut request = client.get(url);
|
||||
|
||||
if let Some(hdrs) = headers {
|
||||
@ -71,11 +81,6 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
||||
}
|
||||
}
|
||||
|
||||
// Add PGP key as a custom header, if provided
|
||||
if let Some(pgp) = pgp_key {
|
||||
request = request.append_header(("X-PGP-Key", pgp));
|
||||
}
|
||||
|
||||
let response = request.send().await?;
|
||||
Ok(response)
|
||||
}
|
||||
@ -83,7 +88,6 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
||||
}
|
||||
}
|
||||
|
||||
// Enum handling unchanged...
|
||||
Data::Enum(data_enum) => {
|
||||
let mut variant_arms = Vec::new();
|
||||
for variant in &data_enum.variants {
|
||||
@ -91,7 +95,7 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
||||
match &variant.fields {
|
||||
Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
|
||||
variant_arms.push(quote! {
|
||||
#name::#vname(inner) => inner.send(base_url, headers.clone(), pgp_key, api_key).await,
|
||||
#name::#vname(inner) => inner.send(client.clone(), headers.clone(), api_key).await,
|
||||
});
|
||||
}
|
||||
_ => panic!(
|
||||
@ -104,9 +108,8 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
|
||||
impl #name {
|
||||
pub async fn send(
|
||||
&self,
|
||||
base_url: &str,
|
||||
client: std::sync::Arc<awc::Client>,
|
||||
headers: Option<Vec<(&str, &str)>>,
|
||||
pgp_key: Option<&str>,
|
||||
api_key: Option<&str>,
|
||||
) -> Result<awc::ClientResponse, awc::error::SendRequestError> {
|
||||
match self {
|
||||
|
Reference in New Issue
Block a user