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
Swan HTTP provides intelligent method-level retry mechanisms with exponential backoff and fixed delay strategies.
Quick Start
// π Simplest config - exponential retry, 3 attempts, 100ms base delay
// π Fixed delay - 3 attempts, 1 second each
// π Detailed config - recommended for production
Syntax Formats
| Format | Example | Description |
|---|---|---|
| Simplified | "exponential(3, 100ms)" |
Quick config with positional args |
| Complete | "exponential(max_attempts=3, base_delay=100ms)" |
Named parameters, recommended for production |
Key Features
- Auto retry conditions: 5xx errors, 429 rate limiting, 408 timeout, network errors
- Idempotency protection: GET/PUT/DELETE auto retry, POST default no retry
- Time unit support:
ms(milliseconds),s(seconds) - Compile-time validation: Configuration errors caught at compile time
π Detailed Documentation: See Complete Retry Guide for all parameters, best practices, and troubleshooting
π 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.