Impl std traits, 'UriDisplay<Query>' on 'MsgPack'.

This brings the 'MsgPack' impls to parity with 'Json'.
This commit is contained in:
Sergio Benitez 2021-06-29 03:14:57 -07:00
parent f827367df0
commit ae796b41ad
1 changed files with 30 additions and 23 deletions

View File

@ -32,6 +32,7 @@ use crate::data::{Limits, Data, FromData, Outcome};
use crate::response::{self, Responder, content};
use crate::http::Status;
use crate::form::prelude as form;
use crate::http::uri::fmt;
use serde::{Serialize, Deserialize};
@ -40,15 +41,34 @@ pub use rmp_serde::decode::Error;
/// The MessagePack guard: easily consume and return MessagePack.
///
/// ## Sending MessagePack
///
/// To respond with serialized MessagePack data, return a `MsgPack<T>` type,
/// where `T` implements [`Serialize`] from [`serde`]. The content type of the
/// response is set to `application/msgpack` automatically.
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// # type User = usize;
/// use rocket::serde::msgpack::MsgPack;
///
/// #[get("/users/<id>")]
/// fn user(id: usize) -> MsgPack<User> {
/// let user_from_id = User::from(id);
/// /* ... */
/// MsgPack(user_from_id)
/// }
/// ```
///
/// ## Receiving MessagePack
///
/// `MsgPack` is both a data guard and a form guard.
///
/// ### Data Guard
///
/// To parse request body data as MessagePack , add a `data` route argument with
/// a target type of `MsgPack<T>`, where `T` is some type you'd like to parse
/// from JSON. `T` must implement [`serde::Deserialize`].
/// To deserialize request body data as MessagePack, add a `data` route
/// argument with a target type of `MsgPack<T>`, where `T` is some type you'd
/// like to parse from JSON. `T` must implement [`serde::Deserialize`].
///
/// ```rust
/// # #[macro_use] extern crate rocket;
@ -102,26 +122,7 @@ pub use rmp_serde::decode::Error;
/// [global.limits]
/// msgpack = 5242880
/// ```
///
/// ## Sending MessagePack
///
/// If you're responding with MessagePack data, return a `MsgPack<T>` type,
/// where `T` implements [`Serialize`] from [`serde`]. The content type of the
/// response is set to `application/msgpack` automatically.
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// # type User = usize;
/// use rocket::serde::msgpack::MsgPack;
///
/// #[get("/users/<id>")]
/// fn user(id: usize) -> MsgPack<User> {
/// let user_from_id = User::from(id);
/// /* ... */
/// MsgPack(user_from_id)
/// }
/// ```
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MsgPack<T>(pub T);
impl<T> MsgPack<T> {
@ -210,6 +211,12 @@ impl<'v, T: Deserialize<'v> + Send> form::FromFormField<'v> for MsgPack<T> {
}
}
impl<T: fmt::UriDisplay<fmt::Query>> fmt::UriDisplay<fmt::Query> for MsgPack<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_, fmt::Query>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl<T> From<T> for MsgPack<T> {
fn from(value: T) -> Self {
MsgPack(value)