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
use crate::database::conn::ConnectionParams;

use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
pub struct ProjectSummary {
  pub name: String,
  #[serde(rename = "connectionParams")]
  pub connection_params: ConnectionParams
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ProjectDescription {
  pub key: String,
  pub name: String,
  pub server: String,
  pub port: Option<i32>,
  pub database: String,
  pub user: String,
  pub tls: bool
}

#[derive(Debug, Serialize)]
pub struct Project {
  key: String,
  name: String,
  connection_params: ConnectionParams
}

impl Project {
  pub fn new(key: String, name: String, connection_params: ConnectionParams) -> Project {
    Project {
      key,
      name,
      connection_params
    }
  }

  pub fn to_summary(&self) -> ProjectSummary {
    ProjectSummary {
      name: self.name.clone(),
      connection_params: self.connection_params.clone()
    }
  }

  pub fn to_description(&self) -> ProjectDescription {
    ProjectDescription {
      key: self.key.clone(),
      name: self.name.clone(),
      server: self.connection_params.server().clone(),
      port: *self.connection_params.port(),
      database: self.connection_params.database().clone(),
      user: self.connection_params.user().clone(),
      tls: self.connection_params.tls()
    }
  }

  pub fn key(&self) -> &String {
    &self.key
  }

  pub fn name(&self) -> &String {
    &self.name
  }

  pub fn connection_params(&self) -> &ConnectionParams {
    &self.connection_params
  }

  pub fn to_json(&self) -> String {
    serde_json::to_string_pretty(&self).unwrap()
  }
}

impl From<(String, ProjectSummary)> for Project {
  fn from(item: (String, ProjectSummary)) -> Self {
    Project::new(item.0, item.1.name, item.1.connection_params)
  }
}