From c11ce53576676cfc730ef956424d2e1659ff144d Mon Sep 17 00:00:00 2001 From: buckn Date: Thu, 3 Jul 2025 15:03:18 -0400 Subject: [PATCH] init --- .gitignore | 3 ++ Cargo.lock | 47 ++++++++++++++++++++++ Cargo.toml | 12 ++++++ src/lib.rs | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8517846 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +/vendor +session.vim diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c0f9980 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ef580d5 --- /dev/null +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..3e62974 --- /dev/null +++ b/src/lib.rs @@ -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> + ) -> Result { + 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 { + match self { + #(#variant_arms)* + } + } + } + } + } + + _ => panic!("#[derive(HttpGetRequest)] only supports structs and enums"), + }; + + TokenStream::from(expanded) +}