From 0a399ed0fa5d00b8531b6c27eba11f0ba47bf3e9 Mon Sep 17 00:00:00 2001 From: Jonas Platte <jplatte@users.noreply.github.com> Date: Mon, 27 Dec 2021 14:02:38 +0100 Subject: [PATCH] A few small refactorings (#655) * Simplify json content-type check * Import HeaderMap from http instead of from headers The headers crate is an optional dependency and its HeaderMap re-export is `#[doc(hidden)]`. * Use headers re-export in axum in examples --- axum/src/docs/extract.md | 4 ++-- axum/src/extract/typed_header.rs | 2 +- axum/src/json.rs | 2 +- axum/src/routing/tests/get_to_head.rs | 2 +- examples/jwt/src/main.rs | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/axum/src/docs/extract.md b/axum/src/docs/extract.md index da62c5f8..6b0c17d2 100644 --- a/axum/src/docs/extract.md +++ b/axum/src/docs/extract.md @@ -40,12 +40,12 @@ Some commonly used extractors are: use axum::{ extract::{Json, TypedHeader, Path, Extension, Query}, routing::post, + headers::UserAgent, http::{Request, header::HeaderMap}, body::{Bytes, Body}, Router, }; use serde_json::Value; -use headers::UserAgent; use std::collections::HashMap; // `Path` gives you the path parameters and deserializes them. See its docs for @@ -148,10 +148,10 @@ individual headers first: use axum::{ extract::TypedHeader, routing::get, + headers::UserAgent, http::header::HeaderMap, Router, }; -use headers::UserAgent; async fn handler( TypedHeader(user_agent): TypedHeader<UserAgent>, diff --git a/axum/src/extract/typed_header.rs b/axum/src/extract/typed_header.rs index 4e5e525d..0151cfe2 100644 --- a/axum/src/extract/typed_header.rs +++ b/axum/src/extract/typed_header.rs @@ -14,10 +14,10 @@ use std::ops::Deref; /// ```rust,no_run /// use axum::{ /// extract::TypedHeader, +/// headers::UserAgent, /// routing::get, /// Router, /// }; -/// use headers::UserAgent; /// /// async fn users_teams_show( /// TypedHeader(user_agent): TypedHeader<UserAgent>, diff --git a/axum/src/json.rs b/axum/src/json.rs index 32cf775c..103e2997 100644 --- a/axum/src/json.rs +++ b/axum/src/json.rs @@ -132,7 +132,7 @@ fn json_content_type<B>(req: &RequestParts<B>) -> Result<bool, HeadersAlreadyExt }; let is_json_content_type = mime.type_() == "application" - && (mime.subtype() == "json" || mime.suffix().filter(|name| *name == "json").is_some()); + && (mime.subtype() == "json" || mime.suffix().map_or(false, |name| name == "json")); Ok(is_json_content_type) } diff --git a/axum/src/routing/tests/get_to_head.rs b/axum/src/routing/tests/get_to_head.rs index a7fe623e..08ce8369 100644 --- a/axum/src/routing/tests/get_to_head.rs +++ b/axum/src/routing/tests/get_to_head.rs @@ -4,7 +4,7 @@ use tower::ServiceExt; mod for_handlers { use super::*; - use headers::HeaderMap; + use http::HeaderMap; #[tokio::test] async fn get_handles_head() { diff --git a/examples/jwt/src/main.rs b/examples/jwt/src/main.rs index b13cedec..e410f975 100644 --- a/examples/jwt/src/main.rs +++ b/examples/jwt/src/main.rs @@ -9,12 +9,12 @@ use axum::{ async_trait, extract::{FromRequest, RequestParts, TypedHeader}, + headers::{authorization::Bearer, Authorization}, http::StatusCode, response::{IntoResponse, Response}, routing::{get, post}, Json, Router, }; -use headers::{authorization::Bearer, Authorization}; use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation}; use once_cell::sync::Lazy; use serde::{Deserialize, Serialize};