Skip to content

Commit f521bed

Browse files
committed
feat(router): 添加对 HTTP HEAD、OPTIONS、TRACE 和 CONNECT 方法的支持
1 parent 22a72e5 commit f521bed

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

crates/webr-macros/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ pub fn patch(_attr: TokenStream, item: TokenStream) -> TokenStream {
5757
item
5858
}
5959

60+
/// HEAD 路由注解
61+
#[proc_macro_attribute]
62+
pub fn head(_attr: TokenStream, item: TokenStream) -> TokenStream {
63+
item
64+
}
65+
66+
/// OPTIONS 路由注解
67+
#[proc_macro_attribute]
68+
pub fn options(_attr: TokenStream, item: TokenStream) -> TokenStream {
69+
item
70+
}
71+
72+
/// TRACE 路由注解
73+
#[proc_macro_attribute]
74+
pub fn trace(_attr: TokenStream, item: TokenStream) -> TokenStream {
75+
item
76+
}
77+
78+
/// CONNECT 路由注解
79+
#[proc_macro_attribute]
80+
pub fn connect(_attr: TokenStream, item: TokenStream) -> TokenStream {
81+
item
82+
}
83+
6084
/// 应用入口宏:将 async fn main 包装为 tokio runtime + WebR 启动逻辑
6185
#[proc_macro_attribute]
6286
pub fn main(_attr: TokenStream, item: TokenStream) -> TokenStream {

crates/webr-macros/src/route.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ pub enum HttpMethod {
2222
Put,
2323
Delete,
2424
Patch,
25+
Head,
26+
Options,
27+
Trace,
28+
Connect,
2529
}
2630

2731
impl HttpMethod {
@@ -33,6 +37,10 @@ impl HttpMethod {
3337
Self::Put => "put",
3438
Self::Delete => "delete",
3539
Self::Patch => "patch",
40+
Self::Head => "head",
41+
Self::Options => "options",
42+
Self::Trace => "trace",
43+
Self::Connect => "connect",
3644
};
3745
proc_macro2::Ident::new(name, proc_macro2::Span::call_site())
3846
}
@@ -45,6 +53,10 @@ impl HttpMethod {
4553
Self::Put => "PUT",
4654
Self::Delete => "DELETE",
4755
Self::Patch => "PATCH",
56+
Self::Head => "HEAD",
57+
Self::Options => "OPTIONS",
58+
Self::Trace => "TRACE",
59+
Self::Connect => "CONNECT",
4860
}
4961
}
5062

@@ -56,13 +68,19 @@ impl HttpMethod {
5668
"put" => Some(Self::Put),
5769
"delete" => Some(Self::Delete),
5870
"patch" => Some(Self::Patch),
71+
"head" => Some(Self::Head),
72+
"options" => Some(Self::Options),
73+
"trace" => Some(Self::Trace),
74+
"connect" => Some(Self::Connect),
5975
_ => None,
6076
}
6177
}
6278
}
6379

6480
/// 路由属性名称集合
65-
pub const ROUTE_ATTR_NAMES: &[&str] = &["get", "post", "put", "delete", "patch"];
81+
pub const ROUTE_ATTR_NAMES: &[&str] = &[
82+
"get", "post", "put", "delete", "patch", "head", "options", "trace", "connect",
83+
];
6684

6785
/// 将 WebR 路径语法转换为 axum 0.8+ 路径语法:
6886
/// `/users/{id}` → `/users/{id}`(axum 0.8 原生支持 `{param}` 语法)

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ pub use webr_middleware::{
9494

9595
pub use webr_macros::HttpError;
9696
pub use webr_macros::{
97-
component, config, controller, delete, entity, get, main, patch, post, put, sql, tx,
97+
component, config, connect, controller, delete, entity, get, head, main, options, patch, post,
98+
put, sql, trace, tx,
9899
};
99100

100101
pub use async_trait;

0 commit comments

Comments
 (0)