Support wildcards in typed paths (#1003)

* Support wildcards in typed paths

* changelog
This commit is contained in:
David Pedersen 2022-05-06 13:05:30 +02:00 committed by GitHub
parent 4ff78e552d
commit b5183afbec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 20 deletions

View file

@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning].
# Unreleased
- **fixed:** `Option` and `Result` are now supported in typed path route handler parameters ([#1001])
- **fixed:** Support wildcards in typed paths ([#1003])
[#1001]: https://github.com/tokio-rs/axum/pull/1001
[#1003]: https://github.com/tokio-rs/axum/pull/1003
# 0.3.0 (27. April, 2022)

View file

@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased
- **fixed:** `Option` and `Result` are now supported in typed path route handler parameters ([#1001])
- **fixed:** Support wildcards in typed paths ([#1003])
[#1001]: https://github.com/tokio-rs/axum/pull/1001
[#1003]: https://github.com/tokio-rs/axum/pull/1003
# 0.2.0 (31. March, 2022)

View file

@ -297,14 +297,10 @@ fn parse_path(path: &LitStr) -> syn::Result<Vec<Segment>> {
path.value()
.split('/')
.map(|segment| {
if segment.contains('*') {
return Err(syn::Error::new_spanned(
path,
"`typed_path` cannot contain wildcards",
));
}
if let Some(capture) = segment.strip_prefix(':') {
if let Some(capture) = segment
.strip_prefix(':')
.or_else(|| segment.strip_prefix('*'))
{
Ok(Segment::Capture(capture.to_owned(), path.span()))
} else {
Ok(Segment::Static(segment.to_owned()))

View file

@ -1,7 +0,0 @@
use axum_extra::routing::TypedPath;
#[derive(TypedPath)]
#[typed_path("/users/*rest")]
struct MyPath;
fn main() {}

View file

@ -1,5 +0,0 @@
error: `typed_path` cannot contain wildcards
--> tests/typed_path/fail/wildcard.rs:4:14
|
4 | #[typed_path("/users/*rest")]
| ^^^^^^^^^^^^^^

View file

@ -0,0 +1,12 @@
use axum_extra::routing::{RouterExt, TypedPath};
use serde::Deserialize;
#[derive(TypedPath, Deserialize)]
#[typed_path("/*rest")]
struct MyPath {
rest: String,
}
fn main() {
axum::Router::<axum::body::Body>::new().typed_get(|_: MyPath| async {});
}