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
//! This module contains the `serde` datastructures for DDP

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

/// A date represented by the JSON object `{ "$date": ts }`, with `ts` in millisecs since the epoch.
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct Timestamp {
    #[serde(rename="$date")]
    millis: Option<u64>,
}

/// DDP messages from client to server
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "msg")]
#[serde(rename_all = "camelCase")]
pub enum ClientMessage {
    /// Connection request. This must be the first message sent, and is used for version negotiation.
    Connect {
        version: String,
        support: Vec<String>,
        #[serde(skip_serializing_if="Option::is_none")]
        session: Option<String>,
    },

    Ping { 
        #[serde(skip_serializing_if="Option::is_none")]
        id: Option<String>
    },
    Pong { 
        #[serde(skip_serializing_if="Option::is_none")]
        id: Option<String>
    },

    /// Method calls
    Method { 
        id: String, 
        method: String, 
        params: Vec<Value>
    },


    Sub { 
        id: String, name: String, params: Vec<Value> 
    },
    Unsub { 
        id: String 
    },

}

/// DDP messages from server to client
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "msg")]
#[serde(rename_all = "camelCase")]
pub enum ServerMessage {
    Connected {
        session: String,
    },
    Failed {
        version: String,
    },
    Ping { 
        #[serde(default, skip_serializing_if="Option::is_none")]
        id: Option<String>
    },
    Pong { 
        #[serde(default, skip_serializing_if="Option::is_none")]
        id: Option<String>
    },

    /// The result of a method call
    Result(MethodResponse),

    /// Sent after unsubscribing, or to signal a subscription failure.
    Nosub { 
        id: String, 
        #[serde(default, skip_serializing_if="Option::is_none")]
        error: Option<Value>
    },

    /// Signals progress on one or several method calls.
    Updated { 
        methods: Vec<String> 
    },
    
    Added {
        collection: String,
        id: String,
        fields: Option<Value>,
    },
    Changed {
        collection: String,
        id: String,
        #[serde(default, skip_serializing_if="Option::is_none")]
        fields: Option<Value>,
        #[serde(default, skip_serializing_if="Option::is_none")]
        cleared: Option<Vec<String>>,
    },
    Removed {
        collection: String,
        id: String,
    },
    Ready {
        subs: Vec<String>,
    },
    AddedBefore {
        collection: String,
        id: String,
        #[serde(default, skip_serializing_if="Option::is_none")]
        fields: Option<Value>,
        before: Option<String>,
    },
    MovedBefore {
        before: Option<String>,
    }

}

impl ServerMessage {

    pub fn pretty(&self) -> String {
        serde_json::to_value(&self)
            .and_then(|v| serde_json::to_string_pretty(&v))
            .unwrap_or_else(|_| "<<serialization error>>".to_string())
    }

}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct MethodResponse {
    pub id: String,
    #[serde(default, skip_serializing_if="Option::is_none")]
    pub result: Option<Value>,
    #[serde(default, skip_serializing_if="Option::is_none")]
    pub error: Option<Value>,
}



#[cfg(test)]
mod tests {

    use super::*;

    use serde::de::DeserializeOwned;

    fn check_message<M>(msg: &M, string: &str)
        where M: Serialize + DeserializeOwned + PartialEq + std::fmt::Debug
    {
        let serialized = serde_json::to_string(msg).unwrap();
        assert_eq!(serialized, string);
        let deserialized: M = serde_json::from_str(&string).unwrap();
        assert_eq!(msg, &deserialized);
        
    }

    #[test]
    fn test_method_result() {
        check_message(&ServerMessage::Result( 
            MethodResponse {
                id: "123".to_string(),
                result: Some(Value::String("burp".to_string())),
                error: None
            }
        ), r#"{"msg":"result","id":"123","result":"burp"}"#);
    }

    #[test]
    fn test_method_error() {
        check_message(&ServerMessage::Result(
            MethodResponse {
                id: "456:kahcubwdasd".to_string(),
                error: Some(Value::Bool(true)),
                result: None,
            }

        ), r#"{"msg":"result","id":"456:kahcubwdasd","error":true}"#);
    }

    #[test]
    fn test_pingpong() {

        check_message(&ServerMessage::Ping { id: None }, r#"{"msg":"ping"}"#);
        check_message(&ServerMessage::Ping { id: Some("pingpong".to_string()) }, r#"{"msg":"ping","id":"pingpong"}"#);
    }

    #[test]
    fn test_timestamp() {
        check_message(&Timestamp{ millis: Some(129348109238) }, r#"{"$date":129348109238}"#);
    }
}