supabase_rust_realtime/
message.rs1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "lowercase")]
7pub enum ChannelEvent {
8 Insert,
9 Update,
10 Delete,
11 All,
12}
13
14impl std::fmt::Display for ChannelEvent {
15 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16 match self {
17 Self::Insert => write!(f, "INSERT"),
18 Self::Update => write!(f, "UPDATE"),
19 Self::Delete => write!(f, "DELETE"),
20 Self::All => write!(f, "*"), }
22 }
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct Payload {
28 pub data: serde_json::Value,
30 #[serde(rename = "type")] pub event_type: Option<String>,
32 pub timestamp: Option<String>, }
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct PresenceChange {
38 pub joins: HashMap<String, serde_json::Value>,
39 pub leaves: HashMap<String, serde_json::Value>,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct PresenceState {
45 pub state: HashMap<String, serde_json::Value>,
47}
48
49impl PresenceState {
50 pub fn new() -> Self {
51 Self { state: HashMap::new() }
52 }
53
54 pub fn sync(&mut self, presence_diff: &PresenceChange) {
56 for (key, value) in &presence_diff.joins {
57 self.state.insert(key.clone(), value.clone());
58 }
59 for key in presence_diff.leaves.keys() {
60 self.state.remove(key);
61 }
62 }
63
64 pub fn list(&self) -> Vec<(String, serde_json::Value)> {
66 self.state.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
67 }
68
69 pub fn get(&self, key: &str) -> Option<&serde_json::Value> {
71 self.state.get(key)
72 }
73}
74
75impl Default for PresenceState {
76 fn default() -> Self {
77 Self::new()
78 }
79}