From 55289af14db0638604e34939012f02ab7d4e232c Mon Sep 17 00:00:00 2001 From: Saru2003 Date: Mon, 21 Oct 2024 20:39:21 +0530 Subject: [PATCH] Added validate function to PgConfig and invoked it in finalize and pool initialization --- martin/src/pg/config.rs | 16 ++++++++++++++++ martin/src/pg/pool.rs | 2 ++ 2 files changed, 18 insertions(+) diff --git a/martin/src/pg/config.rs b/martin/src/pg/config.rs index 39ad64d9c..ee1537eea 100644 --- a/martin/src/pg/config.rs +++ b/martin/src/pg/config.rs @@ -94,6 +94,20 @@ pub struct PgCfgPublishFuncs { impl PgConfig { /// Apply defaults to the config, and validate if there is a connection string + pub fn validate(&self) -> PgResult<()> { + if let Some(pool_size) = self.pool_size() { + if pool_size < 1{ + return Err("pool_size must be greater than or equal to 1.".into()); + } + } + if self.connection_string.is_none() { + return Err("A connection string must be provided.".into()); + } + + Ok(()) + } + + pub fn finalize(&mut self) -> PgResult { let mut res = UnrecognizedValues::new(); if let Some(ref ts) = self.tables { @@ -110,10 +124,12 @@ impl PgConfig { self.auto_publish = OptBoolObj::Bool(true); } + self.validate()?; Ok(res) } pub async fn resolve(&mut self, id_resolver: IdResolver) -> MartinResult { + self.validate()?; let pg = PgBuilder::new(self, id_resolver).await?; let inst_tables = on_slow( pg.instantiate_tables(), diff --git a/martin/src/pg/pool.rs b/martin/src/pg/pool.rs index efa7d3ec2..52efc32a0 100755 --- a/martin/src/pg/pool.rs +++ b/martin/src/pg/pool.rs @@ -33,6 +33,8 @@ pub struct PgPool { impl PgPool { pub async fn new(config: &PgConfig) -> PgResult { + config.validate()?; + let (id, mgr) = Self::parse_config(config)?; let pool = Pool::builder(mgr)