dbui-core 0.0.64

Core classes used by dbui in the app and WASM
use crate::database::conn::ConnectionParams;

use serde::{Deserialize, Serialize};

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

/// Flat representation of the [Project](Project), omitting sensitive information
#[derive(Clone, 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
}

/// A database project, the core model for this application
#[derive(derive_more::Constructor, Debug, Serialize)]
pub struct Project {
  key: String,
  name: String,
  connection_params: ConnectionParams
}

impl Project {
  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 conn(&self) -> &ConnectionParams {
    &self.connection_params
  }
}

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