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

use crate::{Method, Params};

/// Represents a MINI-RPC Request which is a notification.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Notification {
    /// A String containing the name of the method to be invoked.
    pub method: Method,

    /// A Structured value that holds the parameter values to be used during the invocation of the method.
    pub params: Params,
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::{self, Value};

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

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

    #[test]
    fn notification_serialization() {
        let input = 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);
    }
}