use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
#[derive(Default)]
pub enum SslMode {
Disable,
#[default]
Prefer,
Require,
}
impl std::fmt::Display for SslMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Disable => write!(f, "disable"),
Self::Prefer => write!(f, "prefer"),
Self::Require => write!(f, "require"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TableKeyConfig {
pub table: String,
pub key_columns: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PostgresSourceConfig {
#[serde(default = "default_postgres_host")]
pub host: String,
#[serde(default = "default_postgres_port")]
pub port: u16,
pub database: String,
pub user: String,
#[serde(default)]
pub password: String,
#[serde(default)]
pub tables: Vec<String>,
#[serde(default = "default_slot_name")]
pub slot_name: String,
#[serde(default = "default_publication_name")]
pub publication_name: String,
#[serde(default)]
pub ssl_mode: SslMode,
#[serde(default)]
pub table_keys: Vec<TableKeyConfig>,
}
fn default_postgres_host() -> String {
"localhost".to_string()
}
fn default_postgres_port() -> u16 {
5432
}
fn default_slot_name() -> String {
"drasi_slot".to_string()
}
fn default_publication_name() -> String {
"drasi_publication".to_string()
}
impl PostgresSourceConfig {
pub fn validate(&self) -> anyhow::Result<()> {
if self.database.is_empty() {
return Err(anyhow::anyhow!(
"Validation error: database cannot be empty. \
Please specify the PostgreSQL database name"
));
}
if self.user.is_empty() {
return Err(anyhow::anyhow!(
"Validation error: user cannot be empty. \
Please specify the PostgreSQL user for replication"
));
}
if self.port == 0 {
return Err(anyhow::anyhow!(
"Validation error: port cannot be 0. \
Please specify a valid port number (1-65535)"
));
}
if self.slot_name.is_empty() {
return Err(anyhow::anyhow!(
"Validation error: slot_name cannot be empty. \
Please specify a replication slot name"
));
}
if self.publication_name.is_empty() {
return Err(anyhow::anyhow!(
"Validation error: publication_name cannot be empty. \
Please specify a publication name"
));
}
Ok(())
}
}