-
Notifications
You must be signed in to change notification settings - Fork 1
Add TypedPath and ApiError derive macros with typed routing #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -43,6 +43,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use crate::handler::{into_boxed_handler, BoxedHandler, Handler}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use crate::path_params::PathParams; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use crate::typed_path::TypedPath; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use http::{Extensions, Method}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use matchit::Router as MatchitRouter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use rustapi_openapi::Operation; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -331,6 +332,11 @@ impl Router { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Add a typed route using a TypedPath | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Add a typed route using a TypedPath | |
| /// Add a typed route using a [`TypedPath`] implementation. | |
| /// | |
| /// This is a convenience for registering routes whose path is defined by a | |
| /// type that implements [`TypedPath`]. Instead of writing the path string | |
| /// manually, the route path is taken from `P::PATH`, which helps keep your | |
| /// handler and the path definition in sync. | |
| /// | |
| /// Internally this is equivalent to calling [`Router::route`] with | |
| /// `P::PATH`: | |
| /// | |
| /// ```ignore | |
| /// router.route(P::PATH, method_router); | |
| /// ``` | |
| /// | |
| /// # Examples | |
| /// | |
| /// ```no_run | |
| /// use http::Method; | |
| /// use rustapi_core::router::{Router, MethodRouter}; | |
| /// use rustapi_core::typed_path::TypedPath; | |
| /// | |
| /// // Define a typed path for a user resource. | |
| /// // | |
| /// // The derive macro provides a `TypedPath` implementation with | |
| /// // `PATH = "/users/{id}"`. | |
| /// #[derive(TypedPath)] | |
| /// #[typed_path("/users/{id}")] | |
| /// struct UserPath { | |
| /// id: String, | |
| /// } | |
| /// | |
| /// // Register a GET handler for `/users/{id}` using the typed path. | |
| /// let router = Router::new().typed::<UserPath>( | |
| /// MethodRouter::new().on(Method::GET, |req| async move { | |
| /// // handle GET /users/{id} | |
| /// }), | |
| /// ); | |
| /// | |
| /// // This is equivalent to: | |
| /// // | |
| /// // let router = Router::new().route( | |
| /// // "/users/{id}", | |
| /// // MethodRouter::new().on(Method::GET, |req| async move { | |
| /// // // handle GET /users/{id} | |
| /// // }), | |
| /// // ); | |
| /// ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| use serde::{de::DeserializeOwned, Serialize}; | ||
|
|
||
| /// Trait for defining typed paths | ||
| /// | ||
| /// This trait allows structs to define their own path pattern and URL generation logic. | ||
| /// It is usually implemented via `#[derive(TypedPath)]`. | ||
| pub trait TypedPath: Serialize + DeserializeOwned + Send + Sync + 'static { | ||
| /// The URL path pattern (e.g., "/users/{id}") | ||
| const PATH: &'static str; | ||
|
|
||
| /// Convert the struct fields to a path string | ||
| fn to_uri(&self) -> String; | ||
| } | ||
|
Comment on lines
+3
to
+13
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The typed method lacks documentation explaining its purpose and providing a usage example. Users may not understand how it differs from the regular route method or how to use it with the TypedPath trait. Add a doc comment with an example showing how to use it with a TypedPath-derived struct.