use std::fmt;
use tokio_postgres_rustls::MakeRustlsConnect;
pub use postgres;
use crate::error::Result;
use crate::pool::{Pool, PoolOptions};
use crate::PgStore;
pub fn with_connector<F>(connect: F, options: PoolOptions) -> Result<PgStore>
where
F: Fn() -> core::result::Result<postgres::Client, postgres::Error> + Send + Sync + 'static,
{
PgStore::from_pool(Pool::new(Box::new(connect), options))
}
#[derive(Clone)]
pub struct Connector {
config: postgres::Config,
tls: MakeRustlsConnect,
}
impl fmt::Debug for Connector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Connector").field("config", &self.config).finish_non_exhaustive()
}
}
impl Connector {
pub fn new(config: postgres::Config, options: &PoolOptions) -> Result<Connector> {
Ok(Connector { config, tls: crate::tls::connector(options)? })
}
pub fn connect(&self) -> core::result::Result<postgres::Client, postgres::Error> {
self.config.connect(self.tls.clone())
}
}