mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-08 00:00:24 +02:00
Replace HasRoutes with Into<Router> (#819)
* Move `HasRoutes` into axum * fix doc test * Just use `Into<Router>`
This commit is contained in:
@@ -12,12 +12,16 @@ and this project adheres to [Semantic Versioning].
|
|||||||
- **breaking:** `CachedRejection` has been removed ([#699])
|
- **breaking:** `CachedRejection` has been removed ([#699])
|
||||||
- **breaking:** `<Cached<T> as FromRequest>::Rejection` is now `T::Rejection`. ([#699])
|
- **breaking:** `<Cached<T> as FromRequest>::Rejection` is now `T::Rejection`. ([#699])
|
||||||
- **breaking:** `middleware::from_fn` has been moved into the main axum crate ([#719])
|
- **breaking:** `middleware::from_fn` has been moved into the main axum crate ([#719])
|
||||||
|
- **breaking:** `HasRoutes` has been removed. `Router::merge` now accepts `Into<Router>` ([#819])
|
||||||
|
- **breaking:** `RouterExt::with` method has been removed. Use `Router::merge` instead. It works
|
||||||
|
identically ([#819])
|
||||||
|
|
||||||
[#666]: https://github.com/tokio-rs/axum/pull/666
|
[#666]: https://github.com/tokio-rs/axum/pull/666
|
||||||
[#699]: https://github.com/tokio-rs/axum/pull/699
|
[#699]: https://github.com/tokio-rs/axum/pull/699
|
||||||
[#719]: https://github.com/tokio-rs/axum/pull/719
|
[#719]: https://github.com/tokio-rs/axum/pull/719
|
||||||
[#756]: https://github.com/tokio-rs/axum/pull/756
|
[#756]: https://github.com/tokio-rs/axum/pull/756
|
||||||
[#790]: https://github.com/tokio-rs/axum/pull/790
|
[#790]: https://github.com/tokio-rs/axum/pull/790
|
||||||
|
[#819]: https://github.com/tokio-rs/axum/pull/819
|
||||||
|
|
||||||
# 0.1.2 (13. January, 2022)
|
# 0.1.2 (13. January, 2022)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
//! Additional types for defining routes.
|
//! Additional types for defining routes.
|
||||||
|
|
||||||
use axum::{body::Body, handler::Handler, Router};
|
use axum::{handler::Handler, Router};
|
||||||
|
|
||||||
mod resource;
|
mod resource;
|
||||||
|
|
||||||
@@ -17,31 +17,6 @@ pub use self::typed::{FirstElementIs, TypedPath};
|
|||||||
|
|
||||||
/// Extension trait that adds additional methods to [`Router`].
|
/// Extension trait that adds additional methods to [`Router`].
|
||||||
pub trait RouterExt<B>: sealed::Sealed {
|
pub trait RouterExt<B>: sealed::Sealed {
|
||||||
/// Add the routes from `T`'s [`HasRoutes::routes`] to this router.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// Using [`Resource`] which implements [`HasRoutes`]:
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use axum::{Router, routing::get};
|
|
||||||
/// use axum_extra::routing::{RouterExt, Resource};
|
|
||||||
///
|
|
||||||
/// let app = Router::new()
|
|
||||||
/// .with(
|
|
||||||
/// Resource::named("users")
|
|
||||||
/// .index(|| async {})
|
|
||||||
/// .create(|| async {})
|
|
||||||
/// )
|
|
||||||
/// .with(
|
|
||||||
/// Resource::named("teams").index(|| async {})
|
|
||||||
/// );
|
|
||||||
/// # let _: Router<axum::body::Body> = app;
|
|
||||||
/// ```
|
|
||||||
fn with<T>(self, routes: T) -> Self
|
|
||||||
where
|
|
||||||
T: HasRoutes<B>;
|
|
||||||
|
|
||||||
/// Add a typed `GET` route to the router.
|
/// Add a typed `GET` route to the router.
|
||||||
///
|
///
|
||||||
/// The path will be inferred from the first argument to the handler function which must
|
/// The path will be inferred from the first argument to the handler function which must
|
||||||
@@ -151,13 +126,6 @@ impl<B> RouterExt<B> for Router<B>
|
|||||||
where
|
where
|
||||||
B: axum::body::HttpBody + Send + 'static,
|
B: axum::body::HttpBody + Send + 'static,
|
||||||
{
|
{
|
||||||
fn with<T>(self, routes: T) -> Self
|
|
||||||
where
|
|
||||||
T: HasRoutes<B>,
|
|
||||||
{
|
|
||||||
self.merge(routes.routes())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "typed-routing")]
|
#[cfg(feature = "typed-routing")]
|
||||||
fn typed_get<H, T, P>(self, handler: H) -> Self
|
fn typed_get<H, T, P>(self, handler: H) -> Self
|
||||||
where
|
where
|
||||||
@@ -239,20 +207,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait for things that can provide routes.
|
|
||||||
///
|
|
||||||
/// Used with [`RouterExt::with`].
|
|
||||||
pub trait HasRoutes<B = Body> {
|
|
||||||
/// Get the routes.
|
|
||||||
fn routes(self) -> Router<B>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<B> HasRoutes<B> for Router<B> {
|
|
||||||
fn routes(self) -> Router<B> {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod sealed {
|
mod sealed {
|
||||||
pub trait Sealed {}
|
pub trait Sealed {}
|
||||||
impl<B> Sealed for axum::Router<B> {}
|
impl<B> Sealed for axum::Router<B> {}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use super::HasRoutes;
|
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
handler::Handler,
|
handler::Handler,
|
||||||
@@ -45,7 +44,7 @@ use tower_service::Service;
|
|||||||
/// Router::new().route("/featured", get(|| async {})),
|
/// Router::new().route("/featured", get(|| async {})),
|
||||||
/// );
|
/// );
|
||||||
///
|
///
|
||||||
/// let app = Router::new().with(users);
|
/// let app = Router::new().merge(users);
|
||||||
/// # let _: Router<axum::body::Body> = app;
|
/// # let _: Router<axum::body::Body> = app;
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -182,9 +181,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<B> HasRoutes<B> for Resource<B> {
|
impl<B> From<Resource<B>> for Router<B> {
|
||||||
fn routes(self) -> Router<B> {
|
fn from(resource: Resource<B>) -> Self {
|
||||||
self.router
|
resource.router
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +191,6 @@ impl<B> HasRoutes<B> for Resource<B> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::routing::RouterExt;
|
|
||||||
use axum::{extract::Path, http::Method, Router};
|
use axum::{extract::Path, http::Method, Router};
|
||||||
use tower::ServiceExt;
|
use tower::ServiceExt;
|
||||||
|
|
||||||
@@ -214,7 +212,7 @@ mod tests {
|
|||||||
Router::new().route("/featured", get(|| async move { "users#featured" })),
|
Router::new().route("/featured", get(|| async move { "users#featured" })),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut app = Router::new().with(users);
|
let mut app = Router::new().merge(users);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
call_route(&mut app, Method::GET, "/users").await,
|
call_route(&mut app, Method::GET, "/users").await,
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- **added:** `Extension<_>` can now be used in tuples for building responses, and will set an
|
- **added:** `Extension<_>` can now be used in tuples for building responses, and will set an
|
||||||
extension on the response ([#797])
|
extension on the response ([#797])
|
||||||
- **added:** Implement `tower::Layer` for `Extension` ([#801])
|
- **added:** Implement `tower::Layer` for `Extension` ([#801])
|
||||||
|
- **changed:** `Router::merge` now accepts `Into<Routes>` ([#819])
|
||||||
- **breaking:** `sse::Event` now accepts types implementing `AsRef<str>` instead of `Into<String>`
|
- **breaking:** `sse::Event` now accepts types implementing `AsRef<str>` instead of `Into<String>`
|
||||||
as field values.
|
as field values.
|
||||||
- **breaking:** `sse::Event` now panics if a setter method is called twice instead of silently
|
- **breaking:** `sse::Event` now panics if a setter method is called twice instead of silently
|
||||||
@@ -80,6 +81,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
[#800]: https://github.com/tokio-rs/axum/pull/800
|
[#800]: https://github.com/tokio-rs/axum/pull/800
|
||||||
[#801]: https://github.com/tokio-rs/axum/pull/801
|
[#801]: https://github.com/tokio-rs/axum/pull/801
|
||||||
[#807]: https://github.com/tokio-rs/axum/pull/807
|
[#807]: https://github.com/tokio-rs/axum/pull/807
|
||||||
|
[#819]: https://github.com/tokio-rs/axum/pull/819
|
||||||
|
|
||||||
# 0.4.4 (13. January, 2022)
|
# 0.4.4 (13. January, 2022)
|
||||||
|
|
||||||
|
|||||||
@@ -245,13 +245,16 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[doc = include_str!("../docs/routing/merge.md")]
|
#[doc = include_str!("../docs/routing/merge.md")]
|
||||||
pub fn merge(mut self, other: Router<B>) -> Self {
|
pub fn merge<R>(mut self, other: R) -> Self
|
||||||
|
where
|
||||||
|
R: Into<Router<B>>,
|
||||||
|
{
|
||||||
let Router {
|
let Router {
|
||||||
routes,
|
routes,
|
||||||
node,
|
node,
|
||||||
fallback,
|
fallback,
|
||||||
nested_at_root,
|
nested_at_root,
|
||||||
} = other;
|
} = other.into();
|
||||||
|
|
||||||
for (id, route) in routes {
|
for (id, route) in routes {
|
||||||
let path = node
|
let path = node
|
||||||
|
|||||||
Reference in New Issue
Block a user