From 55289af14db0638604e34939012f02ab7d4e232c Mon Sep 17 00:00:00 2001 From: Saru2003 Date: Mon, 21 Oct 2024 20:39:21 +0530 Subject: [PATCH 01/10] 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) From 5a2dbbdf6fc830ab6e326227e7cd48ebf69f3e4e Mon Sep 17 00:00:00 2001 From: Saru2003 Date: Mon, 21 Oct 2024 21:07:22 +0530 Subject: [PATCH 02/10] Fix formatting issues and validate pool size --- martin/src/pg/config.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/martin/src/pg/config.rs b/martin/src/pg/config.rs index ee1537eea..a36daedcf 100644 --- a/martin/src/pg/config.rs +++ b/martin/src/pg/config.rs @@ -95,19 +95,22 @@ 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 let Some(pool_size) = self.pool_size { + if pool_size < 1 { + return Err(PgError::ValidationError( + "pool_size must be greater than or equal to 1.".to_string(), + )); } } if self.connection_string.is_none() { - return Err("A connection string must be provided.".into()); + return Err(PgError::ValidationError( + "A connection string must be provided.".to_string(), + )); } Ok(()) } - - + pub fn finalize(&mut self) -> PgResult { let mut res = UnrecognizedValues::new(); if let Some(ref ts) = self.tables { From 83cb044ea075a6524472d740582c30d40a4dfbc6 Mon Sep 17 00:00:00 2001 From: Saru2003 Date: Mon, 21 Oct 2024 21:16:19 +0530 Subject: [PATCH 03/10] Fix PgError import --- martin/src/pg/config.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/martin/src/pg/config.rs b/martin/src/pg/config.rs index a36daedcf..fe4c1d653 100644 --- a/martin/src/pg/config.rs +++ b/martin/src/pg/config.rs @@ -16,6 +16,7 @@ use crate::pg::PgResult; use crate::source::TileInfoSources; use crate::utils::{IdResolver, OptBoolObj, OptOneMany}; use crate::MartinResult; +use crate::pg::PgError; pub trait PgInfo { fn format_id(&self) -> String; From 14add380f9eb7f8997baae6cabae63fdcd6a4f21 Mon Sep 17 00:00:00 2001 From: Saru2003 Date: Mon, 21 Oct 2024 22:09:16 +0530 Subject: [PATCH 04/10] Fix PgError import and Added ValidationError variant --- martin/src/pg/errors.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/martin/src/pg/errors.rs b/martin/src/pg/errors.rs index b8b995ccb..4e83a2ab9 100644 --- a/martin/src/pg/errors.rs +++ b/martin/src/pg/errors.rs @@ -69,4 +69,7 @@ pub enum PgError { #[error(r#"Unable to get tile {2:#} with {:?} params from {1}: {0}"#, query_to_json(.3.as_ref()))] GetTileWithQueryError(#[source] TokioPgError, String, TileCoord, Option), + + #[error("Validation error: {0}")] + ValidationError(String), } From d9af9e6360908d49830968423b993d1e28449ab9 Mon Sep 17 00:00:00 2001 From: Saru2003 Date: Mon, 21 Oct 2024 22:35:24 +0530 Subject: [PATCH 05/10] Rearrange imports in config.rs --- martin/src/pg/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/martin/src/pg/config.rs b/martin/src/pg/config.rs index fe4c1d653..f6b52ad64 100644 --- a/martin/src/pg/config.rs +++ b/martin/src/pg/config.rs @@ -13,10 +13,10 @@ use crate::pg::config_function::FuncInfoSources; use crate::pg::config_table::TableInfoSources; use crate::pg::utils::on_slow; use crate::pg::PgResult; +use crate::pg::PgError; use crate::source::TileInfoSources; use crate::utils::{IdResolver, OptBoolObj, OptOneMany}; use crate::MartinResult; -use crate::pg::PgError; pub trait PgInfo { fn format_id(&self) -> String; From d847bee916b2292d5e12acfdcdda592453c2ef09 Mon Sep 17 00:00:00 2001 From: Saru2003 Date: Mon, 21 Oct 2024 22:39:30 +0530 Subject: [PATCH 06/10] Rearranged use statements in config.rs for clarity and convention. --- martin/src/pg/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/martin/src/pg/config.rs b/martin/src/pg/config.rs index f6b52ad64..0b95411a1 100644 --- a/martin/src/pg/config.rs +++ b/martin/src/pg/config.rs @@ -12,8 +12,8 @@ use crate::pg::builder::PgBuilder; use crate::pg::config_function::FuncInfoSources; use crate::pg::config_table::TableInfoSources; use crate::pg::utils::on_slow; -use crate::pg::PgResult; use crate::pg::PgError; +use crate::pg::PgResult; use crate::source::TileInfoSources; use crate::utils::{IdResolver, OptBoolObj, OptOneMany}; use crate::MartinResult; From 5f69d95f1adda851a0a397d2260cab982acca8a8 Mon Sep 17 00:00:00 2001 From: Saru2003 Date: Wed, 23 Oct 2024 14:46:20 +0530 Subject: [PATCH 07/10] Refactor config validation and error handling --- martin/src/pg/config.rs | 9 ++------- martin/src/pg/errors.rs | 4 ++-- martin/src/pg/pool.rs | 2 -- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/martin/src/pg/config.rs b/martin/src/pg/config.rs index 0b95411a1..fd4fbe877 100644 --- a/martin/src/pg/config.rs +++ b/martin/src/pg/config.rs @@ -98,15 +98,11 @@ impl PgConfig { pub fn validate(&self) -> PgResult<()> { if let Some(pool_size) = self.pool_size { if pool_size < 1 { - return Err(PgError::ValidationError( - "pool_size must be greater than or equal to 1.".to_string(), - )); + return Err(PgError::ConfigError("pool_size must be greater than or equal to 1.")); } } if self.connection_string.is_none() { - return Err(PgError::ValidationError( - "A connection string must be provided.".to_string(), - )); + return Err(PgError::ConfigError("A connection string must be provided.")); } Ok(()) @@ -133,7 +129,6 @@ impl PgConfig { } 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/errors.rs b/martin/src/pg/errors.rs index 4e83a2ab9..d4220274f 100644 --- a/martin/src/pg/errors.rs +++ b/martin/src/pg/errors.rs @@ -70,6 +70,6 @@ pub enum PgError { #[error(r#"Unable to get tile {2:#} with {:?} params from {1}: {0}"#, query_to_json(.3.as_ref()))] GetTileWithQueryError(#[source] TokioPgError, String, TileCoord, Option), - #[error("Validation error: {0}")] - ValidationError(String), + #[error("Configuration error: {0}")] + ConfigError(&'static str), } diff --git a/martin/src/pg/pool.rs b/martin/src/pg/pool.rs index 52efc32a0..efa7d3ec2 100755 --- a/martin/src/pg/pool.rs +++ b/martin/src/pg/pool.rs @@ -33,8 +33,6 @@ 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) From 36911fc67fcb63eda62cd570904fc4197dede66e Mon Sep 17 00:00:00 2001 From: Saru2003 Date: Wed, 23 Oct 2024 15:03:59 +0530 Subject: [PATCH 08/10] Fixing lint issue in updated validate return --- martin/src/pg/config.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/martin/src/pg/config.rs b/martin/src/pg/config.rs index fd4fbe877..1aa855a76 100644 --- a/martin/src/pg/config.rs +++ b/martin/src/pg/config.rs @@ -98,11 +98,15 @@ impl PgConfig { pub fn validate(&self) -> PgResult<()> { if let Some(pool_size) = self.pool_size { if pool_size < 1 { - return Err(PgError::ConfigError("pool_size must be greater than or equal to 1.")); + return Err(PgError::ConfigError( + "pool_size must be greater than or equal to 1.", + )); } } if self.connection_string.is_none() { - return Err(PgError::ConfigError("A connection string must be provided.")); + return Err(PgError::ConfigError( + "A connection string must be provided.", + )); } Ok(()) From 7ca992a42281e74d5f7b79bf875130cd9603838e Mon Sep 17 00:00:00 2001 From: Saru2003 Date: Wed, 23 Oct 2024 15:07:14 +0530 Subject: [PATCH 09/10] Fixing lint issue in updated validate return, v2 --- martin/src/pg/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/martin/src/pg/config.rs b/martin/src/pg/config.rs index 1aa855a76..856383c4f 100644 --- a/martin/src/pg/config.rs +++ b/martin/src/pg/config.rs @@ -100,13 +100,13 @@ impl PgConfig { if pool_size < 1 { return Err(PgError::ConfigError( "pool_size must be greater than or equal to 1.", - )); + )); } } if self.connection_string.is_none() { return Err(PgError::ConfigError( "A connection string must be provided.", - )); + )); } Ok(()) From fd8d60b7632c159086ffe8289263d675280b84b6 Mon Sep 17 00:00:00 2001 From: Saru2003 Date: Wed, 23 Oct 2024 15:11:29 +0530 Subject: [PATCH 10/10] Fixing lint issue in updated validate return, v3 --- martin/src/pg/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/martin/src/pg/config.rs b/martin/src/pg/config.rs index 856383c4f..b25f5e7ec 100644 --- a/martin/src/pg/config.rs +++ b/martin/src/pg/config.rs @@ -99,13 +99,13 @@ impl PgConfig { if let Some(pool_size) = self.pool_size { if pool_size < 1 { return Err(PgError::ConfigError( - "pool_size must be greater than or equal to 1.", + "pool_size must be greater than or equal to 1.", )); } } if self.connection_string.is_none() { return Err(PgError::ConfigError( - "A connection string must be provided.", + "A connection string must be provided.", )); }