cometd/
response.rs

1use serde::Deserialize;
2
3use crate::advice::Advice;
4
5/// This response is the basic reponse for any that does not match the other
6/// field of this enum.
7#[derive(Deserialize, PartialEq, Debug)]
8#[serde(rename_all = "camelCase")]
9pub struct BasicResponse {
10    pub channel: String,
11    pub successful: bool,
12    pub error: Option<String>,
13    pub advice: Option<Advice>,
14    pub ext: Option<serde_json::Value>,
15    pub client_id: Option<String>,
16    pub id: Option<String>,
17}
18
19/// This response is returned upon a successful handshake request.
20#[derive(Deserialize, PartialEq, Clone, Debug)]
21#[serde(rename_all = "camelCase")]
22pub struct HandshakeResponse {
23    pub channel: String,
24    pub successful: bool,
25    pub version: String,
26    pub minimum_version: Option<String>,
27    pub client_id: String,
28    pub supported_connection_types: Vec<String>,
29    pub advice: Option<Advice>,
30    pub ext: Option<serde_json::Value>,
31    pub id: Option<String>,
32    pub auth_successful: Option<bool>,
33}
34
35/// Represents an errored response from the cometd server. If an advice is provided,
36/// the client might automatically retry the request.
37#[derive(Deserialize, PartialEq, Clone, Debug)]
38#[serde(rename_all = "camelCase")]
39pub struct ErroredResponse {
40    pub channel: String,
41    pub successful: bool,
42    pub error: String,
43    pub client_id: Option<String>,
44    pub subscription: Option<String>,
45    pub advice: Option<Advice>,
46    pub ext: Option<serde_json::Value>,
47    pub id: Option<String>,
48}
49
50/// This response is returned upon a successful publish request.
51#[derive(Deserialize, PartialEq, Clone, Debug)]
52#[serde(rename_all = "camelCase")]
53pub struct PublishResponse {
54    pub channel: String,
55    pub client_id: String,
56    pub successful: bool,
57    pub error: Option<String>,
58    pub advice: Option<Advice>,
59    pub ext: Option<serde_json::Value>,
60    pub data: serde_json::Value,
61    pub id: Option<String>,
62}
63
64/// This response is returned when a message is send to a channel the client
65/// is subscribed to.
66#[derive(Deserialize, PartialEq, Clone, Debug)]
67#[serde(rename_all = "camelCase")]
68pub struct DeliveryResponse {
69    pub channel: String,
70    pub advice: Option<Advice>,
71    pub data: serde_json::Value,
72    pub ext: Option<serde_json::Value>,
73    pub id: Option<String>,
74}
75
76/// Represents a response from the cometd server.
77#[derive(Deserialize, PartialEq, Debug)]
78#[serde(untagged)]
79pub enum Response {
80    /// This response is returned upon a successful handshake request.
81    Handshake(HandshakeResponse),
82    /// This response is returned upon a successful publish request.
83    Publish(PublishResponse),
84    /// This response is returned when a message is send to a channel the client
85    /// is subscribed to.
86    Delivery(DeliveryResponse),
87    /// This response is the basic reponse for any that does not match the other
88    /// field of this enum.
89    Basic(BasicResponse),
90}
91
92impl Response {
93    /// Returns an [Advice](Advice) if the server returned one.
94    pub fn advice(&self) -> Option<Advice> {
95        match self {
96            Response::Handshake(resp) => resp.advice.clone(),
97            Response::Publish(resp) => resp.advice.clone(),
98            Response::Delivery(resp) => resp.advice.clone(),
99            Response::Basic(resp) => resp.advice.clone(),
100        }
101    }
102}