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
This commit is contained in:
Jonas Platte 2021-12-27 14:02:38 +01:00 committed by GitHub
parent f4716084a7
commit 0a399ed0fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 6 additions and 6 deletions

View file

@ -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>,

View file

@ -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>,

View file

@ -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)
}

View file

@ -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() {

View file

@ -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};