use std::collections::HashMap;
use std::time::Duration;
use zeroize::Zeroizing;
#[derive(Debug, Clone)]
pub struct ConnectionConfig {
pub database: String,
pub user: String,
pub password: Option<Zeroizing<String>>,
pub params: HashMap<String, String>,
pub connect_timeout: Option<Duration>,
pub statement_timeout: Option<Duration>,
pub keepalive_idle: Option<Duration>,
pub application_name: Option<String>,
pub extra_float_digits: Option<i32>,
}
impl ConnectionConfig {
pub fn new(database: impl Into<String>, user: impl Into<String>) -> Self {
Self {
database: database.into(),
user: user.into(),
password: None,
params: HashMap::new(),
connect_timeout: None,
statement_timeout: None,
keepalive_idle: None,
application_name: None,
extra_float_digits: None,
}
}
pub fn builder(
database: impl Into<String>,
user: impl Into<String>,
) -> ConnectionConfigBuilder {
ConnectionConfigBuilder {
database: database.into(),
user: user.into(),
password: None,
params: HashMap::new(),
connect_timeout: None,
statement_timeout: None,
keepalive_idle: None,
application_name: None,
extra_float_digits: None,
}
}
pub fn password(mut self, password: impl Into<String>) -> Self {
self.password = Some(Zeroizing::new(password.into()));
self
}
pub fn param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.params.insert(key.into(), value.into());
self
}
}
#[must_use = "call .build() to construct the final value"]
#[derive(Debug, Clone)]
pub struct ConnectionConfigBuilder {
pub(super) database: String,
pub(super) user: String,
pub(super) password: Option<Zeroizing<String>>,
pub(super) params: HashMap<String, String>,
pub(super) connect_timeout: Option<Duration>,
pub(super) statement_timeout: Option<Duration>,
pub(super) keepalive_idle: Option<Duration>,
pub(super) application_name: Option<String>,
pub(super) extra_float_digits: Option<i32>,
}
impl ConnectionConfigBuilder {
pub fn password(mut self, password: impl Into<String>) -> Self {
self.password = Some(Zeroizing::new(password.into()));
self
}
pub fn param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.params.insert(key.into(), value.into());
self
}
pub const fn connect_timeout(mut self, duration: Duration) -> Self {
self.connect_timeout = Some(duration);
self
}
pub const fn statement_timeout(mut self, duration: Duration) -> Self {
self.statement_timeout = Some(duration);
self
}
pub const fn keepalive_idle(mut self, duration: Duration) -> Self {
self.keepalive_idle = Some(duration);
self
}
pub fn application_name(mut self, name: impl Into<String>) -> Self {
self.application_name = Some(name.into());
self
}
pub const fn extra_float_digits(mut self, digits: i32) -> Self {
self.extra_float_digits = Some(digits);
self
}
pub fn build(self) -> ConnectionConfig {
ConnectionConfig {
database: self.database,
user: self.user,
password: self.password,
params: self.params,
connect_timeout: self.connect_timeout,
statement_timeout: self.statement_timeout,
keepalive_idle: self.keepalive_idle,
application_name: self.application_name,
extra_float_digits: self.extra_float_digits,
}
}
}