mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-27 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:
@@ -1,6 +1,6 @@
|
||||
//! Additional types for defining routes.
|
||||
|
||||
use axum::{body::Body, handler::Handler, Router};
|
||||
use axum::{handler::Handler, Router};
|
||||
|
||||
mod resource;
|
||||
|
||||
@@ -17,31 +17,6 @@ pub use self::typed::{FirstElementIs, TypedPath};
|
||||
|
||||
/// Extension trait that adds additional methods to [`Router`].
|
||||
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.
|
||||
///
|
||||
/// 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
|
||||
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")]
|
||||
fn typed_get<H, T, P>(self, handler: H) -> Self
|
||||
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 {
|
||||
pub trait Sealed {}
|
||||
impl<B> Sealed for axum::Router<B> {}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use super::HasRoutes;
|
||||
use axum::{
|
||||
body::Body,
|
||||
handler::Handler,
|
||||
@@ -45,7 +44,7 @@ use tower_service::Service;
|
||||
/// Router::new().route("/featured", get(|| async {})),
|
||||
/// );
|
||||
///
|
||||
/// let app = Router::new().with(users);
|
||||
/// let app = Router::new().merge(users);
|
||||
/// # let _: Router<axum::body::Body> = app;
|
||||
/// ```
|
||||
#[derive(Debug)]
|
||||
@@ -182,9 +181,9 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> HasRoutes<B> for Resource<B> {
|
||||
fn routes(self) -> Router<B> {
|
||||
self.router
|
||||
impl<B> From<Resource<B>> for Router<B> {
|
||||
fn from(resource: Resource<B>) -> Self {
|
||||
resource.router
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,7 +191,6 @@ impl<B> HasRoutes<B> for Resource<B> {
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use crate::routing::RouterExt;
|
||||
use axum::{extract::Path, http::Method, Router};
|
||||
use tower::ServiceExt;
|
||||
|
||||
@@ -214,7 +212,7 @@ mod tests {
|
||||
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!(
|
||||
call_route(&mut app, Method::GET, "/users").await,
|
||||
|
||||
Reference in New Issue
Block a user