1 unstable release
new 0.1.0 | Apr 11, 2025 |
---|
#1662 in Procedural macros
105 downloads per month
12KB
168 lines
kpl-derive
A Rust procedural macro library for generating API client code for stock API endpoints.
Features
ApiEndpoint
derive macro for generating API client code- Customizable endpoint configuration via attributes
- Support for different HTTP methods and response types
- Automatic serialization and deserialization of request parameters and responses
Installation
Add this to your Cargo.toml
:
[dependencies]
kpl-derive = "0.1.1"
Usage
use kpl_derive::ApiEndpoint;
use serde::{Deserialize, Serialize};
// Define your response type
#[derive(Debug, Deserialize)]
struct MyResponseType {
// Your response fields here
data: String,
}
// Define your API endpoint
#[derive(ApiEndpoint, Serialize)]
#[endpoint(name = "My API Endpoint", method = "GET", path = "/api/endpoint", resp = MyResponseType)]
struct MyApiEndpoint {
// Your request parameters here
#[serde(rename = "param_name")]
param: String,
}
// Use the generated code
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let endpoint = MyApiEndpoint {
param: "value".to_string(),
};
// Execute the API call
let response = endpoint.execute().await?;
println!("Response: {:?}", response);
Ok(())
}
Attribute Options
The #[endpoint(...)]
attribute supports the following options:
name
: A display name for the endpoint (required)method
: HTTP method to use (default: "GET")path
: API path (default: "/w1/api/index.php")host
: API host (default: "apphis.longhuvip.com")resp
: Response type (default: serde_json::Value)
Dependencies
~0.5–1MB
~22K SLoC