swan_macro/
lib.rs

1// Swan HTTP Client Library - Procedural Macros
2//
3// This module provides the procedural macros for the Swan HTTP client library.
4// It includes macros for defining HTTP clients and HTTP method handlers.
5
6mod common;
7mod generator;
8mod conversion;
9mod request;
10mod error;
11mod optimization;
12
13use crate::common::common_http_method;
14use crate::generator::generate_http_client_impl;
15use proc_macro::TokenStream;
16use swan_common::{HttpMethod, parse_http_client_args};
17use syn::{ItemStruct, parse_macro_input};
18
19/// HTTP 客户端宏
20/// 
21/// 用于为空结构体生成 HTTP 客户端实现。
22/// 
23/// # 参数
24/// 
25/// * `base_url` - 可选的基础 URL
26/// * `interceptor` - 可选的全局拦截器
27/// * `proxy` - 可选的代理配置(支持 HTTP/HTTPS/SOCKS5)
28/// 
29/// # 示例
30/// 
31/// ```rust
32/// use swan_macro::http_client;
33/// 
34/// // 基本使用
35/// #[http_client(base_url = "https://api.example.com")]
36/// struct ApiClient;
37/// 
38/// // 使用 HTTP 代理
39/// #[http_client(base_url = "https://api.example.com", proxy = "http://proxy.example.com:8080")]
40/// struct ProxyClient;
41/// 
42/// // 使用 SOCKS5 代理
43/// #[http_client(base_url = "https://api.example.com", proxy = "socks5://proxy.example.com:1080")]
44/// struct Socks5Client;
45/// 
46/// // 显式指定代理类型和地址
47/// #[http_client(
48///     base_url = "https://api.example.com", 
49///     proxy(type = http, url = "proxy.example.com:8080")
50/// )]
51/// struct TypedProxyClient;
52/// 
53/// // 使用带认证的 SOCKS5 代理
54/// #[http_client(
55///     base_url = "https://api.example.com", 
56///     proxy(type = socks5, url = "proxy.example.com:1080", username = "user", password = "pass")
57/// )]
58/// struct AuthProxyClient;
59/// 
60/// // 禁用代理
61/// #[http_client(base_url = "https://api.example.com", proxy = false)]
62/// struct NoProxyClient;
63/// ```
64#[proc_macro_attribute]
65pub fn http_client(args: TokenStream, item: TokenStream) -> TokenStream {
66    let input = parse_macro_input!(item as ItemStruct);
67    let args = parse_macro_input!(args with parse_http_client_args);
68
69    match generate_http_client_impl(input, &args) {
70        Ok(tokens) => tokens,
71        Err(error) => error.to_compile_error().into(),
72    }
73}
74
75/// POST 方法宏
76/// 
77/// 用于为方法生成 POST 请求实现。
78/// 
79/// # 参数
80/// 
81/// * `url` - 请求 URL(相对于客户端基础 URL)
82/// * `content_type` - 可选的内容类型
83/// * `header` - 可选的额外头部
84/// * `interceptor` - 可选的方法级拦截器
85/// * `proxy` - 可选的代理配置(覆盖客户端级别配置)
86/// 
87/// # 示例
88/// 
89/// ```rust
90/// use swan_macro::post;
91/// use serde::{Deserialize, Serialize};
92/// 
93/// #[derive(Serialize)]
94/// struct CreateUserRequest {
95///     name: String,
96///     email: String,
97/// }
98/// 
99/// #[derive(Deserialize)]
100/// struct User {
101///     id: u32,
102///     name: String,
103///     email: String,
104/// }
105/// 
106/// impl ApiClient {
107///     #[post(url = "/users", content_type = json)]
108///     async fn create_user(&self, body: CreateUserRequest) -> anyhow::Result<User> {}
109///     
110///     // 为特定方法使用代理
111///     #[post(url = "/secure", content_type = json, proxy = "http://secure-proxy.com:8080")]
112///     async fn secure_request(&self, body: CreateUserRequest) -> anyhow::Result<User> {}
113///     
114///     // 为特定方法禁用代理
115///     #[get(url = "/local", proxy = false)]
116///     async fn local_request(&self) -> anyhow::Result<User> {}
117/// }
118/// ```
119#[proc_macro_attribute]
120pub fn post(args: TokenStream, item: TokenStream) -> TokenStream {
121    common_http_method(args, item, HttpMethod::Post)
122}
123
124/// GET 方法宏
125/// 
126/// 用于为方法生成 GET 请求实现。
127/// 
128/// # 参数
129/// 
130/// * `url` - 请求 URL(相对于客户端基础 URL)
131/// * `header` - 可选的额外头部
132/// * `interceptor` - 可选的方法级拦截器
133/// * `proxy` - 可选的代理配置(覆盖客户端级别配置)
134/// 
135/// # 示例
136/// 
137/// ```rust
138/// use swan_macro::get;
139/// use serde::Deserialize;
140/// 
141/// #[derive(Deserialize)]
142/// struct User {
143///     id: u32,
144///     name: String,
145///     email: String,
146/// }
147/// 
148/// impl ApiClient {
149///     #[get(url = "/users/1")]
150///     async fn get_user(&self) -> anyhow::Result<User> {}
151///     
152///     // 使用 SOCKS5 代理获取敏感数据
153///     #[get(url = "/sensitive", proxy(type = socks5, url = "proxy.example.com:1080"))]
154///     async fn get_sensitive(&self) -> anyhow::Result<User> {}
155/// }
156/// ```
157#[proc_macro_attribute]
158pub fn get(args: TokenStream, item: TokenStream) -> TokenStream {
159    common_http_method(args, item, HttpMethod::Get)
160}
161
162/// PUT 方法宏
163/// 
164/// 用于为方法生成 PUT 请求实现。
165#[proc_macro_attribute]
166pub fn put(args: TokenStream, item: TokenStream) -> TokenStream {
167    common_http_method(args, item, HttpMethod::Put)
168}
169
170/// DELETE 方法宏
171/// 
172/// 用于为方法生成 DELETE 请求实现。
173#[proc_macro_attribute]
174pub fn delete(args: TokenStream, item: TokenStream) -> TokenStream {
175    common_http_method(args, item, HttpMethod::Delete)
176}