Compare commits

..

36 Commits

Author SHA1 Message Date
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
0ba23fad0e ud 2025-07-17 12:18:58 -04:00
37fa009fdd adding error type capability 2025-07-15 11:09:41 -04:00
3d97489712 fixed macro and made take response type attr optionally 2025-07-15 10:57:59 -04:00
1524769694 trying fix 2025-07-15 10:32:08 -04:00
0a88b1d7a1 changed to Q, now actually 2025-07-14 23:32:39 -04:00
4b1fac6403 changed to Q 2025-07-14 23:29:00 -04:00
a9e59b6865 fixed 2025-07-14 23:26:50 -04:00
3e75cd00ff simplified the macros 2025-07-14 23:22:28 -04:00
a096885f02 fixed awc thing 2025-07-14 23:06:29 -04:00
5b3641d930 trying fix for error 2025-07-14 22:55:48 -04:00
981e662bd3 trying fix for error 2025-07-14 22:53:40 -04:00
2c84a8bb18 removing urlencoding because it should be in the main crate 2025-07-14 22:34:51 -04:00
f1a6786419 fixed the enum calls 2025-07-14 22:32:21 -04:00
919c5eda3e fixed deps 2025-07-14 22:29:00 -04:00
8dcd080f5b got rid of base url param 2025-07-14 22:24:46 -04:00
9fc0ab1f0d all good 2025-07-14 22:15:06 -04:00
5aa65e1a38 i think i fixed it from prev commits 2025-07-14 22:12:49 -04:00
b3b813b092 working???? 2025-07-14 21:29:24 -04:00
dbba095e22 working??? 2025-07-14 21:25:53 -04:00
c89d5bf748 working??? 2025-07-14 21:21:29 -04:00
4504e1132b working?? 2025-07-14 21:20:46 -04:00
820f5445a3 working? 2025-07-14 21:18:57 -04:00
e18eb7bb02 ud 2025-07-14 21:14:28 -04:00
745415ba13 fixed to use awc 2025-07-14 21:11:28 -04:00
716a72fffd macro update 2025-07-14 20:39:06 -04:00
8264838b2c hopefully fixed everything 2025-07-14 20:10:31 -04:00
a6f8a94666 made use serde instead of bytes 2025-07-14 20:01:38 -04:00
259c5562e4 update, fixed all warnings, proc macros both work with enums now 2025-07-14 19:51:49 -04:00
d82f663c27 update 2025-07-14 14:49:44 -04:00
e96def50f3 removed the traits so this thing works 2025-07-14 13:26:15 -04:00
533f718e57 added the traits to the crate and made proc macros compatible 2025-07-14 13:18:28 -04:00
f9ee10491d got rid of the Jsonpayload error, switching to generic errors 2025-07-14 13:02:05 -04:00
b11cecdff4 switching back to awc for actix_web 4 2025-07-13 09:45:30 -04:00
20413273aa switched away from awc deps to actix_web deps, testing now 2025-07-11 12:29:34 -04:00
3 changed files with 259 additions and 255 deletions

12
Cargo.lock generated
View File

@ -2,10 +2,22 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "async-trait"
version = "0.1.88"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e539d3fca749fcee5236ab05e93a52867dd549cc157c8cb7f99595f3cedffdb5"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "derive_http" name = "derive_http"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-trait",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",

View File

@ -7,6 +7,7 @@ edition = "2024"
proc-macro = true proc-macro = true
[dependencies] [dependencies]
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"] }

View File

@ -1,190 +1,142 @@
use proc_macro::TokenStream; use proc_macro::TokenStream;
use quote::quote; use quote::quote;
use syn::{parse_macro_input, ItemEnum, Lit, DeriveInput, Fields, Data}; use syn::{parse_macro_input, Lit, ItemEnum, DeriveInput, Fields, Data};
use quote::format_ident;
#[proc_macro_derive(HttpRequest, attributes(http_get))] #[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 name = &input.ident; let query_name = &input.ident;
let query_name_str = query_name.to_string();
// Parse #[http_get(url = "...")] attribute // Parse optional #[http_response = "..."] attribute
let mut response_name_opt: Option<String> = None;
for attr in &input.attrs {
if attr.path().is_ident("http_response") {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("http_response") {
let lit: Lit = meta.value()?.parse()?;
if let Lit::Str(litstr) = lit {
response_name_opt = Some(litstr.value());
}
}
Ok(())
}).unwrap_or_else(|e| panic!("Error parsing http_response attribute: {}", e));
}
}
// Determine response type name
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);
// 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 = "...")]
let mut base_url = None; let mut base_url = None;
for attr in &input.attrs { for attr in &input.attrs {
if attr.path().is_ident("http_get") { if attr.path().is_ident("http_get") {
let _ = attr.parse_nested_meta(|meta| { attr.parse_nested_meta(|meta| {
if meta.path.is_ident("url") { if meta.path.is_ident("url") {
let value: Lit = meta.value()?.parse()?; let lit: Lit = meta.value()?.parse()?;
if let Lit::Str(litstr) = value { if let Lit::Str(litstr) = lit {
base_url = Some(litstr.value()); base_url = Some(litstr.value());
} }
} }
Ok(()) 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 = base_url.expect("Missing #[http_get(url = \"...\")] attribute");
let base_url_lit = syn::LitStr::new(&base_url, proc_macro2::Span::call_site());
let expanded = match &input.data { // Collect query parameters from fields named "lnk_p_*" (only for structs)
Data::Struct(data_struct) => { let query_param_code = if let Data::Struct(data_struct) = &input.data {
let fields = match &data_struct.fields { if let Fields::Named(fields_named) = &data_struct.fields {
Fields::Named(named) => &named.named, fields_named.named.iter().filter_map(|field| {
_ => panic!("#[derive(HttpRequest)] only supports structs with named fields"), let ident = field.ident.as_ref()?;
};
let mut query_param_code = Vec::new();
for field in fields {
let ident = field.ident.clone().unwrap();
let field_name = ident.to_string(); let field_name = ident.to_string();
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()..];
query_param_code.push(quote! { Some(quote! {
query_params.push((#key.to_string(), self.#ident.to_string())); query_params.push((#key.to_string(), self.#ident.to_string()));
}); })
} else {
None
} }
} }).collect::<Vec<_>>()
} else {
quote! { Vec::new()
impl #name {
pub async fn send(
&self,
client: std::sync::Arc<awc::Client>,
headers: Option<Vec<(&str, &str)>>,
api_key: Option<&str>,
) -> Result<awc::ClientResponse, awc::error::SendRequestError> {
use urlencoding::encode;
let mut query_params: Vec<(String, String)> = Vec::new();
#(#query_param_code)*
if let Some(key) = api_key {
query_params.push(("api_key".to_string(), key.to_string()));
}
let mut url = #base_url.to_string();
if !query_params.is_empty() {
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 mut request = client.get(url);
if let Some(hdrs) = headers {
for (k, v) in hdrs {
request = request.append_header((k, v));
}
}
let response = request.send().await?;
Ok(response)
}
}
}
} }
} else {
Data::Enum(data_enum) => { Vec::new()
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(client.clone(), headers.clone(), api_key).await,
});
}
_ => panic!("#[derive(HttpRequest)] enum variants must have a single unnamed field"),
}
}
quote! {
impl #name {
pub async fn send(
&self,
client: std::sync::Arc<awc::Client>,
headers: Option<Vec<(&str, &str)>>,
api_key: Option<&str>,
) -> Result<awc::ClientResponse, awc::error::SendRequestError> {
match self {
#(#variant_arms)*
}
}
}
}
}
_ => panic!("#[derive(HttpRequest)] only supports structs and enums"),
}; };
TokenStream::from(expanded)
}
#[proc_macro_derive(HttpResponse)]
pub fn derive_http_response(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let expanded = quote! { let expanded = quote! {
impl #name { #[async_trait::async_trait]
pub async fn receive(resp: awc::ClientResponse) -> Result<Self, awc::error::JsonPayloadError> { impl Queryable for #query_name {
resp.json().await type R = #response_name;
}
}
};
TokenStream::from(expanded) async fn send(
} &self,
#[proc_macro_derive(SendVec)]
pub fn derive_send_vec(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let expanded = quote! {
impl #name {
/// Sends all items in the vec sequentially, awaiting each.
pub async fn send_vec(
items: Vec<Self>,
client: std::sync::Arc<awc::Client>,
headers: Option<Vec<(&str, &str)>>, headers: Option<Vec<(&str, &str)>>,
api_key: Option<&str>, ) -> Result<Self::R, #error_type> { // Use the error type here
) -> Result<Vec<awc::ClientResponse>, awc::error::SendRequestError> { use awc::Client;
let mut responses = Vec::with_capacity(items.len()); use urlencoding::encode;
for item in items {
let resp = item.send(client.clone(), headers.clone(), api_key).await?; let mut query_params: Vec<(String, String)> = Vec::new();
responses.push(resp); #(#query_param_code)*
let mut url = #base_url_lit.to_string();
if !query_params.is_empty() {
let mut query_string = String::new();
let mut first = true;
for (k, v) in &query_params {
if !first {
query_string.push('&');
}
first = false;
query_string.push_str(&format!("{}={}", k, encode(v)));
}
url.push('?');
url.push_str(&query_string);
} }
Ok(responses)
}
}
};
TokenStream::from(expanded) let client = Client::default();
} let mut request = client.get(url);
#[proc_macro_derive(ResponseVec)] if let Some(hdrs) = headers {
pub fn derive_response_vec(input: TokenStream) -> TokenStream { for (k, v) in hdrs {
let input = parse_macro_input!(input as DeriveInput); request = request.append_header((k, v));
let name = &input.ident; }
let expanded = quote! {
impl #name {
/// Deserializes all responses sequentially into a Vec<Self>.
/// Assumes `Self` implements `DeserializeOwned`.
pub async fn response_vec(
responses: Vec<awc::ClientResponse>,
) -> Result<Vec<Self>, awc::error::JsonPayloadError>
where
Self: Sized + serde::de::DeserializeOwned,
{
let mut results = Vec::with_capacity(responses.len());
for resp in responses {
let item = resp.json::<Self>().await?;
results.push(item);
} }
Ok(results)
let response = request.send().await?;
let bytes = response.body().await?;
let parsed: Self::R = serde_json::from_slice(&bytes)?;
Ok(parsed)
} }
} }
}; };
@ -195,120 +147,159 @@ pub fn derive_response_vec(input: TokenStream) -> TokenStream {
#[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 enum_name = &input_enum.ident; let top_enum_ident = &input_enum.ident;
let variants = &input_enum.variants; let top_variants = &input_enum.variants;
// Match arms for regular command variants (Single, Asset, etc.) // Build outer match arms
let regular_arms = variants.iter().filter_map(|v| { let match_arms: Vec<_> = top_variants.iter().map(|variant| {
let v_name = &v.ident; let variant_ident = &variant.ident;
// Skip Bulk variant — we handle it separately // Expecting tuple variants like Alpaca(AlpacaCmd)
if v_name == "Bulk" { let inner_type = match &variant.fields {
return None; Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
} match &fields.unnamed.first().unwrap().ty {
syn::Type::Path(p) => p.path.segments.last().unwrap().ident.clone(),
Some(quote! { _ => panic!("Expected tuple variant with a type path"),
#enum_name::#v_name(req) => { }
let res = req.send(client.clone(), &api_key).await?;
let body = res.body().await?;
println!("{}", std::str::from_utf8(&body)?);
} }
}) _ => panic!("Each variant must be a tuple variant like `Alpaca(AlpacaCmd)`"),
}); };
quote! {
#top_enum_ident::#variant_ident(inner) => {
match inner {
#inner_type::Bulk { input } => {
let mut reader: Box<dyn std::io::Read> = match input {
Some(path) => Box::new(std::fs::File::open(path)?),
None => Box::new(std::io::stdin()),
};
let mut buf = String::new();
reader.read_to_string(&mut buf)?;
let queries: Vec<#inner_type> = serde_json::from_str(&buf)?;
use std::sync::Arc;
let client = Arc::new(awc::Client::default());
let keys = Arc::new(crate::load_api_keys()?);
const THREADS: usize = 4;
let total = queries.len();
let per_thread = std::cmp::max(1, total / THREADS);
let shared_queries = Arc::new(queries);
let mut handles = Vec::new();
for i in 0..THREADS {
let queries = Arc::clone(&shared_queries);
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 };
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();
}
});
handles.push(handle);
}
for h in handles {
h.join().expect("Thread panicked");
}
}
other => {
let client = awc::Client::default();
let keys = crate::load_api_keys()?;
other.send_all(&client, &keys).await?;
}
}
}
}
}).collect();
// Generate the final code
let expanded = quote! { let expanded = quote! {
#[derive(structopt::StructOpt, Debug)] use clap::Parser;
use std::io::Read;
#[derive(clap::Parser, Debug)]
#input_enum #input_enum
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
use structopt::StructOpt; let cmd = #top_enum_ident::parse();
use std::fs::File;
use std::io::{BufReader, Read};
use std::sync::Arc;
use std::thread;
const THREADS: usize = 4;
// Initialize shared HTTP client and API key
let client = Arc::new(awc::Client::default());
let api_key = std::env::var("APCA_API_KEY_ID")?;
let cmd = #enum_name::from_args();
match cmd { match cmd {
#(#regular_arms)* #(#match_arms),*
#enum_name::Bulk { input } => {
// Choose input source: file or stdin
let mut reader: Box<dyn Read> = match input {
Some(path) => Box::new(File::open(path)?),
None => Box::new(std::io::stdin()),
};
// Read input JSON into buffer
let mut buf = String::new();
reader.read_to_string(&mut buf)?;
// Deserialize into Vec<Query>
let queries: Vec<Query> = serde_json::from_str(&buf)?;
let total = queries.len();
if total == 0 {
eprintln!("No queries provided.");
return Ok(());
}
let shared_queries = Arc::new(queries);
let shared_key = Arc::new(api_key);
let per_thread = total / THREADS;
let mut handles = Vec::with_capacity(THREADS);
for i in 0..THREADS {
let queries_clone = Arc::clone(&shared_queries);
let client_clone = Arc::clone(&client);
let key_clone = Arc::clone(&shared_key);
let start_index = i * per_thread;
let end_index = if i == THREADS - 1 {
total // Last thread gets the remainder
} else {
start_index + per_thread
};
let handle = thread::spawn(move || {
let rt = tokio::runtime::Runtime::new().expect("Failed to create runtime");
for idx in start_index..end_index {
let query = &queries_clone[idx];
let send_result = rt.block_on(query.send(client_clone.clone(), &key_clone));
match send_result {
Ok(response) => {
let body_result = rt.block_on(response.body());
match body_result {
Ok(body) => println!("{}", String::from_utf8_lossy(&body)),
Err(e) => eprintln!("Error reading response body: {:?}", e),
}
}
Err(e) => {
eprintln!("Request failed: {:?}", e);
}
}
}
});
handles.push(handle);
}
// Wait for all threads to complete
for handle in handles {
handle.join().expect("A thread panicked");
}
}
} }
Ok(()) Ok(())
} }
// Trait for dispatching API calls
pub trait ApiDispatch {
fn send_all(
&self,
client: &awc::Client,
keys: &std::collections::HashMap<String, crate::Keys>,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), Box<dyn std::error::Error>>> + Send>>;
}
};
TokenStream::from(expanded)
}
#[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(
&self,
client: &awc::Client,
keys: &std::collections::HashMap<String, crate::Keys>,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), Box<dyn std::error::Error>>> + Send>> {
Box::pin(async move {
match self {
#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(())
})
}
}
}; };
TokenStream::from(expanded) TokenStream::from(expanded)