Compare commits

..

5 Commits

Author SHA1 Message Date
fc96d91c77 random ud 2025-08-20 19:18:46 -04:00
32b1aa7e89 ud 2025-08-11 19:27:34 -04:00
b37f386fe8 ud 2025-06-30 12:11:49 -04:00
83d5d85962 added final macro to auto implement cli from api types 2025-07-18 14:14:07 -04:00
41f85afa6c ud, fixed errors 2025-07-18 13:34:27 -04:00
3 changed files with 1308 additions and 115 deletions

1203
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
[package] [package]
name = "derive_http" name = "http_derive"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2024"
@ -11,3 +11,4 @@ async-trait = "0.1.88"
proc-macro2 = "1.0.95" proc-macro2 = "1.0.95"
quote = "1.0.40" quote = "1.0.40"
syn = { version = "2.0.104", features = ["full"] } syn = { version = "2.0.104", features = ["full"] }
http_core = { path = "../http_core" }

View File

@ -2,8 +2,9 @@ use proc_macro::TokenStream;
use quote::quote; 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;
use http_core::{Queryable, ApiDispatch, HasHttp, Keys};
#[proc_macro_derive(HttpRequest, attributes(http_get, http_response, http_error_type))] #[proc_macro_derive(HttpRequest, attributes(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;
@ -25,19 +26,10 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
} }
} }
// Determine response type name let response_name_str = response_name_opt.unwrap_or_else(|| format!("{}Resp", query_name_str));
let response_name_str = if let Some(custom_resp) = response_name_opt {
custom_resp
} else if query_name_str == "Q" {
"R".to_string()
} else if query_name_str.ends_with('Q') {
format!("{}R", &query_name_str[..query_name_str.len() - 1])
} else {
panic!("HttpRequest derive macro expects the type name to be 'Q' or end with 'Q', or specify #[http_response = \"...\"] to override");
};
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`) // Parse optional #[http_error_type = "..."] attribute (default to `E`)
let mut error_type = syn::Path::from(syn::Ident::new("E", proc_macro2::Span::call_site())); let mut error_type = syn::Path::from(syn::Ident::new("E", proc_macro2::Span::call_site()));
for attr in &input.attrs { for attr in &input.attrs {
if attr.path().is_ident("http_error_type") { if attr.path().is_ident("http_error_type") {
@ -53,25 +45,7 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
} }
} }
// Extract base URL from #[http_get(url = "...")] // Collect query parameters from lnk_p_* fields
let mut base_url = None;
for attr in &input.attrs {
if attr.path().is_ident("http_get") {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("url") {
let lit: Lit = meta.value()?.parse()?;
if let Lit::Str(litstr) = lit {
base_url = Some(litstr.value());
}
}
Ok(())
}).unwrap_or_else(|e| panic!("Error parsing http_get attribute: {}", e));
}
}
let base_url = base_url.expect("Missing #[http_get(url = \"...\")] attribute");
let base_url_lit = syn::LitStr::new(&base_url, proc_macro2::Span::call_site());
// Collect query parameters from fields named "lnk_p_*" (only for structs)
let query_param_code = if let Data::Struct(data_struct) = &input.data { let query_param_code = if let Data::Struct(data_struct) = &input.data {
if let Fields::Named(fields_named) = &data_struct.fields { if let Fields::Named(fields_named) = &data_struct.fields {
fields_named.named.iter().filter_map(|field| { fields_named.named.iter().filter_map(|field| {
@ -80,7 +54,9 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
if field_name.starts_with("lnk_p_") { if field_name.starts_with("lnk_p_") {
let key = &field_name["lnk_p_".len()..]; let key = &field_name["lnk_p_".len()..];
Some(quote! { Some(quote! {
query_params.push((#key.to_string(), self.#ident.to_string())); if let Some(val) = &self.#ident {
query_params.push((#key.to_string(), val.to_string()));
}
}) })
} else { } else {
None None
@ -100,15 +76,16 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
async fn send( async fn send(
&self, &self,
base_url: &str,
headers: Option<Vec<(&str, &str)>>, headers: Option<Vec<(&str, &str)>>,
) -> Result<Self::R, #error_type> { // Use the error type here ) -> Result<Self::R, #error_type> {
use awc::Client; use awc::Client;
use urlencoding::encode; use urlencoding::encode;
let mut query_params: Vec<(String, String)> = Vec::new(); let mut query_params: Vec<(String, String)> = Vec::new();
#(#query_param_code)* #(#query_param_code)*
let mut url = #base_url_lit.to_string(); let mut url = base_url.to_string();
if !query_params.is_empty() { if !query_params.is_empty() {
let mut query_string = String::new(); let mut query_string = String::new();
let mut first = true; let mut first = true;
@ -134,7 +111,6 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
let response = request.send().await?; let response = request.send().await?;
let bytes = response.body().await?; let bytes = response.body().await?;
let parsed: Self::R = serde_json::from_slice(&bytes)?; let parsed: Self::R = serde_json::from_slice(&bytes)?;
Ok(parsed) Ok(parsed)
} }
@ -144,88 +120,83 @@ pub fn derive_http_get_request(input: TokenStream) -> TokenStream {
TokenStream::from(expanded) TokenStream::from(expanded)
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn alpaca_cli(_attr: TokenStream, item: TokenStream) -> TokenStream { pub fn alpaca_cli(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input_enum = parse_macro_input!(item as ItemEnum); let input_enum = parse_macro_input!(item as ItemEnum);
let top_enum_ident = &input_enum.ident; // e.g., "Cmd" let top_enum_ident = &input_enum.ident;
let top_variants = &input_enum.variants; let top_variants = &input_enum.variants;
// For each variant like Alpaca(AlpacaCmd) // Build outer match arms
let outer_match_arms = top_variants.iter().map(|v| { let match_arms: Vec<_> = top_variants.iter().map(|variant| {
let variant_ident = &v.ident; // e.g., "Alpaca" let variant_ident = &variant.ident;
// Extract the inner sub-enum type like AlpacaCmd // Expecting tuple variants like Alpaca(AlpacaCmd)
if let syn::Fields::Unnamed(fields) = &v.fields { let inner_type = match &variant.fields {
if let Some(field) = fields.unnamed.first() { Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
if let syn::Type::Path(inner_type) = &field.ty { match &fields.unnamed.first().unwrap().ty {
let inner_type_ident = &inner_type.path.segments.last().unwrap().ident; syn::Type::Path(p) => p.path.segments.last().unwrap().ident.clone(),
_ => panic!("Expected tuple variant with a type path"),
}
}
_ => panic!("Each variant must be a tuple variant like `Alpaca(AlpacaCmd)`"),
};
// Match arms inside the nested enum (AlpacaCmd) quote! {
let inner_match_arm = quote! { #top_enum_ident::#variant_ident(inner) => {
match #inner_type_ident::parse() { match inner {
#inner_type_ident::Bulk { input } => { #inner_type::Bulk { input } => {
// Bulk: read and parse Vec<inner_type_ident> let mut reader: Box<dyn std::io::Read> = match input {
let mut reader: Box<dyn std::io::Read> = match input { Some(path) => Box::new(std::fs::File::open(path)?),
Some(path) => Box::new(std::fs::File::open(path)?), None => Box::new(std::io::stdin()),
None => Box::new(std::io::stdin()), };
};
let mut buf = String::new();
reader.read_to_string(&mut buf)?;
let queries: Vec<#inner_type_ident> = serde_json::from_str(&buf)?;
use std::sync::Arc; let mut buf = String::new();
let client = Arc::new(awc::Client::default()); reader.read_to_string(&mut buf)?;
let api_keys = Arc::new(crate::load_api_keys()?); let queries: Vec<#inner_type> = serde_json::from_str(&buf)?;
const THREADS: usize = 4; use std::sync::Arc;
let total = queries.len(); let client = Arc::new(awc::Client::default());
let per_thread = total / THREADS; let keys = Arc::new(crate::load_api_keys()?);
let shared_queries = Arc::new(queries);
let mut handles = Vec::new(); const THREADS: usize = 4;
for i in 0..THREADS { let total = queries.len();
let queries = Arc::clone(&shared_queries); let per_thread = std::cmp::max(1, total / THREADS);
let client = Arc::clone(&client); let shared_queries = Arc::new(queries);
let keys = Arc::clone(&api_keys);
let start = i * per_thread;
let end = if i == THREADS - 1 { total } else { start + per_thread };
let handle = std::thread::spawn(move || { let mut handles = Vec::new();
let rt = tokio::runtime::Runtime::new().unwrap(); for i in 0..THREADS {
for q in &queries[start..end] { let queries = Arc::clone(&shared_queries);
rt.block_on(q.send_all(&client, &keys)); let client = Arc::clone(&client);
} let keys = Arc::clone(&keys);
}); let start = i * per_thread;
let end = if i == THREADS - 1 { total } else { start + per_thread };
handles.push(handle); let handle = std::thread::spawn(move || {
let rt = tokio::runtime::Runtime::new().unwrap();
for q in &queries[start..end] {
rt.block_on(q.send_all(&client, &keys)).unwrap();
} }
});
for h in handles { handles.push(handle);
h.join().expect("Thread panicked");
}
}
other => {
let client = awc::Client::default();
let keys = crate::load_api_keys()?;
other.send_all(&client, &keys).await?;
}
} }
};
// Wrap the outer enum match (Cmd::Alpaca(inner)) for h in handles {
return quote! { h.join().expect("Thread panicked");
#top_enum_ident::#variant_ident(inner) => {
#inner_match_arm
} }
}; }
other => {
let client = awc::Client::default();
let keys = crate::load_api_keys()?;
other.send_all(&client, &keys).await?;
}
} }
} }
} }
}).collect();
panic!("Each outer enum variant must be a tuple variant like `Alpaca(AlpacaCmd)`"); // Generate the final code
});
// Generate the final program
let expanded = quote! { let expanded = quote! {
use clap::Parser; use clap::Parser;
use std::io::Read; use std::io::Read;
@ -237,22 +208,40 @@ pub fn alpaca_cli(_attr: TokenStream, item: TokenStream) -> TokenStream {
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let cmd = #top_enum_ident::parse(); let cmd = #top_enum_ident::parse();
match cmd { match cmd {
#(#outer_match_arms),* #(#match_arms),*
} }
Ok(()) Ok(())
} }
// Helper trait to unify async calls on sub-commands // Trait for dispatching API calls
trait ApiDispatch { pub trait ApiDispatch {
fn send_all( fn send_all(
&self, &self,
client: &awc::Client, client: &awc::Client,
keys: &std::collections::HashMap<String, crate::Keys>, keys: &std::collections::HashMap<String, crate::Keys>,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), Box<dyn std::error::Error>>> + Send>>; ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), Box<dyn std::error::Error>>> + Send>>;
} }
};
// Implement ApiDispatch for every subcommand variant TokenStream::from(expanded)
#(impl ApiDispatch for #top_enum_ident { }
#[proc_macro_attribute]
pub fn api_dispatch(attr: TokenStream, item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as syn::ItemEnum);
let enum_ident = &input.ident;
// Parse attribute input: input = "MyQuery"
let meta_args = attr.to_string();
let input_type: syn::Ident = {
let cleaned = meta_args.trim().replace("input", "").replace('=', "").replace('"', "").trim().to_string();
syn::Ident::new(&cleaned, proc_macro2::Span::call_site())
};
let expanded = quote! {
#input
impl ApiDispatch for #enum_ident {
fn send_all( fn send_all(
&self, &self,
client: &awc::Client, client: &awc::Client,
@ -260,12 +249,34 @@ pub fn alpaca_cli(_attr: TokenStream, item: TokenStream) -> TokenStream {
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), Box<dyn std::error::Error>>> + Send>> { ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), Box<dyn std::error::Error>>> + Send>> {
Box::pin(async move { Box::pin(async move {
match self { match self {
#(#outer_match_arms),* #enum_ident::Single { query } => {
let parsed: #input_type = serde_json::from_str(query)?;
parsed.send(client, keys).await?;
}
#enum_ident::Bulk { input } => {
let json = if let Some(raw) = input {
if std::path::Path::new(&raw).exists() {
std::fs::read_to_string(raw)?
} else {
raw.clone()
}
} else {
use std::io::Read;
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf)?;
buf
};
let items: Vec<#input_type> = serde_json::from_str(&json)?;
for item in items {
item.send(client, keys).await?;
}
}
} }
Ok(()) Ok(())
}) })
} }
})* }
}; };
TokenStream::from(expanded) TokenStream::from(expanded)