This commit is contained in:
David Pedersen
2022-02-14 09:52:14 +01:00
parent da7418be7d
commit 0743a86f6a
12 changed files with 134 additions and 0 deletions
+15
View File
@@ -262,3 +262,18 @@ enum Segment {
Capture(String, Span),
Static(String),
}
#[test]
fn ui() {
#[rustversion::stable]
fn go() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/typed_path/fail/*.rs");
t.pass("tests/typed_path/pass/*.rs");
}
#[rustversion::not(stable)]
fn go() {}
go();
}
@@ -0,0 +1,10 @@
use axum_macros::TypedPath;
use serde::Deserialize;
#[derive(TypedPath, Deserialize)]
#[typed_path("/users")]
struct MyPath {
id: u32,
}
fn main() {}
@@ -0,0 +1,14 @@
error[E0027]: pattern does not mention field `id`
--> tests/typed_path/fail/missing_capture.rs:5:14
|
5 | #[typed_path("/users")]
| ^^^^^^^^ missing field `id`
|
help: include the missing field in the pattern
|
5 | #[typed_path("/users" { id })]
| ++++++
help: if you don't care about this missing field, you can explicitly ignore it
|
5 | #[typed_path("/users" { .. })]
| ++++++
@@ -0,0 +1,9 @@
use axum_macros::TypedPath;
use serde::Deserialize;
#[derive(TypedPath, Deserialize)]
#[typed_path("/users/:id")]
struct MyPath {}
fn main() {
}
@@ -0,0 +1,5 @@
error[E0026]: struct `MyPath` does not have a field named `id`
--> tests/typed_path/fail/missing_field.rs:5:14
|
5 | #[typed_path("/users/:id")]
| ^^^^^^^^^^^^ struct `MyPath` does not have this field
@@ -0,0 +1,9 @@
use axum_macros::TypedPath;
#[derive(TypedPath)]
#[typed_path("/users/:id")]
struct MyPath {
id: u32,
}
fn main() {}
@@ -0,0 +1,8 @@
error[E0277]: the trait bound `for<'de> MyPath: serde::de::Deserialize<'de>` is not satisfied
--> tests/typed_path/fail/not_deserialize.rs:4:14
|
4 | #[typed_path("/users/:id")]
| ^^^^^^^^^^^^ the trait `for<'de> serde::de::Deserialize<'de>` is not implemented for `MyPath`
|
= note: required because of the requirements on the impl of `serde::de::DeserializeOwned` for `MyPath`
= note: required because of the requirements on the impl of `FromRequest<B>` for `axum::extract::Path<MyPath>`
@@ -0,0 +1,8 @@
use axum_macros::TypedPath;
use serde::Deserialize;
#[derive(TypedPath, Deserialize)]
#[typed_path("/users/:id")]
struct MyPath;
fn main() {}
@@ -0,0 +1,5 @@
error: Typed paths for unit structs cannot contain captures
--> tests/typed_path/fail/unit_with_capture.rs:5:14
|
5 | #[typed_path("/users/:id")]
| ^^^^^^^^^^^^
@@ -0,0 +1,25 @@
use axum_extra::routing::TypedPath;
use serde::Deserialize;
#[derive(TypedPath, Deserialize)]
#[typed_path("/users/:user_id/teams/:team_id")]
struct MyPath {
user_id: u32,
team_id: u32,
}
fn main() {
axum::Router::<axum::body::Body>::new().route("/", axum::routing::get(|_: MyPath| async {}));
assert_eq!(MyPath::PATH, "/users/:user_id/teams/:team_id");
assert_eq!(
format!(
"{}",
MyPath {
user_id: 1,
team_id: 2
}
),
"/users/1/teams/2"
);
}
@@ -0,0 +1,13 @@
use axum_extra::routing::TypedPath;
use serde::Deserialize;
#[derive(TypedPath, Deserialize)]
#[typed_path("/users/:user_id/teams/:team_id")]
struct MyPath(u32, u32);
fn main() {
axum::Router::<axum::body::Body>::new().route("/", axum::routing::get(|_: MyPath| async {}));
assert_eq!(MyPath::PATH, "/users/:user_id/teams/:team_id");
assert_eq!(format!("{}", MyPath(1, 2)), "/users/1/teams/2");
}
@@ -0,0 +1,13 @@
use axum_extra::routing::TypedPath;
#[derive(TypedPath)]
#[typed_path("/users")]
struct MyPath;
fn main() {
axum::Router::<axum::body::Body>::new()
.route("/", axum::routing::get(|_: MyPath| async {}));
assert_eq!(MyPath::PATH, "/users");
assert_eq!(format!("{}", MyPath), "/users");
}