This commit is contained in:
2025-07-03 15:03:18 -04:00
commit c11ce53576
4 changed files with 175 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
/vendor
session.vim

47
Cargo.lock generated Normal file
View File

@ -0,0 +1,47 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "derive_http"
version = "0.1.0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"

12
Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "derive_http"
version = "0.1.0"
edition = "2024"
[lib]
proc-macro = true
[dependencies]
proc-macro2 = "1.0.95"
quote = "1.0.40"
syn = "2.0.104"

113
src/lib.rs Normal file
View File

@ -0,0 +1,113 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Fields};
/// 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)]
pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;
let expanded = match &input.data {
// ---- Struct: Build .send() that builds and sends the GET request
Data::Struct(data_struct) => {
let fields = match &data_struct.fields {
Fields::Named(named) => &named.named,
_ => 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();
let field_name = ident.to_string();
if field_name.starts_with("lnk_p_") {
let key = &field_name["lnk_p_".len()..];
query_param_code.push(quote! {
query_params.push((#key.to_string(), self.#ident.to_string()));
});
}
}
quote! {
impl #name {
/// Sends a GET request using `awc::Client`.
pub async fn send(
&self,
base_url: &str,
headers: Option<Vec<(&str, &str)>>
) -> Result<awc::ClientResponse, awc::error::SendRequestError> {
use awc::Client;
use urlencoding::encode;
// Collect query parameters
let mut query_params: Vec<(String, String)> = Vec::new();
#(#query_param_code)*
// Build URL with query 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)));
}
url.push('?');
url.push_str(&query_parts.join("&"));
}
// Prepare client and request
let client = Client::default();
let mut request = client.get(url);
// Optional headers
if let Some(hdrs) = headers {
for (k, v) in hdrs {
request = request.append_header((k, v));
}
}
// Send the request
let response = request.send().await?;
Ok(response)
}
}
}
}
// ---- Enum: Match each variant and call .send() on the inner type
Data::Enum(data_enum) => {
let mut variant_arms = Vec::new();
for variant in &data_enum.variants {
let vname = &variant.ident;
match &variant.fields {
Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
variant_arms.push(quote! {
#name::#vname(inner) => inner.send().await,
});
}
_ => panic!(
"#[derive(HttpGetRequest)] enum variants must have a single unnamed field"
),
}
}
quote! {
impl #name {
/// Calls `.send()` on the wrapped query variant.
pub async fn send(&self) -> Result<awc::ClientResponse, awc::error::SendRequestError> {
match self {
#(#variant_arms)*
}
}
}
}
}
_ => panic!("#[derive(HttpGetRequest)] only supports structs and enums"),
};
TokenStream::from(expanded)
}