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
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
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ResponseMessage {
  ServerError {
    reason: String,
    content: String
  },
  Pong {
    v: i64
  },
  Notification {
    level: NotificationLevel,
    content: String
  },
  Connected {
    connection_id: Uuid,
    user_id: Uuid,
    p: Box<crate::project::ProjectDescription>,
    u: Box<crate::profile::UserProfile>,
    b: bool
  },
  SchemaResponse {
    schema: crate::Schema
  },
  SqlResponse {
    result: crate::database::results::ResultSet
  }
}

impl ResponseMessage {
  pub fn from_json(s: &str) -> Result<ResponseMessage> {
    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<ResponseMessage> {
    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")
  }
}