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
//! MINI-RPC Request.

use crate::{Call, Notification};

/// Request.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Request {
    /// A batch of requests (payloads).
    Batch(Vec<Payload>),

    /// A single request (payload).
    Single(Payload),
}

/// Request payload.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Payload {
    /// Fire a notification.
    Notification(Notification),

    /// Call a method.
    Call(Call),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Call, Id, Method, Notification, Params};
    use serde_json::{self, Value};

    #[test]
    fn request_deserialization() {
        // Single Notification.
        let input = r#"{"method":"test_method","params":[1,2,3]}"#;
        let expected = Request::Single(Payload::Notification(Notification {
            method: Method::String("test_method".to_owned()),
            params: Params::Array(vec![Value::from(1), Value::from(2), Value::from(3)]),
        }));

        let result: Request = serde_json::from_str(input).unwrap();
        assert_eq!(result, expected);

        // Single Call.
        let input = r#"{"id":1,"method":"test_method","params":[1,2,3]}"#;
        let expected = Request::Single(Payload::Call(Call {
            id: Id::Number(1),
            method: Method::String("test_method".to_owned()),
            params: Params::Array(vec![Value::from(1), Value::from(2), Value::from(3)]),
        }));

        let result: Request = serde_json::from_str(input).unwrap();
        assert_eq!(result, expected);

        // Batch Notification and Call.
        let input =
            r#"[{"method":"test_method","params":[1,2,3]},{"id":1,"method":"test_method","params":[1,2,3]}]"#;
        let expected = Request::Batch(vec![
            Payload::Notification(Notification {
                method: Method::String("test_method".to_owned()),
                params: Params::Array(vec![Value::from(1), Value::from(2), Value::from(3)]),
            }),
            Payload::Call(Call {
                id: Id::Number(1),
                method: Method::String("test_method".to_owned()),
                params: Params::Array(vec![Value::from(1), Value::from(2), Value::from(3)]),
            }),
        ]);

        let result: Request = serde_json::from_str(input).unwrap();
        assert_eq!(result, expected);
    }

    #[test]
    fn request_serialization() {
        // Single Notification.
        let input = Request::Single(Payload::Notification(Notification {
            method: Method::String("test_method".to_owned()),
            params: Params::Array(vec![Value::from(1), Value::from(2), Value::from(3)]),
        }));
        let expected = r#"{"method":"test_method","params":[1,2,3]}"#;

        let result = serde_json::to_string(&input).unwrap();
        assert_eq!(result, expected);

        // Single Call.
        let input = Request::Single(Payload::Call(Call {
            id: Id::Number(1),
            method: Method::String("test_method".to_owned()),
            params: Params::Array(vec![Value::from(1), Value::from(2), Value::from(3)]),
        }));
        let expected = r#"{"id":1,"method":"test_method","params":[1,2,3]}"#;

        let result = serde_json::to_string(&input).unwrap();
        assert_eq!(result, expected);

        // Batch Notification and Call.
        let input = Request::Batch(vec![
            Payload::Notification(Notification {
                method: Method::String("test_method".to_owned()),
                params: Params::Array(vec![Value::from(1), Value::from(2), Value::from(3)]),
            }),
            Payload::Call(Call {
                id: Id::Number(1),
                method: Method::String("test_method".to_owned()),
                params: Params::Array(vec![Value::from(1), Value::from(2), Value::from(3)]),
            }),
        ]);
        let expected =
            r#"[{"method":"test_method","params":[1,2,3]},{"id":1,"method":"test_method","params":[1,2,3]}]"#;

        let result = serde_json::to_string(&input).unwrap();
        assert_eq!(result, expected);
    }
}