Make JSON and HTTP1 support optional (#286)

* Make json support an optional feature

* Fix Json type documentation

* Make hyper's http1 feature optional
This commit is contained in:
Jonas Platte
2021-10-02 15:46:33 +02:00
committed by GitHub
parent cc4ae6b297
commit 250ea0cfef
7 changed files with 28 additions and 8 deletions
+1
View File
@@ -192,6 +192,7 @@ pub use self::{
request_parts::{BodyStream, RawBody},
};
#[doc(no_inline)]
#[cfg(feature = "json")]
pub use crate::Json;
#[cfg(feature = "multipart")]
+4
View File
@@ -25,9 +25,11 @@ define_rejection! {
pub struct HeadersAlreadyExtracted;
}
#[cfg(feature = "json")]
define_rejection! {
#[status = BAD_REQUEST]
#[body = "Failed to parse the request body as JSON"]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
/// Rejection type for [`Json`](super::Json).
pub struct InvalidJsonBody(Error);
}
@@ -199,11 +201,13 @@ composite_rejection! {
}
}
#[cfg(feature = "json")]
composite_rejection! {
/// Rejection used for [`Json`](super::Json).
///
/// Contains one variant for each way the [`Json`](super::Json) extractor
/// can fail.
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub enum JsonRejection {
InvalidJsonBody,
MissingJsonContentType,
+3 -2
View File
@@ -17,10 +17,10 @@ use std::{
ops::{Deref, DerefMut},
};
/// JSON Extractor/Response
/// JSON Extractor / Response.
///
/// When used as an extractor, it can deserialize request bodies into some type that
/// implements [`serde::Serialize`]. If the request body cannot be parsed, or it does not contain
/// implements [`serde::Deserialize`]. If the request body cannot be parsed, or it does not contain
/// the `Content-Type: application/json` header, it will reject the request and return a
/// `400 Bad Request` response.
///
@@ -87,6 +87,7 @@ use std::{
/// # };
/// ```
#[derive(Debug, Clone, Copy, Default)]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub struct Json<T>(pub T);
#[async_trait]
+8 -1
View File
@@ -1120,7 +1120,10 @@
//! The following optional features are available:
//!
//! - `headers`: Enables extracting typed headers via [`extract::TypedHeader`].
//! - `http1`: Enables hyper's `http1` feature. Enabled by default.
//! - `http2`: Enables hyper's `http2` feature.
//! - `json`: Enables the [`Json`] type and some similar convenience functionality.
//! Enabled by default.
//! - `multipart`: Enables parsing `multipart/form-data` requests with [`extract::Multipart`].
//! - `tower-log`: Enables `tower`'s `log` feature. Enabled by default.
//! - `ws`: Enables WebSockets support via [`extract::ws`].
@@ -1193,6 +1196,7 @@ pub(crate) mod macros;
mod clone_box_service;
mod error;
#[cfg(feature = "json")]
mod json;
mod util;
@@ -1216,7 +1220,10 @@ pub use hyper::Server;
pub use tower_http::add_extension::{AddExtension, AddExtensionLayer};
#[doc(inline)]
pub use self::{error::Error, json::Json, routing::Router};
#[cfg(feature = "json")]
pub use self::json::Json;
#[doc(inline)]
pub use self::{error::Error, routing::Router};
/// Alias for a type-erased error type.
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
+1
View File
@@ -18,6 +18,7 @@ mod redirect;
pub mod sse;
#[doc(no_inline)]
#[cfg(feature = "json")]
pub use crate::Json;
#[doc(inline)]
+5 -2
View File
@@ -37,7 +37,6 @@ use futures_util::{
use http::Response;
use http_body::Body as HttpBody;
use pin_project_lite::pin_project;
use serde::Serialize;
use std::{
borrow::Cow,
fmt,
@@ -181,6 +180,7 @@ pub struct Event {
#[derive(Debug)]
enum DataType {
Text(String),
#[cfg(feature = "json")]
Json(String),
}
@@ -197,9 +197,11 @@ impl Event {
/// Set Server-sent event data
/// data field(s) ("data:<content>")
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub fn json_data<T>(mut self, data: T) -> Result<Event, serde_json::Error>
where
T: Serialize,
T: serde::Serialize,
{
self.data = Some(DataType::Json(serde_json::to_string(&data)?));
Ok(self)
@@ -265,6 +267,7 @@ impl fmt::Display for Event {
f.write_char('\n')?;
}
}
#[cfg(feature = "json")]
Some(DataType::Json(data)) => {
"data:".fmt(f)?;
data.fmt(f)?;