Swan Macro
π Languages: English | δΈζ
Swan Macro is the procedural macro component of the Swan HTTP library, providing declarative HTTP client definition syntax.
π Core Features
- Declarative Client Definition: Define HTTP clients and methods using macro annotations
- Automatic Code Generation: Generate high-performance HTTP client code at compile time
- Smart Retry Mechanism: Method-level progressive exponential backoff retry
- Interceptor Integration: Seamless integration of global and method-level interceptors
- Dynamic Parameter Support: Parameter placeholders in URLs and headers
- State Injection: Axum-like application state management
π¦ Installation
Add the following to your Cargo.toml:
[]
= "0.2"
= "0.2" # Required runtime dependency
= { = "1.0", = ["derive"] }
= "1.0"
= { = "1.0", = ["macros", "rt-multi-thread"] }
π Quick Start
Basic Usage
use ;
use ;
// Define HTTP client
;
async
π§ Supported Macros
#[http_client]
Define HTTP client struct:
;
HTTP Method Macros
Supported HTTP methods:
#[get(url = "...")]- GET requests#[post(url = "...", content_type = json)]- POST requests#[put(url = "...", content_type = json)]- PUT requests#[delete(url = "...")]- DELETE requests
Method Parameters
π Retry Mechanism
Retry Strategy Types
// Exponential backoff retry
// Fixed delay retry
Automatic Retry Conditions
- 5xx Server Errors (500-599)
- 429 Too Many Requests (rate limiting)
- 408 Request Timeout (timeout)
- Network Connection Errors
Idempotency Protection
By default, only safe HTTP methods are retried:
// β
Auto retry
// β
Auto retry
// β
Auto retry
// β No retry by default (non-idempotent)
// Force retry for non-idempotent methods (use with caution)
π Dynamic Parameters
URL Parameters
// Path parameters
async
// Query parameters
async
// Positional parameter reference
async
Dynamic Headers
async
π Interceptor Integration
use async_trait;
use SwanInterceptor;
;
// Use interceptor
;
π·οΈ Content Types
Supported content types:
content_type = json- application/jsoncontent_type = form_urlencoded- application/x-www-form-urlencodedcontent_type = form_multipart- multipart/form-data
β‘ Compile-Time Optimization
Swan Macro generates highly optimized code at compile time:
- Zero Runtime Overhead: All configuration determined at compile time
- Inline Optimization: Automatically inline small function calls
- Conditional Compilation: Remove debug code in release mode
- Smart Caching: Interceptor instance reuse
π§ͺ Testing
Run tests:
π Documentation
Detailed API documentation:
π€ Use with Swan Common
Swan Macro depends on Swan Common for runtime support:
[]
= "0.2"
= "0.2"
π License
This project is licensed under the GPL-3.0 License. See the LICENSE file for details.