Skip to content

Commit 4d339df

Browse files
authored
improve WebContext parsing. (#1184)
1 parent 909d177 commit 4d339df

4 files changed

Lines changed: 49 additions & 4 deletions

File tree

codegen/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# unreleased 0.4.0
2+
## Fix
3+
- fix `xitca_web::WebContext` parsing when generic body type is presented.
4+
5+
## Change
26
- macro is refactored to target xitca-web `0.7.0`
37

48
# 0.3.1

codegen/src/route.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub(crate) fn route(attr: Args, input: ItemFn) -> Result<TokenStream, Error> {
8080
enum State<'a> {
8181
Partial(&'a Type),
8282
Full(&'a Type),
83+
Default,
8384
}
8485

8586
let mut state = Vec::new();
@@ -114,13 +115,24 @@ pub(crate) fn route(attr: Args, input: ItemFn) -> Result<TokenStream, Error> {
114115
let PathArguments::AngleBracketed(ref arg) = path.arguments else {
115116
return Err(Error::new(path.span(), format!("expect &{ident}<'_, _>")));
116117
};
117-
match arg.args.last() {
118+
let mut args = arg.args.iter();
119+
120+
match args.next() {
121+
Some(GenericArgument::Lifetime(..)) => {}
122+
_ => return Err(Error::new(ty.span(), "expect lifetime param '_")),
123+
}
124+
125+
match args.next() {
118126
Some(GenericArgument::Type(ref ty)) => {
119127
state.push(State::Full(ty));
120-
break;
128+
}
129+
None => {
130+
state.push(State::Default);
121131
}
122132
_ => return Err(Error::new(ty.span(), "expect state type.")),
123-
}
133+
};
134+
135+
break;
124136
}
125137
_ => {}
126138
}
@@ -152,6 +164,11 @@ pub(crate) fn route(attr: Args, input: ItemFn) -> Result<TokenStream, Error> {
152164
state_ident = quote! { #ty };
153165
break;
154166
}
167+
State::Default => {
168+
generic_arg = quote! {};
169+
where_clause = quote! {};
170+
state_ident = quote! {};
171+
}
155172
}
156173
}
157174
}

test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ xitca-io = "0.4.1"
1717
xitca-server = { version = "0.5", features = ["quic"] }
1818
xitca-service = "0.3.0"
1919
xitca-unsafe-collection = "0.2"
20-
xitca-web = "0.7"
20+
xitca-web = { version = "0.7", features = ["codegen"] }
2121

2222
http-ws = { version = "0.4", features = ["stream"] }
2323

test/tests/macro.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,27 @@ impl MyError {
135135
WebResponse::builder().status(*status).body(Default::default()).unwrap()
136136
}
137137
}
138+
139+
#[tokio::test]
140+
async fn web_handler() {
141+
#[xitca_web::codegen::route("/", method = get)]
142+
async fn test(_: &WebContext<'_>) -> &'static str {
143+
""
144+
}
145+
146+
#[xitca_web::codegen::route("/2", method = get)]
147+
async fn test2(_: &WebContext<'_, ()>) -> &'static str {
148+
""
149+
}
150+
151+
#[xitca_web::codegen::route("/3", method = get)]
152+
async fn test3(_: &WebContext<'_, (), xitca_web::body::RequestBody>) -> &'static str {
153+
""
154+
}
155+
156+
let _ = xitca_web::App::new()
157+
.at_typed(test)
158+
.at_typed(test2)
159+
.at_typed(test3)
160+
.finish();
161+
}

0 commit comments

Comments
 (0)