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
//! Message and relevant types.

use super::channel;
use super::timetoken::Timetoken;
use json::JsonValue;

/// # PubNub Message
///
/// This is the message structure yielded by [`Subscription`].
///
/// [`Subscription`]: crate::Subscription
#[derive(Debug, Clone)]
pub struct Message {
    /// Enum Type of Message.
    pub message_type: Type,
    /// Wildcard channel or channel group.
    pub route: Option<channel::Name>,
    /// Origin Channel of Message Receipt.
    pub channel: channel::Name,
    /// Decoded JSON Message Payload.
    pub json: JsonValue,
    /// Metadata of Message.
    pub metadata: JsonValue,
    /// Message ID Timetoken.
    pub timetoken: Timetoken,
    /// Issuing client ID.
    pub client: Option<String>,
    /// Subscribe key associated with the message.
    pub subscribe_key: String,
    /// Message flags.
    pub flags: u32,
}

/// # PubNub Message Types
///
/// PubNub delivers multiple kinds of messages. This enumeration describes the various types
/// available.
///
/// The special `Unknown` variant may be delivered as the PubNub service evolves. It allows
/// applications built on the PubNub Rust client to be forward-compatible without requiring a full
/// client upgrade.
#[derive(Debug, Clone, Eq, PartialEq, Copy)]
pub enum Type {
    /// A class message containing arbitrary payload data.
    Publish,
    /// A Lightweight message.
    Signal,
    /// An Objects service event, like space description updated.
    Objects,
    /// A message action event.
    Action,
    /// Presence event from channel (e.g. another client joined).
    Presence,
    /// Unknown type. The value may have special meaning in some contexts.
    Unknown(u32),
}

impl Type {
    /// # Create a `MessageType` from an integer
    ///
    /// Subscribe message pyloads include a non-enumerated integer to describe message types. We
    /// instead provide a concrete type, using this function to convert the integer into the
    /// appropriate type.
    #[must_use]
    pub fn from_json(i: &JsonValue) -> Self {
        match i.as_u32().unwrap_or(0) {
            0 => Self::Publish,
            1 => Self::Signal,
            2 => Self::Objects,
            3 => Self::Action,
            i => Self::Unknown(i),
        }
    }
}

impl Default for Message {
    #[must_use]
    fn default() -> Self {
        Self {
            message_type: Type::Unknown(0),
            route: None,
            channel: channel::Name::default(),
            json: JsonValue::Null,
            metadata: JsonValue::Null,
            timetoken: Timetoken::default(),
            client: None,
            subscribe_key: String::default(),
            flags: Default::default(),
        }
    }
}