| Crates.io | swan-common |
| lib.rs | swan-common |
| version | 0.3.6 |
| created_at | 2025-03-16 11:18:44.57504+00 |
| updated_at | 2025-09-06 09:33:28.233939+00 |
| description | Common utilities and types for the Swan HTTP client library. |
| homepage | |
| repository | https://github.com/red-swan-rust/swan-http.git |
| max_upload_size | |
| id | 1594351 |
| size | 102,171 |
π Languages: English | δΈζ
Swan Common is the core component of the Swan HTTP library, providing shared type definitions, interceptor interfaces, retry mechanisms, and other foundational features.
Add the following to your Cargo.toml:
[dependencies]
swan-common = "0.3"
async-trait = "0.1"
anyhow = "1.0"
use swan_common::{HttpMethod, ContentType};
let method = HttpMethod::Get;
let content_type = ContentType::Json;
use async_trait::async_trait;
use swan_common::SwanInterceptor;
use std::borrow::Cow;
#[derive(Default)]
struct MyInterceptor;
#[async_trait]
impl SwanInterceptor for MyInterceptor {
async fn before_request<'a>(
&self,
request: reqwest::RequestBuilder,
request_body: &'a [u8],
) -> anyhow::Result<(reqwest::RequestBuilder, Cow<'a, [u8]>)> {
// Zero-copy: only modify request body when needed
Ok((request, Cow::Borrowed(request_body)))
}
async fn after_response(
&self,
response: reqwest::Response,
) -> anyhow::Result<reqwest::Response> {
println!("Response status: {}", response.status());
Ok(response)
}
}
use swan_common::{RetryPolicy, RetryConfig};
use syn::LitStr;
// Create exponential retry strategy
let policy = RetryPolicy::exponential(3, 100); // 3 retries, base delay 100ms
// Parse retry configuration from string
let config_str: LitStr = syn::parse_quote!("exponential(5, 200ms)");
let retry_config = RetryConfig::parse(&config_str)?;
// Simplified format
"exponential(3, 100ms)" // 3 retries, base delay 100ms
"fixed(max_attempts=4, delay=1s)" // 4 retries, fixed delay 1 second
// Detailed format
"exponential(
max_attempts=5,
base_delay=200ms,
max_delay=30s,
exponential_base=2.0,
jitter_ratio=0.1,
idempotent_only=true
)"
Cow<[u8]> to avoid unnecessary memory copyingRetryPolicy memory footprint β€ 64 bytesRun tests:
cargo test --lib
Detailed API documentation:
cargo doc --open
Swan Common is typically used with Swan Macro:
[dependencies]
swan-common = "0.3"
swan-macro = "0.3"
This project is licensed under the GPL-3.0 License. See the LICENSE file for details.