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
use crate::database::conn::ConnectionParams;
use crate::result::{Result, ResultExt};

use serde::{Deserialize, Serialize};

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

impl ProjectSummary {
  pub fn load(key: &str, fs: &crate::files::FileService) -> Result<ProjectSummary> {
    let content = fs.read_json("projects", key)?;
    serde_json::from_str(&content).chain_err(|| "Can't decode ProjectSummary")
  }
}

#[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 load(key: &str, fs: &crate::files::FileService) -> Result<Project> {
    let summ = ProjectSummary::load(key, fs)?;
    Ok(Project {
      key: key.into(),
      name: summ.name,
      connection_params: summ.connection_params
    })
  }

  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)
  }
}