Files
axum/src/lib.rs
T

118 lines
3.0 KiB
Rust
Raw Normal View History

2021-06-06 11:37:08 +02:00
// #![doc(html_root_url = "https://docs.rs/tower-http/0.1.0")]
#![warn(
clippy::all,
clippy::dbg_macro,
clippy::todo,
clippy::empty_enum,
clippy::enum_glob_use,
clippy::pub_enum_variant_names,
clippy::mem_forget,
clippy::unused_self,
clippy::filter_map_next,
clippy::needless_continue,
clippy::needless_borrow,
clippy::match_wildcard_for_single_variants,
clippy::if_let_mutex,
clippy::mismatched_target_os,
clippy::await_holding_lock,
clippy::match_on_vec_items,
clippy::imprecise_flops,
clippy::suboptimal_flops,
clippy::lossy_float_literal,
clippy::rest_pat_in_fully_bound_structs,
clippy::fn_params_excessive_bools,
clippy::exit,
clippy::inefficient_to_string,
clippy::linkedlist,
clippy::macro_use_imports,
clippy::option_option,
clippy::verbose_file_reads,
clippy::unnested_or_patterns,
rust_2018_idioms,
future_incompatible,
nonstandard_style,
// missing_docs,
)]
#![deny(unreachable_pub, broken_intra_doc_links, private_in_public)]
#![allow(
elided_lifetimes_in_paths,
// TODO: Remove this once the MSRV bumps to 1.42.0 or above.
clippy::match_like_matches_macro,
clippy::type_complexity
)]
#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(test, allow(clippy::float_cmp))]
2021-06-04 01:00:48 +02:00
use self::body::Body;
2021-05-29 21:13:06 +02:00
use bytes::Bytes;
2021-06-06 11:37:08 +02:00
use http::{Request, Response};
2021-06-01 17:17:10 +02:00
use response::IntoResponse;
2021-06-06 11:37:08 +02:00
use routing::{EmptyRouter, Route};
use std::convert::Infallible;
use tower::{BoxError, Service};
2021-05-29 21:13:06 +02:00
2021-05-30 13:24:03 +02:00
pub mod body;
pub mod extract;
pub mod handler;
pub mod response;
pub mod routing;
2021-06-06 11:37:08 +02:00
pub mod service;
2021-05-30 13:24:03 +02:00
2021-06-04 01:00:48 +02:00
#[doc(inline)]
2021-06-06 11:37:08 +02:00
pub use self::{
handler::{get, on, post, Handler},
routing::AddRoute,
};
2021-06-04 01:00:48 +02:00
2021-06-01 14:52:18 +02:00
pub use async_trait::async_trait;
2021-06-01 17:17:10 +02:00
pub use tower_http::add_extension::{AddExtension, AddExtensionLayer};
2021-06-01 14:52:18 +02:00
2021-06-06 11:42:44 +02:00
pub mod prelude {
pub use crate::{
body::Body,
extract,
handler::{get, on, post, Handler},
response, route,
routing::AddRoute,
};
}
2021-06-04 01:00:48 +02:00
pub fn route<S>(spec: &str, svc: S) -> Route<S, EmptyRouter>
where
S: Service<Request<Body>, Error = Infallible> + Clone,
{
routing::EmptyRouter.route(spec, svc)
2021-05-30 04:28:24 +02:00
}
2021-06-04 01:00:48 +02:00
#[cfg(test)]
mod tests;
2021-06-01 00:34:09 +02:00
pub(crate) trait ResultExt<T> {
fn unwrap_infallible(self) -> T;
}
impl<T> ResultExt<T> for Result<T, Infallible> {
fn unwrap_infallible(self) -> T {
match self {
Ok(value) => value,
Err(err) => match err {},
2021-05-30 04:28:24 +02:00
}
2021-05-30 00:52:04 +02:00
}
2021-05-30 04:28:24 +02:00
}
2021-06-01 00:34:09 +02:00
pub trait ServiceExt<B>: Service<Request<Body>, Response = Response<B>> {
2021-06-06 11:37:08 +02:00
fn handle_error<F, Res>(self, f: F) -> service::HandleError<Self, F>
where
Self: Sized,
2021-06-01 17:17:10 +02:00
F: FnOnce(Self::Error) -> Res,
Res: IntoResponse<Body>,
B: http_body::Body<Data = Bytes> + Send + Sync + 'static,
B::Error: Into<BoxError> + Send + Sync + 'static,
{
2021-06-06 11:37:08 +02:00
service::HandleError::new(self, f)
}
}
impl<S, B> ServiceExt<B> for S where S: Service<Request<Body>, Response = Response<B>> {}