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
mod advice;
mod channel;
mod connection_type;

pub use self::{
    advice::{
        Advice,
        Reconnect,
    },
    channel::Channel,
    connection_type::ConnectionType,
};
use serde::{
    Deserialize,
    Serialize,
};
use std::collections::HashMap;

/// A Cometd data packet
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Packet {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub advice: Option<Advice>,

    /// The channel this is sent on
    pub channel: Channel,

    #[serde(rename = "clientId", skip_serializing_if = "Option::is_none")]
    pub client_id: Option<String>,

    #[serde(rename = "connectionType", skip_serializing_if = "Option::is_none")]
    pub connection_type: Option<ConnectionType>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<serde_json::Value>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub ext: Option<serde_json::Value>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    #[serde(rename = "minimumVersion", skip_serializing_if = "Option::is_none")]
    pub minimum_version: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub subscription: Option<Channel>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub successful: Option<bool>,

    #[serde(
        rename = "supportedConnectionTypes",
        skip_serializing_if = "Option::is_none"
    )]
    pub supported_connection_types: Option<Vec<ConnectionType>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,

    /// Extra fields in the json packet
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

impl Packet {
    pub fn new() -> Self {
        Self {
            advice: None,
            channel: Default::default(),
            client_id: None,
            connection_type: None,
            data: None,
            error: None,
            ext: None,
            id: None,
            minimum_version: None,
            subscription: None,
            successful: None,
            supported_connection_types: None,
            version: None,

            extra: Default::default(),
        }
    }

    pub fn advice(mut self, advice: Advice) -> Self {
        self.advice = Some(advice);
        self
    }

    pub fn channel(mut self, channel: Channel) -> Self {
        self.channel = channel;
        self
    }

    pub fn client_id(mut self, id: String) -> Self {
        self.client_id = Some(id);
        self
    }

    pub fn connection_type(mut self, connection_type: ConnectionType) -> Self {
        self.connection_type = Some(connection_type);
        self
    }

    pub fn data(mut self, data: serde_json::Value) -> Self {
        self.data = Some(data);
        self
    }

    pub fn version(mut self, version: String) -> Self {
        self.version = Some(version);
        self
    }

    pub fn minimum_version(mut self, minimum_version: String) -> Self {
        self.minimum_version = Some(minimum_version);
        self
    }

    pub fn supported_connection_types(
        mut self,
        supported_connection_types: Vec<ConnectionType>,
    ) -> Self {
        self.supported_connection_types = Some(supported_connection_types);
        self
    }

    pub fn ext(mut self, ext: serde_json::Value) -> Self {
        self.ext = Some(ext);
        self
    }

    pub fn subscription(mut self, channel: Channel) -> Self {
        self.subscription = Some(channel);
        self
    }
}

impl Default for Packet {
    fn default() -> Self {
        Packet::new()
    }
}