logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! Represents a JSON extractor or responder.

use std::{
    fmt,
    ops::{Deref, DerefMut},
};

use crate::{
    async_trait, FromRequest, IntoResponse, Request, RequestExt, Response, ResponseExt, Result,
};

use super::{Payload, PayloadError};

/// Extracts JSON data from the body of a request, or responds a JSON data to response.
pub struct Json<T = ()>(pub T);

impl<T> Json<T> {
    /// Create new `Json` instance.
    #[inline]
    pub fn new(data: T) -> Self {
        Json(data)
    }

    /// Consumes the JSON, returning the wrapped value.
    #[inline]
    pub fn into_inner(self) -> T {
        self.0
    }
}

impl<T> Clone for Json<T>
where
    T: Clone,
{
    fn clone(&self) -> Self {
        Json(self.0.clone())
    }
}

impl<T> AsRef<T> for Json<T> {
    fn as_ref(&self) -> &T {
        &self.0
    }
}

impl<T> Deref for Json<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.0
    }
}

impl<T> DerefMut for Json<T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

impl<T> fmt::Debug for Json<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        T::fmt(self, f)
    }
}

impl<T> Payload for Json<T> {
    const NAME: &'static str = "json";

    // 1MB
    const LIMIT: u64 = 1024 * 1024;

    fn detect(m: &mime::Mime) -> bool {
        m.type_() == mime::APPLICATION
            && (m.subtype() == mime::JSON || m.suffix() == Some(mime::JSON))
    }

    fn mime() -> mime::Mime {
        mime::APPLICATION_JAVASCRIPT_UTF_8
    }
}

#[async_trait]
impl<T> FromRequest for Json<T>
where
    T: serde::de::DeserializeOwned,
{
    type Error = PayloadError;

    #[inline]
    async fn extract(req: &mut Request) -> Result<Self, Self::Error> {
        req.json().await.map(Self)
    }
}

/// Responds with JSON Data.
impl<T> IntoResponse for Json<T>
where
    T: serde::Serialize,
{
    fn into_response(self) -> Response {
        match Response::json(self.0) {
            Ok(res) => res,
            Err(err) => err.into_response(),
        }
    }
}