From 97b53768ba7fec445a5ea91ae7299790a68bf459 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 19 Aug 2021 21:24:32 +0200 Subject: [PATCH] Replace `RoutingDsl` trait with `Router` type (#214) * Remove `RoutingDsl` * Fix typo --- CHANGELOG.md | 8 +- examples/async-graphql/src/main.rs | 5 +- examples/chat/src/main.rs | 1 - .../src/main.rs | 4 +- examples/form/src/main.rs | 2 +- examples/global-404-handler/src/main.rs | 1 - examples/hello-world/src/main.rs | 2 +- examples/key-value-store/src/main.rs | 6 +- examples/multipart-form/src/main.rs | 1 - examples/oauth/src/main.rs | 4 +- examples/sessions/src/main.rs | 4 +- examples/sse/src/main.rs | 2 +- examples/static-file-server/src/main.rs | 5 +- examples/templates/src/main.rs | 1 - examples/testing/src/main.rs | 6 +- examples/todos/src/main.rs | 4 +- examples/tokio-postgres/src/main.rs | 4 +- examples/tracing-aka-logging/src/main.rs | 2 +- examples/unix-domain-socket/src/main.rs | 1 - examples/versioning/src/main.rs | 1 - examples/websockets/src/main.rs | 2 +- src/extract/connect_info.rs | 20 +- src/extract/content_length_limit.rs | 1 - src/extract/extension.rs | 1 - src/extract/extractor_middleware.rs | 1 - src/extract/form.rs | 1 - src/extract/mod.rs | 7 - src/extract/multipart.rs | 1 - src/extract/path/mod.rs | 3 - src/extract/query.rs | 1 - src/extract/raw_query.rs | 1 - src/extract/request_parts.rs | 4 +- src/extract/typed_header.rs | 1 - src/extract/ws.rs | 2 - src/handler/mod.rs | 17 +- src/json.rs | 2 - src/lib.rs | 34 +-- src/response/headers.rs | 1 - src/response/mod.rs | 1 - src/response/redirect.rs | 1 - src/response/sse.rs | 1 - src/routing/mod.rs | 215 +++++++++--------- src/routing/or.rs | 10 +- src/service/mod.rs | 66 ++---- src/tests/mod.rs | 2 +- 45 files changed, 177 insertions(+), 283 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab4ea5f9..171fc4be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,9 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Overall compile time improvements. If you're having issues with compile time please file an issue! - Remove `prelude`. Explicit imports are now required. +- Add dedicated `Router` to replace the `RoutingDsl` trait - Make `FromRequest` default to being generic over `body::Body` ([#146](https://github.com/tokio-rs/axum/pull/146)) - Implement `std::error::Error` for all rejections ([#153](https://github.com/tokio-rs/axum/pull/153)) -- Add `RoutingDsl::or` for combining routes ([#108](https://github.com/tokio-rs/axum/pull/108)) +- Add `Router::or` for combining routes ([#108](https://github.com/tokio-rs/axum/pull/108)) - Add `handle_error` to `service::OnMethod` ([#160](https://github.com/tokio-rs/axum/pull/160)) - Add `OriginalUri` for extracting original request URI in nested services ([#197](https://github.com/tokio-rs/axum/pull/197)) - Implement `FromRequest` for `http::Extensions` @@ -32,16 +33,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Change WebSocket API to use an extractor ([#121](https://github.com/tokio-rs/axum/pull/121)) - Make WebSocket `Message` an enum ([#116](https://github.com/tokio-rs/axum/pull/116)) - `WebSocket` now uses `Error` as its error type ([#150](https://github.com/tokio-rs/axum/pull/150)) -- Ensure a `HandleError` service created from `ServiceExt::handle_error` - _does not_ implement `RoutingDsl` as that could lead to confusing routing behavior ([#120](https://github.com/tokio-rs/axum/pull/120)) - Implement `routing::MethodFilter` via [`bitflags`](https://crates.io/crates/bitflags) - Removed `extract::UrlParams` and `extract::UrlParamsMap`. Use `extract::Path` instead - `EmptyRouter` now requires the response body to implement `Send + Sync + 'static'` ([#108](https://github.com/tokio-rs/axum/pull/108)) -- `ServiceExt` has been removed and its methods have been moved to `RoutingDsl` ([#160](https://github.com/tokio-rs/axum/pull/160)) - `extractor_middleware` now requires `RequestBody: Default` ([#167](https://github.com/tokio-rs/axum/pull/167)) - Convert `RequestAlreadyExtracted` to an enum with each possible error variant ([#167](https://github.com/tokio-rs/axum/pull/167)) -- `RoutingDsl::check_infallible` now returns a `CheckInfallible` service. This +- `Router::check_infallible` now returns a `CheckInfallible` service. This is to improve compile times. - These future types have been moved - `extract::extractor_middleware::ExtractorMiddlewareResponseFuture` moved diff --git a/examples/async-graphql/src/main.rs b/examples/async-graphql/src/main.rs index 7fe65435..3d649d6d 100644 --- a/examples/async-graphql/src/main.rs +++ b/examples/async-graphql/src/main.rs @@ -3,10 +3,7 @@ mod starwars; use async_graphql::http::{playground_source, GraphQLPlaygroundConfig}; use async_graphql::{EmptyMutation, EmptySubscription, Request, Response, Schema}; use axum::response::IntoResponse; -use axum::{ - extract::Extension, handler::get, response::Html, route, routing::RoutingDsl, - AddExtensionLayer, Json, -}; +use axum::{extract::Extension, handler::get, response::Html, route, AddExtensionLayer, Json}; use starwars::{QueryRoot, StarWars, StarWarsSchema}; async fn graphql_handler(schema: Extension, req: Json) -> Json { diff --git a/examples/chat/src/main.rs b/examples/chat/src/main.rs index da06f578..1bfeddd1 100644 --- a/examples/chat/src/main.rs +++ b/examples/chat/src/main.rs @@ -11,7 +11,6 @@ use axum::extract::Extension; use axum::handler::get; use axum::response::{Html, IntoResponse}; use axum::route; -use axum::routing::RoutingDsl; use axum::AddExtensionLayer; use futures::{sink::SinkExt, stream::StreamExt}; use std::collections::HashSet; diff --git a/examples/error-handling-and-dependency-injection/src/main.rs b/examples/error-handling-and-dependency-injection/src/main.rs index d4dc4c1d..87033773 100644 --- a/examples/error-handling-and-dependency-injection/src/main.rs +++ b/examples/error-handling-and-dependency-injection/src/main.rs @@ -14,9 +14,7 @@ use axum::{ handler::{get, post}, http::{Response, StatusCode}, response::IntoResponse, - route, - routing::RoutingDsl, - AddExtensionLayer, Json, + route, AddExtensionLayer, Json, }; use serde::{Deserialize, Serialize}; use serde_json::json; diff --git a/examples/form/src/main.rs b/examples/form/src/main.rs index f35db2a3..e74b88fe 100644 --- a/examples/form/src/main.rs +++ b/examples/form/src/main.rs @@ -4,7 +4,7 @@ //! cargo run -p example-form //! ``` -use axum::{extract::Form, handler::get, response::Html, route, routing::RoutingDsl}; +use axum::{extract::Form, handler::get, response::Html, route}; use serde::Deserialize; use std::net::SocketAddr; diff --git a/examples/global-404-handler/src/main.rs b/examples/global-404-handler/src/main.rs index 3f94a803..30c08caf 100644 --- a/examples/global-404-handler/src/main.rs +++ b/examples/global-404-handler/src/main.rs @@ -10,7 +10,6 @@ use axum::{ http::{Response, StatusCode}, response::Html, route, - routing::RoutingDsl, }; use std::net::SocketAddr; use tower::util::MapResponseLayer; diff --git a/examples/hello-world/src/main.rs b/examples/hello-world/src/main.rs index 5bb9d567..03b14363 100644 --- a/examples/hello-world/src/main.rs +++ b/examples/hello-world/src/main.rs @@ -4,7 +4,7 @@ //! cargo run -p example-hello-world //! ``` -use axum::{handler::get, route, routing::RoutingDsl}; +use axum::{handler::get, route}; use std::net::SocketAddr; #[tokio::main] diff --git a/examples/key-value-store/src/main.rs b/examples/key-value-store/src/main.rs index debf3bc2..f16c1a23 100644 --- a/examples/key-value-store/src/main.rs +++ b/examples/key-value-store/src/main.rs @@ -7,13 +7,13 @@ //! ``` use axum::{ - body::{Body, Bytes}, + body::Bytes, extract::{ContentLengthLimit, Extension, Path}, handler::{delete, get, Handler}, http::StatusCode, response::IntoResponse, route, - routing::{BoxRoute, RoutingDsl}, + routing::{BoxRoute, Router}, }; use std::{ borrow::Cow, @@ -108,7 +108,7 @@ async fn list_keys(Extension(state): Extension) -> String { .join("\n") } -fn admin_routes() -> BoxRoute { +fn admin_routes() -> Router { async fn delete_all_keys(Extension(state): Extension) { state.write().unwrap().db.clear(); } diff --git a/examples/multipart-form/src/main.rs b/examples/multipart-form/src/main.rs index fd5f75ca..cd6017ef 100644 --- a/examples/multipart-form/src/main.rs +++ b/examples/multipart-form/src/main.rs @@ -9,7 +9,6 @@ use axum::{ handler::get, response::Html, route, - routing::RoutingDsl, }; use std::net::SocketAddr; diff --git a/examples/oauth/src/main.rs b/examples/oauth/src/main.rs index a226192c..5c392bee 100644 --- a/examples/oauth/src/main.rs +++ b/examples/oauth/src/main.rs @@ -14,9 +14,7 @@ use axum::{ handler::get, http::{header::SET_COOKIE, HeaderMap, Response}, response::{IntoResponse, Redirect}, - route, - routing::RoutingDsl, - AddExtensionLayer, + route, AddExtensionLayer, }; use oauth2::{ basic::BasicClient, reqwest::async_http_client, AuthUrl, AuthorizationCode, ClientId, diff --git a/examples/sessions/src/main.rs b/examples/sessions/src/main.rs index 638b91c5..afde44e3 100644 --- a/examples/sessions/src/main.rs +++ b/examples/sessions/src/main.rs @@ -15,9 +15,7 @@ use axum::{ StatusCode, }, response::IntoResponse, - route, - routing::RoutingDsl, - AddExtensionLayer, + route, AddExtensionLayer, }; use serde::{Deserialize, Serialize}; use std::net::SocketAddr; diff --git a/examples/sse/src/main.rs b/examples/sse/src/main.rs index 9b5f9ff4..26c87a3e 100644 --- a/examples/sse/src/main.rs +++ b/examples/sse/src/main.rs @@ -9,7 +9,7 @@ use axum::{ handler::get, http::StatusCode, response::sse::{sse, Event, Sse}, - routing::{nest, RoutingDsl}, + routing::nest, }; use futures::stream::{self, Stream}; use std::{convert::Infallible, net::SocketAddr, time::Duration}; diff --git a/examples/static-file-server/src/main.rs b/examples/static-file-server/src/main.rs index 069ffd5f..a8d4775d 100644 --- a/examples/static-file-server/src/main.rs +++ b/examples/static-file-server/src/main.rs @@ -4,10 +4,7 @@ //! cargo run -p example-static-file-server //! ``` -use axum::{ - http::StatusCode, - routing::{nest, RoutingDsl}, -}; +use axum::{http::StatusCode, routing::nest}; use std::net::SocketAddr; use tower_http::{services::ServeDir, trace::TraceLayer}; diff --git a/examples/templates/src/main.rs b/examples/templates/src/main.rs index 707b67a3..70e1076d 100644 --- a/examples/templates/src/main.rs +++ b/examples/templates/src/main.rs @@ -12,7 +12,6 @@ use axum::{ http::{Response, StatusCode}, response::{Html, IntoResponse}, route, - routing::RoutingDsl, }; use std::{convert::Infallible, net::SocketAddr}; diff --git a/examples/testing/src/main.rs b/examples/testing/src/main.rs index 05c567cc..a7c15f6d 100644 --- a/examples/testing/src/main.rs +++ b/examples/testing/src/main.rs @@ -5,10 +5,9 @@ //! ``` use axum::{ - body::Body, handler::{get, post}, route, - routing::{BoxRoute, RoutingDsl}, + routing::{BoxRoute, Router}, Json, }; use tower_http::trace::TraceLayer; @@ -34,7 +33,7 @@ async fn main() { /// Having a function that produces our app makes it easy to call it from tests /// without having to create an HTTP server. #[allow(dead_code)] -fn app() -> BoxRoute { +fn app() -> Router { route("/", get(|| async { "Hello, World!" })) .route( "/json", @@ -50,6 +49,7 @@ fn app() -> BoxRoute { #[cfg(test)] mod tests { use super::*; + use axum::body::Body; use axum::http::{self, Request, StatusCode}; use serde_json::{json, Value}; use std::net::{SocketAddr, TcpListener}; diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 828828ed..e5b02e20 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -18,9 +18,7 @@ use axum::{ handler::{get, patch}, http::StatusCode, response::IntoResponse, - route, - routing::RoutingDsl, - Json, + route, Json, }; use serde::{Deserialize, Serialize}; use std::{ diff --git a/examples/tokio-postgres/src/main.rs b/examples/tokio-postgres/src/main.rs index 27271776..3123b754 100644 --- a/examples/tokio-postgres/src/main.rs +++ b/examples/tokio-postgres/src/main.rs @@ -9,9 +9,7 @@ use axum::{ extract::{Extension, FromRequest, RequestParts}, handler::get, http::StatusCode, - route, - routing::RoutingDsl, - AddExtensionLayer, + route, AddExtensionLayer, }; use bb8::{Pool, PooledConnection}; use bb8_postgres::PostgresConnectionManager; diff --git a/examples/tracing-aka-logging/src/main.rs b/examples/tracing-aka-logging/src/main.rs index e509565a..03a39f9c 100644 --- a/examples/tracing-aka-logging/src/main.rs +++ b/examples/tracing-aka-logging/src/main.rs @@ -4,7 +4,7 @@ //! cargo run -p example-tracing-aka-logging //! ``` -use axum::{handler::get, response::Html, route, routing::RoutingDsl}; +use axum::{handler::get, response::Html, route}; use std::net::SocketAddr; use tower_http::trace::TraceLayer; diff --git a/examples/unix-domain-socket/src/main.rs b/examples/unix-domain-socket/src/main.rs index c0730af0..5d23c39c 100644 --- a/examples/unix-domain-socket/src/main.rs +++ b/examples/unix-domain-socket/src/main.rs @@ -10,7 +10,6 @@ use axum::{ handler::get, http::{Method, Request, StatusCode, Uri}, route, - routing::RoutingDsl, }; use futures::ready; use hyper::{ diff --git a/examples/versioning/src/main.rs b/examples/versioning/src/main.rs index 3ca175b2..a5bca134 100644 --- a/examples/versioning/src/main.rs +++ b/examples/versioning/src/main.rs @@ -12,7 +12,6 @@ use axum::{ http::{Response, StatusCode}, response::IntoResponse, route, - routing::RoutingDsl, }; use std::collections::HashMap; use std::net::SocketAddr; diff --git a/examples/websockets/src/main.rs b/examples/websockets/src/main.rs index 938adec2..71403aa1 100644 --- a/examples/websockets/src/main.rs +++ b/examples/websockets/src/main.rs @@ -14,7 +14,7 @@ use axum::{ handler::get, http::StatusCode, response::IntoResponse, - routing::{nest, RoutingDsl}, + routing::nest, }; use std::net::SocketAddr; use tower_http::{ diff --git a/src/extract/connect_info.rs b/src/extract/connect_info.rs index 4eacb9ac..dd510ad0 100644 --- a/src/extract/connect_info.rs +++ b/src/extract/connect_info.rs @@ -1,8 +1,8 @@ //! Extractor for getting connection information from a client. //! -//! See [`RoutingDsl::into_make_service_with_connect_info`] for more details. +//! See [`Router::into_make_service_with_connect_info`] for more details. //! -//! [`RoutingDsl::into_make_service_with_connect_info`]: crate::routing::RoutingDsl::into_make_service_with_connect_info +//! [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info use super::{Extension, FromRequest, RequestParts}; use async_trait::async_trait; @@ -19,10 +19,10 @@ use tower_http::add_extension::AddExtension; /// A [`MakeService`] created from a router. /// -/// See [`RoutingDsl::into_make_service_with_connect_info`] for more details. +/// See [`Router::into_make_service_with_connect_info`] for more details. /// /// [`MakeService`]: tower::make::MakeService -/// [`RoutingDsl::into_make_service_with_connect_info`]: crate::routing::RoutingDsl::into_make_service_with_connect_info +/// [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info pub struct IntoMakeServiceWithConnectInfo { svc: S, _connect_info: PhantomData C>, @@ -54,9 +54,9 @@ where /// The goal for this trait is to allow users to implement custom IO types that /// can still provide the same connection metadata. /// -/// See [`RoutingDsl::into_make_service_with_connect_info`] for more details. +/// See [`Router::into_make_service_with_connect_info`] for more details. /// -/// [`RoutingDsl::into_make_service_with_connect_info`]: crate::routing::RoutingDsl::into_make_service_with_connect_info +/// [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info pub trait Connected { /// The connection information type the IO resources generates. type ConnectInfo: Clone + Send + Sync + 'static; @@ -104,12 +104,12 @@ opaque_future! { /// Extractor for getting connection information produced by a [`Connected`]. /// /// Note this extractor requires you to use -/// [`RoutingDsl::into_make_service_with_connect_info`] to run your app +/// [`Router::into_make_service_with_connect_info`] to run your app /// otherwise it will fail at runtime. /// -/// See [`RoutingDsl::into_make_service_with_connect_info`] for more details. +/// See [`Router::into_make_service_with_connect_info`] for more details. /// -/// [`RoutingDsl::into_make_service_with_connect_info`]: crate::routing::RoutingDsl::into_make_service_with_connect_info +/// [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info #[derive(Clone, Copy, Debug)] pub struct ConnectInfo(pub T); @@ -131,7 +131,7 @@ where mod tests { use super::*; use crate::Server; - use crate::{handler::get, route, routing::RoutingDsl}; + use crate::{handler::get, route}; use std::net::{SocketAddr, TcpListener}; #[tokio::test] diff --git a/src/extract/content_length_limit.rs b/src/extract/content_length_limit.rs index 02799e28..08dd83d8 100644 --- a/src/extract/content_length_limit.rs +++ b/src/extract/content_length_limit.rs @@ -13,7 +13,6 @@ use std::ops::Deref; /// extract::ContentLengthLimit, /// handler::post, /// route, -/// routing::RoutingDsl /// }; /// /// async fn handler(body: ContentLengthLimit) { diff --git a/src/extract/extension.rs b/src/extract/extension.rs index 52298405..ed73c42f 100644 --- a/src/extract/extension.rs +++ b/src/extract/extension.rs @@ -14,7 +14,6 @@ use std::ops::Deref; /// extract::Extension, /// handler::get, /// route, -/// routing::RoutingDsl /// }; /// use std::sync::Arc; /// diff --git a/src/extract/extractor_middleware.rs b/src/extract/extractor_middleware.rs index 49d25d56..863e5e48 100644 --- a/src/extract/extractor_middleware.rs +++ b/src/extract/extractor_middleware.rs @@ -38,7 +38,6 @@ use tower::{BoxError, Layer, Service}; /// extract::{extractor_middleware, FromRequest, RequestParts}, /// handler::{get, post}, /// route, -/// routing::RoutingDsl /// }; /// use http::StatusCode; /// use async_trait::async_trait; diff --git a/src/extract/form.rs b/src/extract/form.rs index df26c8ae..bfdb578f 100644 --- a/src/extract/form.rs +++ b/src/extract/form.rs @@ -18,7 +18,6 @@ use tower::BoxError; /// extract::Form, /// handler::post, /// route, -/// routing::RoutingDsl /// }; /// use serde::Deserialize; /// diff --git a/src/extract/mod.rs b/src/extract/mod.rs index 8e3adaea..027b0ce7 100644 --- a/src/extract/mod.rs +++ b/src/extract/mod.rs @@ -12,7 +12,6 @@ //! Json, //! handler::{post, Handler}, //! route, -//! routing::RoutingDsl //! }; //! use serde::Deserialize; //! @@ -44,7 +43,6 @@ //! extract::{FromRequest, RequestParts}, //! handler::get, //! route, -//! routing::RoutingDsl //! }; //! use http::{StatusCode, header::{HeaderValue, USER_AGENT}}; //! @@ -89,7 +87,6 @@ //! extract::{Path, Query}, //! handler::get, //! route, -//! routing::RoutingDsl //! }; //! use std::collections::HashMap; //! @@ -122,7 +119,6 @@ //! extract::Json, //! handler::post, //! route, -//! routing::RoutingDsl //! }; //! use serde_json::Value; //! @@ -148,7 +144,6 @@ //! extract::{Json, rejection::JsonRejection}, //! handler::post, //! route, -//! routing::RoutingDsl //! }; //! use serde_json::Value; //! @@ -190,7 +185,6 @@ //! extract::Json, //! handler::post, //! route, -//! routing::RoutingDsl //! }; //! use serde_json::Value; //! @@ -224,7 +218,6 @@ //! handler::get, //! http::{header::HeaderMap, Request}, //! route, -//! routing::RoutingDsl //! }; //! //! struct MyBody(B); diff --git a/src/extract/multipart.rs b/src/extract/multipart.rs index e9f30035..abe33774 100644 --- a/src/extract/multipart.rs +++ b/src/extract/multipart.rs @@ -24,7 +24,6 @@ use tower::BoxError; /// extract::Multipart, /// handler::post, /// route, -/// routing::RoutingDsl /// }; /// use futures::stream::StreamExt; /// diff --git a/src/extract/path/mod.rs b/src/extract/path/mod.rs index 4f2d133a..f8f2d1ff 100644 --- a/src/extract/path/mod.rs +++ b/src/extract/path/mod.rs @@ -15,7 +15,6 @@ use std::ops::{Deref, DerefMut}; /// extract::Path, /// handler::get, /// route, -/// routing::RoutingDsl /// }; /// use uuid::Uuid; /// @@ -38,7 +37,6 @@ use std::ops::{Deref, DerefMut}; /// extract::Path, /// handler::get, /// route, -/// routing::RoutingDsl, /// }; /// use uuid::Uuid; /// @@ -60,7 +58,6 @@ use std::ops::{Deref, DerefMut}; /// extract::Path, /// handler::get, /// route, -/// routing::RoutingDsl /// }; /// use serde::Deserialize; /// use uuid::Uuid; diff --git a/src/extract/query.rs b/src/extract/query.rs index 939710a4..074596d3 100644 --- a/src/extract/query.rs +++ b/src/extract/query.rs @@ -14,7 +14,6 @@ use std::ops::Deref; /// extract::Query, /// handler::get, /// route, -/// routing::RoutingDsl /// }; /// use serde::Deserialize; /// diff --git a/src/extract/raw_query.rs b/src/extract/raw_query.rs index b05de88d..f64985a0 100644 --- a/src/extract/raw_query.rs +++ b/src/extract/raw_query.rs @@ -11,7 +11,6 @@ use std::convert::Infallible; /// extract::RawQuery, /// handler::get, /// route, -/// routing::RoutingDsl /// }; /// use futures::StreamExt; /// diff --git a/src/extract/request_parts.rs b/src/extract/request_parts.rs index 60efdf4e..529c10f1 100644 --- a/src/extract/request_parts.rs +++ b/src/extract/request_parts.rs @@ -93,7 +93,7 @@ where /// use axum::{ /// handler::get, /// route, -/// routing::{nest, RoutingDsl}, +/// routing::nest, /// extract::OriginalUri, /// http::Uri /// }; @@ -175,7 +175,6 @@ where /// extract::BodyStream, /// handler::get, /// route, -/// routing::RoutingDsl /// }; /// use futures::StreamExt; /// @@ -229,7 +228,6 @@ where /// extract::Body, /// handler::get, /// route, -/// routing::RoutingDsl, /// }; /// use futures::StreamExt; /// diff --git a/src/extract/typed_header.rs b/src/extract/typed_header.rs index 527ea5b6..2ce2c6bf 100644 --- a/src/extract/typed_header.rs +++ b/src/extract/typed_header.rs @@ -15,7 +15,6 @@ use std::{convert::Infallible, ops::Deref}; /// extract::TypedHeader, /// handler::get, /// route, -/// routing::RoutingDsl /// }; /// use headers::UserAgent; /// diff --git a/src/extract/ws.rs b/src/extract/ws.rs index 8000851e..f95e5251 100644 --- a/src/extract/ws.rs +++ b/src/extract/ws.rs @@ -8,7 +8,6 @@ //! handler::get, //! response::IntoResponse, //! route, -//! routing::RoutingDsl //! }; //! //! let app = route("/ws", get(handler)); @@ -115,7 +114,6 @@ impl WebSocketUpgrade { /// handler::get, /// response::IntoResponse, /// route, - /// routing::RoutingDsl /// }; /// /// let app = route("/ws", get(handler)); diff --git a/src/handler/mod.rs b/src/handler/mod.rs index ee54cbe8..e7d5e2c6 100644 --- a/src/handler/mod.rs +++ b/src/handler/mod.rs @@ -5,7 +5,7 @@ use crate::{ extract::FromRequest, response::IntoResponse, routing::{EmptyRouter, MethodFilter}, - service::{HandleError, HandleErrorFromRouter}, + service::HandleError, util::Either, }; use async_trait::async_trait; @@ -34,7 +34,6 @@ pub use self::into_service::IntoService; /// use axum::{ /// handler::any, /// route, -/// routing::RoutingDsl /// }; /// /// async fn handler() {} @@ -80,7 +79,6 @@ where /// use axum::{ /// handler::get, /// route, -/// routing::RoutingDsl /// }; /// /// async fn handler() {} @@ -170,7 +168,7 @@ where /// use axum::{ /// handler::on, /// route, -/// routing::{MethodFilter, RoutingDsl}, +/// routing::MethodFilter, /// }; /// /// async fn handler() {} @@ -237,7 +235,6 @@ pub trait Handler: Clone + Send + Sized + 'static { /// use axum::{ /// handler::{get, Handler}, /// route, - /// routing::RoutingDsl /// }; /// use tower::limit::{ConcurrencyLimitLayer, ConcurrencyLimit}; /// @@ -415,13 +412,13 @@ impl Layered { /// This is used to convert errors to responses rather than simply /// terminating the connection. /// - /// It works similarly to [`routing::RoutingDsl::handle_error`]. See that for more details. + /// It works similarly to [`routing::Router::handle_error`]. See that for more details. /// - /// [`routing::RoutingDsl::handle_error`]: crate::routing::RoutingDsl::handle_error + /// [`routing::Router::handle_error`]: crate::routing::Router::handle_error pub fn handle_error( self, f: F, - ) -> Layered, T> + ) -> Layered, T> where S: Service, Response = Response>, F: FnOnce(S::Error) -> Result, @@ -514,7 +511,7 @@ impl OnMethod { /// # Example /// /// ```rust - /// use axum::{handler::post, route, routing::RoutingDsl}; + /// use axum::{handler::post, route}; /// /// async fn handler() {} /// @@ -607,7 +604,7 @@ impl OnMethod { /// use axum::{ /// handler::get, /// route, - /// routing::{MethodFilter, RoutingDsl} + /// routing::MethodFilter /// }; /// /// async fn handler() {} diff --git a/src/json.rs b/src/json.rs index bbd9bb88..a4a5f80d 100644 --- a/src/json.rs +++ b/src/json.rs @@ -31,7 +31,6 @@ use tower::BoxError; /// extract, /// handler::post, /// route, -/// routing::RoutingDsl /// }; /// use serde::Deserialize; /// @@ -61,7 +60,6 @@ use tower::BoxError; /// extract::Path, /// handler::get, /// route, -/// routing::RoutingDsl, /// Json, /// }; /// use serde::Serialize; diff --git a/src/lib.rs b/src/lib.rs index 14f4292c..42576084 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,7 +49,6 @@ //! use axum::{ //! handler::get, //! route, -//! routing::RoutingDsl //! }; //! //! #[tokio::main] @@ -107,7 +106,6 @@ //! use axum::{ //! handler::get, //! route, -//! routing::RoutingDsl //! }; //! //! let app = route("/", get(get_slash).post(post_slash)) @@ -132,7 +130,7 @@ //! Routes can also be dynamic like `/users/:id`. See [extractors](#extractors) //! for more details. //! -//! You can also define routes separately and merge them with [`RoutingDsl::or`]. +//! You can also define routes separately and merge them with [`Router::or`]. //! //! ## Precedence //! @@ -145,7 +143,6 @@ //! handler::get, //! http::Request, //! route, -//! routing::RoutingDsl //! }; //! use tower::{Service, ServiceExt}; //! use http::{Method, Response, StatusCode}; @@ -212,7 +209,6 @@ //! use axum::{ //! route, //! handler::{get, post}, -//! routing::RoutingDsl //! }; //! //! // `GET /` and `POST /` are both accepted @@ -237,7 +233,6 @@ //! body::Body, //! http::Request, //! route, -//! routing::RoutingDsl, //! service //! }; //! use tower_http::services::ServeFile; @@ -288,12 +283,12 @@ //! http::Request, //! handler::get, //! route, -//! routing::{BoxRoute, RoutingDsl} +//! routing::{BoxRoute, Router} //! }; //! use tower_http::services::ServeFile; //! use http::Response; //! -//! fn api_routes() -> BoxRoute { +//! fn api_routes() -> Router { //! route("/users", get(|_: Request| async { /* ... */ })).boxed() //! } //! @@ -322,7 +317,6 @@ //! extract, //! handler::post, //! route, -//! routing::RoutingDsl //! }; //! use serde::Deserialize; //! @@ -353,7 +347,6 @@ //! extract, //! handler::post, //! route, -//! routing::RoutingDsl //! }; //! use uuid::Uuid; //! @@ -374,7 +367,6 @@ //! extract, //! handler::get, //! route, -//! routing::RoutingDsl //! }; //! use uuid::Uuid; //! use serde::Deserialize; @@ -414,7 +406,6 @@ //! handler::post, //! http::Request, //! route, -//! routing::RoutingDsl //! }; //! //! let app = route("/users/:id", post(handler)); @@ -447,7 +438,6 @@ //! http::Request, //! response::{Html, Json}, //! route, -//! routing::RoutingDsl //! }; //! use http::{StatusCode, Response, Uri}; //! use serde_json::{Value, json}; @@ -531,7 +521,6 @@ //! use axum::{ //! handler::{get, Handler}, //! route, -//! routing::RoutingDsl //! }; //! use tower::limit::ConcurrencyLimitLayer; //! @@ -554,7 +543,6 @@ //! use axum::{ //! handler::{get, post}, //! route, -//! routing::RoutingDsl //! }; //! use tower::limit::ConcurrencyLimitLayer; //! @@ -588,7 +576,6 @@ //! use axum::{ //! handler::{get, Handler}, //! route, -//! routing::RoutingDsl //! }; //! use tower::{ //! BoxError, timeout::{TimeoutLayer, error::Elapsed}, @@ -631,7 +618,7 @@ //! return `Result` where `T` implements //! [`IntoResponse`](response::IntoResponse). //! -//! See [`routing::RoutingDsl::handle_error`] for more details. +//! See [`routing::Router::handle_error`] for more details. //! //! ## Applying multiple middleware //! @@ -643,7 +630,6 @@ //! handler::get, //! http::Request, //! route, -//! routing::RoutingDsl //! }; //! use tower::ServiceBuilder; //! use tower_http::compression::CompressionLayer; @@ -680,7 +666,6 @@ //! extract, //! handler::get, //! route, -//! routing::RoutingDsl //! }; //! use std::sync::Arc; //! @@ -750,7 +735,7 @@ //! [`IntoResponse`]: crate::response::IntoResponse //! [`Timeout`]: tower::timeout::Timeout //! [examples]: https://github.com/tokio-rs/axum/tree/main/examples -//! [`RoutingDsl::or`]: crate::routing::RoutingDsl::or +//! [`Router::or`]: crate::routing::Router::or //! [`axum::Server`]: hyper::server::Server //! [`OriginalUri`]: crate::extract::OriginalUri @@ -863,13 +848,14 @@ pub use self::{error::Error, json::Json}; /// # Panics /// /// Panics if `description` doesn't start with `/`. -pub fn route(description: &str, service: S) -> Route> +pub fn route( + description: &str, + service: S, +) -> routing::Router>> where S: Service> + Clone, { - use routing::RoutingDsl; - - routing::EmptyRouter::not_found().route(description, service) + routing::Router::new().route(description, service) } mod sealed { diff --git a/src/response/headers.rs b/src/response/headers.rs index 6df6c081..c492f369 100644 --- a/src/response/headers.rs +++ b/src/response/headers.rs @@ -14,7 +14,6 @@ use tower::{util::Either, BoxError}; /// ```rust /// use axum::{ /// route, -/// routing::RoutingDsl, /// response::{IntoResponse, Headers}, /// handler::get, /// }; diff --git a/src/response/mod.rs b/src/response/mod.rs index 9d3a342c..2bbb8ae8 100644 --- a/src/response/mod.rs +++ b/src/response/mod.rs @@ -45,7 +45,6 @@ pub use self::{ /// handler::get, /// response::IntoResponse, /// route, -/// routing::RoutingDsl /// }; /// use http_body::Body; /// use http::{Response, HeaderMap}; diff --git a/src/response/redirect.rs b/src/response/redirect.rs index 15e1b946..3c7eaabd 100644 --- a/src/response/redirect.rs +++ b/src/response/redirect.rs @@ -13,7 +13,6 @@ use std::convert::TryFrom; /// handler::get, /// response::Redirect, /// route, -/// routing::RoutingDsl /// }; /// /// let app = route("/old", get(|| async { Redirect::permanent("/new".parse().unwrap()) })) diff --git a/src/response/sse.rs b/src/response/sse.rs index 873ee26e..2577aca6 100644 --- a/src/response/sse.rs +++ b/src/response/sse.rs @@ -6,7 +6,6 @@ //! use axum::{ //! handler::get, //! route, -//! routing::RoutingDsl //! }; //! use axum::response::sse::{sse, Event, KeepAlive, Sse}; //! use std::{time::Duration, convert::Infallible}; diff --git a/src/routing/mod.rs b/src/routing/mod.rs index 82954398..2e2bc315 100644 --- a/src/routing/mod.rs +++ b/src/routing/mod.rs @@ -8,10 +8,9 @@ use crate::{ connect_info::{Connected, IntoMakeServiceWithConnectInfo}, OriginalUri, }, - service::{HandleError, HandleErrorFromRouter}, + service::HandleError, util::ByteStr, }; -use async_trait::async_trait; use bytes::Bytes; use http::{Request, Response, StatusCode, Uri}; use regex::Regex; @@ -31,24 +30,55 @@ use tower_http::map_response_body::MapResponseBodyLayer; pub mod future; pub mod or; + pub use self::method_filter::MethodFilter; mod method_filter; -/// A route that sends requests to one of two [`Service`]s depending on the -/// path. -/// -/// Created with [`route`](crate::route). See that function for more details. +/// The router type for composing handlers and services. #[derive(Debug, Clone)] -pub struct Route { - pub(crate) pattern: PathPattern, - pub(crate) svc: S, - pub(crate) fallback: F, +pub struct Router { + svc: S, } -/// Trait for building routers. -#[async_trait] -pub trait RoutingDsl: crate::sealed::Sealed + Sized { +impl Router> { + /// Create a new `Router`. + /// + /// Unless you add additional routes this will respond to `404 Not Found` to + /// all requests. + pub fn new() -> Self { + Self { + svc: EmptyRouter::not_found(), + } + } +} + +impl Default for Router> { + fn default() -> Self { + Self::new() + } +} + +impl Service for Router +where + S: Service, +{ + type Response = S::Response; + type Error = S::Error; + type Future = S::Future; + + #[inline] + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + self.svc.poll_ready(cx) + } + + #[inline] + fn call(&mut self, req: R) -> Self::Future { + self.svc.call(req) + } +} + +impl Router { /// Add another route to the router. /// /// # Example @@ -57,7 +87,6 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// use axum::{ /// handler::get, /// route, - /// routing::RoutingDsl /// }; /// /// async fn first_handler() { /* ... */ } @@ -74,29 +103,29 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # }; /// ``` - fn route(self, description: &str, svc: T) -> Route + pub fn route(self, description: &str, svc: T) -> Router> where T: Service> + Clone, { - Route { + self.map(|fallback| Route { pattern: PathPattern::new(description), svc, - fallback: self, - } + fallback, + }) } /// Nest another service inside this router at the given path. /// /// See [`nest`] for more details. - fn nest(self, description: &str, svc: T) -> Nested + pub fn nest(self, description: &str, svc: T) -> Router> where T: Service> + Clone, { - Nested { + self.map(|fallback| Nested { pattern: PathPattern::new(description), svc, - fallback: self, - } + fallback, + }) } /// Create a boxed route trait object. @@ -109,7 +138,7 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// body::Body, /// handler::get, /// route, - /// routing::{BoxRoute, RoutingDsl} + /// routing::{Router, BoxRoute} /// }; /// /// async fn first_handler() { /* ... */ } @@ -118,7 +147,7 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// /// async fn third_handler() { /* ... */ } /// - /// fn app() -> BoxRoute { + /// fn app() -> Router { /// route("/", get(first_handler).post(second_handler)) /// .route("/foo", get(third_handler)) /// .boxed() @@ -127,22 +156,24 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// /// It also helps with compile times when you have a very large number of /// routes. - fn boxed(self) -> BoxRoute + pub fn boxed(self) -> Router> where - Self: Service, Response = Response> + Send + 'static, - >>::Error: Into + Send + Sync, - >>::Future: Send, + S: Service, Response = Response> + Send + 'static, + S::Error: Into + Send + Sync, + S::Future: Send, ReqBody: http_body::Body + Send + Sync + 'static, ReqBody::Error: Into + Send + Sync + 'static, ResBody: http_body::Body + Send + Sync + 'static, ResBody::Error: Into + Send + Sync + 'static, { - ServiceBuilder::new() - .layer_fn(BoxRoute) - .layer_fn(MpscBuffer::new) - .layer(BoxService::layer()) - .layer(MapResponseBodyLayer::new(box_body)) - .service(self) + self.map(|svc| { + ServiceBuilder::new() + .layer_fn(BoxRoute) + .layer_fn(MpscBuffer::new) + .layer(BoxService::layer()) + .layer(MapResponseBodyLayer::new(box_body)) + .service(svc) + }) } /// Apply a [`tower::Layer`] to the router. @@ -165,7 +196,6 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// use axum::{ /// handler::get, /// route, - /// routing::RoutingDsl /// }; /// use tower::limit::{ConcurrencyLimitLayer, ConcurrencyLimit}; /// @@ -195,7 +225,6 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// use axum::{ /// handler::get, /// route, - /// routing::RoutingDsl /// }; /// use tower_http::trace::TraceLayer; /// @@ -213,11 +242,11 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # }; /// ``` - fn layer(self, layer: L) -> Layered + pub fn layer(self, layer: L) -> Router> where - L: Layer, + L: Layer, { - Layered::new(layer.layer(self)) + self.map(|svc| Layered::new(layer.layer(svc))) } /// Convert this router into a [`MakeService`], that is a [`Service`] who's @@ -230,7 +259,6 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// use axum::{ /// handler::get, /// route, - /// routing::RoutingDsl /// }; /// /// let app = route("/", get(|| async { "Hi!" })); @@ -244,11 +272,11 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// ``` /// /// [`MakeService`]: tower::make::MakeService - fn into_make_service(self) -> tower::make::Shared + pub fn into_make_service(self) -> tower::make::Shared where - Self: Clone, + S: Clone, { - tower::make::Shared::new(self) + tower::make::Shared::new(self.svc) } /// Convert this router into a [`MakeService`], that will store `C`'s @@ -264,7 +292,6 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// extract::ConnectInfo, /// handler::get, /// route, - /// routing::RoutingDsl /// }; /// use std::net::SocketAddr; /// @@ -291,7 +318,6 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// extract::connect_info::{ConnectInfo, Connected}, /// handler::get, /// route, - /// routing::RoutingDsl /// }; /// use hyper::server::conn::AddrStream; /// @@ -335,14 +361,14 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// [`Connected`]: crate::extract::connect_info::Connected /// [`ConnectInfo`]: crate::extract::connect_info::ConnectInfo /// [uds]: https://github.com/tokio-rs/axum/blob/main/examples/unix_domain_socket.rs - fn into_make_service_with_connect_info( + pub fn into_make_service_with_connect_info( self, - ) -> IntoMakeServiceWithConnectInfo + ) -> IntoMakeServiceWithConnectInfo where - Self: Clone, + S: Clone, C: Connected, { - IntoMakeServiceWithConnectInfo::new(self) + IntoMakeServiceWithConnectInfo::new(self.svc) } /// Merge two routers into one. @@ -354,7 +380,6 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// use axum::{ /// handler::get, /// route, - /// routing::RoutingDsl /// }; /// # /// # async fn users_list() {} @@ -373,14 +398,11 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// # hyper::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # }; /// ``` - fn or(self, other: S) -> or::Or - where - S: RoutingDsl, - { - or::Or { - first: self, + pub fn or(self, other: S2) -> Router> { + self.map(|first| or::Or { + first, second: other, - } + }) } /// Handle errors services in this router might produce, by mapping them to @@ -395,7 +417,6 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// handler::get, /// http::StatusCode, /// route, - /// routing::RoutingDsl /// }; /// use tower::{BoxError, timeout::TimeoutLayer}; /// use std::{time::Duration, convert::Infallible}; @@ -435,7 +456,6 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// handler::get, /// http::StatusCode, /// route, - /// routing::RoutingDsl /// }; /// use tower::{BoxError, timeout::TimeoutLayer}; /// use std::time::Duration; @@ -457,24 +477,35 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized { /// # hyper::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # }; /// ``` - fn handle_error( - self, - f: F, - ) -> HandleError { - HandleError::new(self, f) + pub fn handle_error(self, f: F) -> Router> { + self.map(|svc| HandleError::new(svc, f)) } /// Check that your service cannot fail. /// /// That is, its error type is [`Infallible`]. - fn check_infallible(self) -> CheckInfallible { - CheckInfallible(self) + pub fn check_infallible(self) -> Router> { + self.map(CheckInfallible) + } + + fn map(self, f: F) -> Router + where + F: FnOnce(S) -> S2, + { + Router { svc: f(self.svc) } } } -impl RoutingDsl for Route {} - -impl crate::sealed::Sealed for Route {} +/// A route that sends requests to one of two [`Service`]s depending on the +/// path. +/// +/// Created with [`route`](crate::route). See that function for more details. +#[derive(Debug, Clone)] +pub struct Route { + pub(crate) pattern: PathPattern, + pub(crate) svc: S, + pub(crate) fallback: F, +} impl Service> for Route where @@ -560,10 +591,6 @@ impl fmt::Debug for EmptyRouter { } } -impl RoutingDsl for EmptyRouter {} - -impl crate::sealed::Sealed for EmptyRouter {} - impl Service> for EmptyRouter where B: Send + Sync + 'static, @@ -728,8 +755,8 @@ type Captures = Vec<(String, String)>; /// A boxed route trait object. /// -/// See [`RoutingDsl::boxed`] for more details. -pub struct BoxRoute( +/// See [`Router::boxed`] for more details. +pub struct BoxRoute( MpscBuffer, Response, E>, Request>, ); @@ -745,10 +772,6 @@ impl fmt::Debug for BoxRoute { } } -impl RoutingDsl for BoxRoute {} - -impl crate::sealed::Sealed for BoxRoute {} - impl Service> for BoxRoute where E: Into, @@ -772,7 +795,7 @@ where /// A [`Service`] created from a router by applying a Tower middleware. /// -/// Created with [`RoutingDsl::layer`]. See that method for more details. +/// Created with [`Router::layer`]. See that method for more details. pub struct Layered { inner: S, } @@ -803,10 +826,6 @@ where } } -impl RoutingDsl for Layered {} - -impl crate::sealed::Sealed for Layered {} - impl Service for Layered where S: Service, @@ -835,7 +854,7 @@ where /// use axum::{ /// handler::get, /// route, -/// routing::{nest, RoutingDsl}, +/// routing::nest, /// }; /// use http::Uri; /// @@ -864,7 +883,7 @@ where /// extract::Path, /// handler::get, /// route, -/// routing::{nest, RoutingDsl}, +/// routing::nest, /// }; /// use std::collections::HashMap; /// @@ -888,7 +907,7 @@ where /// /// ``` /// use axum::{ -/// routing::{nest, RoutingDsl}, +/// routing::nest, /// service::get, /// }; /// use tower_http::services::ServeDir; @@ -902,23 +921,19 @@ where /// # }; /// ``` /// -/// If necessary you can use [`RoutingDsl::boxed`] to box a group of routes +/// If necessary you can use [`Router::boxed`] to box a group of routes /// making the type easier to name. This is sometimes useful when working with /// `nest`. -pub fn nest(description: &str, svc: S) -> Nested> +pub fn nest(description: &str, svc: S) -> Router>> where S: Service> + Clone, { - Nested { - pattern: PathPattern::new(description), - svc, - fallback: EmptyRouter::not_found(), - } + Router::new().nest(description, svc) } /// A [`Service`] that has been nested inside a router at some path. /// -/// Created with [`nest`] or [`RoutingDsl::nest`]. +/// Created with [`nest`] or [`Router::nest`]. #[derive(Debug, Clone)] pub struct Nested { pattern: PathPattern, @@ -926,10 +941,6 @@ pub struct Nested { fallback: F, } -impl RoutingDsl for Nested {} - -impl crate::sealed::Sealed for Nested {} - impl Service> for Nested where S: Service, Response = Response> + Clone, @@ -1002,7 +1013,7 @@ fn strip_prefix(uri: &Uri, prefix: &str) -> Uri { /// Middleware that statically verifies that a service cannot fail. /// -/// Created with [`check_infallible`](RoutingDsl::check_infallible). +/// Created with [`check_infallible`](Router::check_infallible). #[derive(Debug, Clone, Copy)] pub struct CheckInfallible(S); @@ -1025,10 +1036,6 @@ where } } -impl RoutingDsl for CheckInfallible {} - -impl crate::sealed::Sealed for CheckInfallible {} - #[cfg(test)] mod tests { use super::*; diff --git a/src/routing/or.rs b/src/routing/or.rs index 836bf9db..d1c02ed1 100644 --- a/src/routing/or.rs +++ b/src/routing/or.rs @@ -1,6 +1,6 @@ //! [`Or`] used to combine two services into one. -use super::{FromEmptyRouter, OrDepth, RoutingDsl}; +use super::{FromEmptyRouter, OrDepth}; use crate::body::BoxBody; use futures_util::ready; use http::{Request, Response}; @@ -14,19 +14,15 @@ use tower::{util::Oneshot, Service, ServiceExt}; /// [`tower::Service`] that is the combination of two routers. /// -/// See [`RoutingDsl::or`] for more details. +/// See [`Router::or`] for more details. /// -/// [`RoutingDsl::or`]: super::RoutingDsl::or +/// [`Router::or`]: super::Router::or #[derive(Debug, Clone, Copy)] pub struct Or { pub(super) first: A, pub(super) second: B, } -impl RoutingDsl for Or {} - -impl crate::sealed::Sealed for Or {} - #[allow(warnings)] impl Service> for Or where diff --git a/src/service/mod.rs b/src/service/mod.rs index b8c04333..1b54c48b 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -16,7 +16,6 @@ //! handler::get, //! http::Request, //! route, -//! routing::RoutingDsl, //! service, //! }; //! @@ -68,7 +67,6 @@ //! use axum::{ //! handler::get, //! route, -//! routing::RoutingDsl //! }; //! use tower::ServiceBuilder; //! # let some_backpressure_sensitive_middleware = @@ -151,7 +149,6 @@ where /// use axum::{ /// http::Request, /// route, -/// routing::RoutingDsl, /// service, /// }; /// use http::Response; @@ -249,7 +246,7 @@ where /// handler::on, /// service, /// route, -/// routing::{MethodFilter, RoutingDsl}, +/// routing::MethodFilter, /// }; /// use http::Response; /// use std::convert::Infallible; @@ -344,7 +341,7 @@ impl OnMethod { /// handler::on, /// service, /// route, - /// routing::{MethodFilter, RoutingDsl}, + /// routing::MethodFilter, /// }; /// use http::Response; /// use std::convert::Infallible; @@ -447,7 +444,7 @@ impl OnMethod { /// handler::on, /// service, /// route, - /// routing::{MethodFilter, RoutingDsl}, + /// routing::MethodFilter, /// }; /// use http::Response; /// use std::convert::Infallible; @@ -483,14 +480,11 @@ impl OnMethod { /// /// Unhandled errors will close the connection without sending a response. /// - /// Works similarly to [`RoutingDsl::handle_error`]. See that for more + /// Works similarly to [`Router::handle_error`]. See that for more /// details. /// - /// [`RoutingDsl::handle_error`]: crate::routing::RoutingDsl::handle_error - pub fn handle_error( - self, - f: H, - ) -> HandleError { + /// [`Router::handle_error`]: crate::routing::Router::handle_error + pub fn handle_error(self, f: H) -> HandleError { HandleError::new(self, f) } } @@ -536,15 +530,15 @@ where /// /// Created with /// [`handler::Layered::handle_error`](crate::handler::Layered::handle_error) or -/// [`routing::RoutingDsl::handle_error`](crate::routing::RoutingDsl::handle_error). +/// [`routing::Router::handle_error`](crate::routing::Router::handle_error). /// See those methods for more details. -pub struct HandleError { +pub struct HandleError { inner: S, f: F, - _marker: PhantomData (B, T)>, + _marker: PhantomData B>, } -impl Clone for HandleError +impl Clone for HandleError where S: Clone, F: Clone, @@ -554,23 +548,7 @@ where } } -/// Marker type used for [`HandleError`] to indicate that it should implement -/// [`RoutingDsl`](crate::routing::RoutingDsl). -#[non_exhaustive] -#[derive(Debug)] -pub struct HandleErrorFromRouter; - -/// Marker type used for [`HandleError`] to indicate that it should _not_ implement -/// [`RoutingDsl`](crate::routing::RoutingDsl). -#[non_exhaustive] -#[derive(Debug)] -pub struct HandleErrorFromService; - -impl crate::routing::RoutingDsl for HandleError {} - -impl crate::sealed::Sealed for HandleError {} - -impl HandleError { +impl HandleError { pub(crate) fn new(inner: S, f: F) -> Self { Self { inner, @@ -580,7 +558,7 @@ impl HandleError { } } -impl fmt::Debug for HandleError +impl fmt::Debug for HandleError where S: fmt::Debug, { @@ -592,7 +570,7 @@ where } } -impl Service> for HandleError +impl Service> for HandleError where S: Service, Response = Response> + Clone, F: FnOnce(S::Error) -> Result + Clone, @@ -615,21 +593,3 @@ where } } } - -/// ```compile_fail -/// use crate::{service::ServiceExt}; -/// use tower::service_fn; -/// use hyper::Body; -/// use http::{Request, Response, StatusCode}; -/// -/// let svc = service_fn(|_: Request| async { -/// Ok::<_, hyper::Error>(Response::new(Body::empty())) -/// }) -/// .handle_error::<_, _, hyper::Error>(|_| Ok(StatusCode::INTERNAL_SERVER_ERROR)); -/// -/// // `.route` should not compile, ie `HandleError` created from any -/// // random service should not implement `RoutingDsl` -/// svc.route::<_, Body>("/", get(|| async {})); -/// ``` -#[allow(dead_code)] -fn compile_fail_tests() {} diff --git a/src/tests/mod.rs b/src/tests/mod.rs index b273401f..3ea24cd4 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -6,7 +6,7 @@ use crate::{ response::IntoResponse, route, routing::nest, - routing::{MethodFilter, RoutingDsl}, + routing::MethodFilter, service, }; use bytes::Bytes;