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
use crate::member::Member;
use crate::poll::{Poll, Vote};
use crate::session::EstimateSession;
use crate::util::NotificationLevel;

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Sent from server to client, this shared model is used for all client communication
#[allow(variant_size_differences)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ResponseMessage {
  Connected {
    connection_id: Uuid,
    user_id: Uuid,
    u: Box<crate::profile::UserProfile>,
    b: bool
  },
  ServerError {
    reason: String,
    content: String
  },
  Pong {
    v: i64
  },
  Notification {
    level: NotificationLevel,
    content: String
  },
  // Session messages
  SessionNotFound {
    id: Uuid
  },
  SessionJoined {
    session: Box<EstimateSession>,
    members: Vec<Member>,
    connected: Vec<Uuid>,
    polls: Vec<Poll>,
    votes: Vec<Vote>
  },
  UpdateSession {
    session: EstimateSession
  },
  UpdateStatus {
    user_id: Uuid,
    connected: bool
  },
  UpdateMember {
    member: Member
  },
  UpdatePoll {
    poll: Poll
  },
  UpdateVote {
    vote: Vote
  }
}

impl ResponseMessage {
  pub fn from_json(s: &str) -> Result<Self> {
    serde_json::from_str(s).with_context(|| "Can't decode json ResponseMessage")
  }

  pub fn to_json(&self) -> Result<String> {
    serde_json::to_string_pretty(&self).with_context(|| "Can't encode json ResponseMessage")
  }

  pub fn from_binary(b: &[u8]) -> Result<Self> {
    bincode::deserialize(b).with_context(|| "Can't decode binary ResponseMessage")
  }

  pub fn to_binary(&self) -> Result<Vec<u8>> {
    bincode::serialize(&self).with_context(|| "Can't encode binary ResponseMessage")
  }
}