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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use std::{fmt, marker::PhantomData};

use serde::{
    de::{self, DeserializeOwned},
    Deserialize, Serialize,
};
use serde_json::Value;

use crate::{
    error::{Error, ErrorCode},
    id::Id,
};

/// Represents success / failure output of JSON-RPC 1.0 response.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Output<T = Value> {
    /// Successful execution result.
    pub result: Option<T>,
    /// Failed execution error.
    pub error: Option<Error>,
    /// Correlation id.
    ///
    /// It **MUST** be the same as the value of the id member in the Request Object.
    ///
    /// If there was an error in detecting the id in the Request object (e.g. Parse error/Invalid Request),
    /// it **MUST** be Null.
    pub id: Option<Id>,
}

impl<T: Serialize> fmt::Display for Output<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let json = serde_json::to_string(self).expect("`Output` is serializable");
        write!(f, "{}", json)
    }
}

impl<'de, T: Deserialize<'de>> de::Deserialize<'de> for Output<T> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        use self::response_field::{Field, FIELDS};

        struct Visitor<'de, T> {
            marker: PhantomData<Output<T>>,
            lifetime: PhantomData<&'de ()>,
        }
        impl<'de, T: Deserialize<'de>> de::Visitor<'de> for Visitor<'de, T> {
            type Value = Output<T>;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("struct Output")
            }

            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
            where
                A: de::MapAccess<'de>,
            {
                let mut result = Option::<Option<T>>::None;
                let mut error = Option::<Option<Error>>::None;
                let mut id = Option::<Option<Id>>::None;

                while let Some(key) = de::MapAccess::next_key::<Field>(&mut map)? {
                    match key {
                        Field::Result => {
                            if result.is_some() {
                                return Err(de::Error::duplicate_field("result"));
                            }
                            result = Some(de::MapAccess::next_value::<Option<T>>(&mut map)?)
                        }
                        Field::Error => {
                            if error.is_some() {
                                return Err(de::Error::duplicate_field("error"));
                            }
                            error = Some(de::MapAccess::next_value::<Option<Error>>(&mut map)?)
                        }
                        Field::Id => {
                            if id.is_some() {
                                return Err(de::Error::duplicate_field("id"));
                            }
                            id = Some(de::MapAccess::next_value::<Option<Id>>(&mut map)?)
                        }
                    }
                }

                let result = result.ok_or_else(|| de::Error::missing_field("result"))?;
                let error = error.ok_or_else(|| de::Error::missing_field("error"))?;
                let id = id.ok_or_else(|| de::Error::missing_field("id"))?;
                let (result, error, id) = match (result, error, id) {
                    (Some(value), None, Some(id)) => (Some(value), None, Some(id)),
                    (None, Some(error), id) => (None, Some(error), id),
                    _ => return Err(de::Error::custom("Invalid JSON-RPC 1.0 response")),
                };
                Ok(Output { result, error, id })
            }
        }

        de::Deserializer::deserialize_struct(
            deserializer,
            "Output",
            FIELDS,
            Visitor {
                marker: PhantomData::<Output<T>>,
                lifetime: PhantomData,
            },
        )
    }
}

impl<T: Serialize + DeserializeOwned> Output<T> {
    /// Creates a JSON-RPC 1.0 success response output.
    pub fn success(result: T, id: Id) -> Self {
        Self {
            result: Some(result),
            error: None,
            id: Some(id),
        }
    }

    /// Creates a JSON-RPC 1.0 failure response output.
    pub fn failure(error: Error, id: Option<Id>) -> Self {
        Self {
            result: None,
            error: Some(error),
            id,
        }
    }

    /// Creates a new failure response output indicating malformed request.
    pub fn invalid_request(id: Option<Id>) -> Self {
        Output::failure(Error::new(ErrorCode::InvalidRequest), id)
    }
}

impl<T: Serialize + DeserializeOwned> From<Output<T>> for Result<T, Error> {
    // Convert into a result.
    // Will be `Ok` if it is a `SuccessResponse` and `Err` if `FailureResponse`.
    fn from(output: Output<T>) -> Result<T, Error> {
        match (output.result, output.error) {
            (Some(result), None) => Ok(result),
            (None, Some(error)) => Err(error),
            _ => unreachable!("Invalid JSON-RPC 1.0 Response"),
        }
    }
}

/// JSON-RPC 1.0 Response object.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(untagged)]
pub enum Response<T = Value> {
    /// Single response
    Single(Output<T>),
    /// Response to batch request (batch of responses)
    Batch(Vec<Output<T>>),
}

impl<T: Serialize> fmt::Display for Response<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let json = serde_json::to_string(self).expect("`Response` is serializable");
        write!(f, "{}", json)
    }
}

mod response_field {
    use super::*;

    pub const FIELDS: &[&str] = &["result", "error", "id"];
    pub enum Field {
        Result,
        Error,
        Id,
    }

    impl<'de> de::Deserialize<'de> for Field {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: de::Deserializer<'de>,
        {
            de::Deserializer::deserialize_identifier(deserializer, FieldVisitor)
        }
    }

    struct FieldVisitor;
    impl<'de> de::Visitor<'de> for FieldVisitor {
        type Value = Field;

        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str("field identifier")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            match v {
                "result" => Ok(Field::Result),
                "error" => Ok(Field::Error),
                "id" => Ok(Field::Id),
                _ => Err(de::Error::unknown_field(v, &FIELDS)),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn response_output_cases() -> Vec<(Output, &'static str)> {
        vec![
            (
                // JSON-RPC 1.0 success response output
                Output {
                    result: Some(Value::Bool(true)),
                    error: None,
                    id: Some(Id::Num(1)),
                },
                r#"{"result":true,"error":null,"id":1}"#,
            ),
            (
                // JSON-RPC 1.0 failure response output
                Output {
                    result: None,
                    error: Some(Error::parse_error()),
                    id: Some(Id::Num(1)),
                },
                r#"{"result":null,"error":{"code":-32700,"message":"Parse error"},"id":1}"#,
            ),
            (
                // JSON-RPC 1.0 failure response output
                Output {
                    result: None,
                    error: Some(Error::parse_error()),
                    id: None,
                },
                r#"{"result":null,"error":{"code":-32700,"message":"Parse error"},"id":null}"#,
            ),
        ]
    }

    #[test]
    fn response_output_serialization() {
        for (success_response, expect) in response_output_cases() {
            let ser = serde_json::to_string(&success_response).unwrap();
            assert_eq!(ser, expect);
            let de = serde_json::from_str::<Output>(expect).unwrap();
            assert_eq!(de, success_response);
        }
    }

    #[test]
    fn response_serialization() {
        for (output, expect) in response_output_cases() {
            let response = Response::Single(output);
            assert_eq!(serde_json::to_string(&response).unwrap(), expect);
            assert_eq!(serde_json::from_str::<Response>(expect).unwrap(), response);
        }

        let batch_response = Response::Batch(vec![
            Output {
                result: Some(Value::Bool(true)),
                error: None,
                id: Some(Id::Num(1)),
            },
            Output {
                result: Some(Value::Bool(false)),
                error: None,
                id: Some(Id::Num(2)),
            },
        ]);
        let batch_expect =
            r#"[{"result":true,"error":null,"id":1},{"result":false,"error":null,"id":2}]"#;
        assert_eq!(
            serde_json::to_string(&batch_response).unwrap(),
            batch_expect
        );
        assert_eq!(
            serde_json::from_str::<Response>(&batch_expect).unwrap(),
            batch_response
        );
    }

    #[test]
    fn invalid_response() {
        let cases = vec![
            // JSON-RPC 1.0 invalid response
            r#"{"result":true,"error":null,"id":1,unknown:[]}"#,
            r#"{"result":true,"error":{"code": -32700,"message": "Parse error"},"id":1}"#,
            r#"{"result":true,"error":{"code": -32700,"message": "Parse error"}}"#,
            r#"{"result":true,"id":1}"#,
            r#"{"error":{"code": -32700,"message": "Parse error"},"id":1}"#,
            r#"{"unknown":[]}"#,
        ];

        for case in cases {
            let response = serde_json::from_str::<Response>(case);
            assert!(response.is_err());
        }
    }

    #[test]
    fn valid_response() {
        let cases = vec![
            // JSON-RPC 1.0 valid response
            r#"{"result":true,"error":null,"id":1}"#,
            r#"{"result":null,"error":{"code": -32700,"message": "Parse error"},"id":1}"#,
        ];

        for case in cases {
            let response = serde_json::from_str::<Response>(case);
            assert!(response.is_ok());
        }
    }
}