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/// 
28/// # 示例
29/// 
30/// ```rust
31/// use swan_macro::http_client;
32/// 
33/// #[http_client(base_url = "https://api.example.com")]
34/// struct ApiClient;
35/// ```
36#[proc_macro_attribute]
37pub fn http_client(args: TokenStream, item: TokenStream) -> TokenStream {
38    let input = parse_macro_input!(item as ItemStruct);
39    let args = parse_macro_input!(args with parse_http_client_args);
40
41    match generate_http_client_impl(input, &args) {
42        Ok(tokens) => tokens,
43        Err(error) => error.to_compile_error().into(),
44    }
45}
46
47/// POST 方法宏
48/// 
49/// 用于为方法生成 POST 请求实现。
50/// 
51/// # 参数
52/// 
53/// * `url` - 请求 URL(相对于客户端基础 URL)
54/// * `content_type` - 可选的内容类型
55/// * `header` - 可选的额外头部
56/// * `interceptor` - 可选的方法级拦截器
57/// 
58/// # 示例
59/// 
60/// ```rust
61/// use swan_macro::post;
62/// use serde::{Deserialize, Serialize};
63/// 
64/// #[derive(Serialize)]
65/// struct CreateUserRequest {
66///     name: String,
67///     email: String,
68/// }
69/// 
70/// #[derive(Deserialize)]
71/// struct User {
72///     id: u32,
73///     name: String,
74///     email: String,
75/// }
76/// 
77/// impl ApiClient {
78///     #[post(url = "/users", content_type = json)]
79///     async fn create_user(&self, body: CreateUserRequest) -> anyhow::Result<User> {}
80/// }
81/// ```
82#[proc_macro_attribute]
83pub fn post(args: TokenStream, item: TokenStream) -> TokenStream {
84    common_http_method(args, item, HttpMethod::Post)
85}
86
87/// GET 方法宏
88/// 
89/// 用于为方法生成 GET 请求实现。
90/// 
91/// # 参数
92/// 
93/// * `url` - 请求 URL(相对于客户端基础 URL)
94/// * `header` - 可选的额外头部
95/// * `interceptor` - 可选的方法级拦截器
96/// 
97/// # 示例
98/// 
99/// ```rust
100/// use swan_macro::get;
101/// use serde::Deserialize;
102/// 
103/// #[derive(Deserialize)]
104/// struct User {
105///     id: u32,
106///     name: String,
107///     email: String,
108/// }
109/// 
110/// impl ApiClient {
111///     #[get(url = "/users/1")]
112///     async fn get_user(&self) -> anyhow::Result<User> {}
113/// }
114/// ```
115#[proc_macro_attribute]
116pub fn get(args: TokenStream, item: TokenStream) -> TokenStream {
117    common_http_method(args, item, HttpMethod::Get)
118}
119
120/// PUT 方法宏
121/// 
122/// 用于为方法生成 PUT 请求实现。
123#[proc_macro_attribute]
124pub fn put(args: TokenStream, item: TokenStream) -> TokenStream {
125    common_http_method(args, item, HttpMethod::Put)
126}
127
128/// DELETE 方法宏
129/// 
130/// 用于为方法生成 DELETE 请求实现。
131#[proc_macro_attribute]
132pub fn delete(args: TokenStream, item: TokenStream) -> TokenStream {
133    common_http_method(args, item, HttpMethod::Delete)
134}