From 81e08408fe426e667c1270416d2b814601151e68 Mon Sep 17 00:00:00 2001 From: David Brungardt <34960989+brungardtdb@users.noreply.github.com> Date: Sat, 23 Dec 2023 09:26:16 -0600 Subject: [PATCH 1/3] Added a second constructor to services that takes a validation config. Then passed the validation config into the validation service. --- crates/fj-core/src/services/mod.rs | 14 ++++++++++- crates/fj-core/src/services/service.rs | 8 +++++- crates/fj-core/src/services/validation.rs | 30 +++++++++++++++++++++-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/crates/fj-core/src/services/mod.rs b/crates/fj-core/src/services/mod.rs index b2820a436..07dfc3abe 100644 --- a/crates/fj-core/src/services/mod.rs +++ b/crates/fj-core/src/services/mod.rs @@ -8,7 +8,7 @@ mod validation; use crate::{ objects::{Object, Objects, WithHandle}, - validate::ValidationErrors, + validate::{ValidationConfig, ValidationErrors}, }; pub use self::{ @@ -42,6 +42,18 @@ impl Services { } } + /// Construct an instance of `Services` with a pre-defined configuration for the validation service + pub fn with_validation_config(config: &ValidationConfig) -> Self { + let objects = Service::::default(); + let mut validation = Service::::default(); + validation + .evolve(&ValidationEvent::ConfigurationDefined { config: *config }); + Self { + objects, + validation, + } + } + /// Insert an object into the stores pub fn insert_object(&mut self, object: Object) { let mut object_events = Vec::new(); diff --git a/crates/fj-core/src/services/service.rs b/crates/fj-core/src/services/service.rs index 4317fa61d..bacacd8b4 100644 --- a/crates/fj-core/src/services/service.rs +++ b/crates/fj-core/src/services/service.rs @@ -1,4 +1,4 @@ -use std::ops::Deref; +use std::ops::{Deref, DerefMut}; /// A service that controls access to some state /// @@ -62,6 +62,12 @@ impl Deref for Service { } } +impl DerefMut for Service { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.state + } +} + impl Default for Service where S: Default, diff --git a/crates/fj-core/src/services/validation.rs b/crates/fj-core/src/services/validation.rs index cbfea30f6..c40cce03d 100644 --- a/crates/fj-core/src/services/validation.rs +++ b/crates/fj-core/src/services/validation.rs @@ -3,16 +3,34 @@ use std::{collections::BTreeMap, error::Error, thread}; use crate::{ objects::{BehindHandle, Object}, storage::ObjectId, - validate::ValidationError, + validate::{ValidationConfig, ValidationError}, }; use super::State; /// Errors that occurred while validating the objects inserted into the stores -#[derive(Default)] pub struct Validation { /// All unhandled validation errors pub errors: BTreeMap, + /// Optional validation config + config: Option, +} + +impl Validation { + fn with_validation_config(&mut self, config: &ValidationConfig) -> &Self { + self.config = Some(*config); + self + } +} + +impl Default for Validation { + fn default() -> Self { + let errors = BTreeMap::new(); + Self { + errors, + config: None, + } + } } impl Drop for Validation { @@ -71,6 +89,9 @@ impl State for Validation { ValidationEvent::ValidationFailed { object, err } => { self.errors.insert(object.id(), err.clone()); } + ValidationEvent::ConfigurationDefined { config } => { + self.with_validation_config(config); + } } } } @@ -95,4 +116,9 @@ pub enum ValidationEvent { /// The validation error err: ValidationError, }, + /// A validation configuration has been defined + ConfigurationDefined { + /// The predefined validation configuration + config: ValidationConfig, + }, } From c411a636979005b0c849b2e891896c258cbb2a5d Mon Sep 17 00:00:00 2001 From: David Brungardt <34960989+brungardtdb@users.noreply.github.com> Date: Sat, 23 Dec 2023 09:40:33 -0600 Subject: [PATCH 2/3] Added functionality to use optional validation config if it is defined to the validation service, and added validate_with_config() method to object.rs --- crates/fj-core/src/objects/object.rs | 11 ++++++++++- crates/fj-core/src/services/validation.rs | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/fj-core/src/objects/object.rs b/crates/fj-core/src/objects/object.rs index 3cddf3f7a..c303532c9 100644 --- a/crates/fj-core/src/objects/object.rs +++ b/crates/fj-core/src/objects/object.rs @@ -4,7 +4,7 @@ use crate::{ Surface, Vertex, }, storage::{Handle, HandleWrapper, ObjectId}, - validate::{Validate, ValidationError}, + validate::{Validate, ValidationConfig, ValidationError}, }; macro_rules! object { @@ -40,6 +40,15 @@ macro_rules! object { )* } } + + /// Validate the object with a pre-defined validation configuration + pub fn validate_with_config(&self, config: &ValidationConfig, errors: &mut Vec) { + match self { + $( + Self::$ty(object) => object.validate_with_config(config, errors), + )* + } + } } impl Object { diff --git a/crates/fj-core/src/services/validation.rs b/crates/fj-core/src/services/validation.rs index c40cce03d..6f674a341 100644 --- a/crates/fj-core/src/services/validation.rs +++ b/crates/fj-core/src/services/validation.rs @@ -72,6 +72,10 @@ impl State for Validation { match command { ValidationCommand::ValidateObject { object } => { + match self.config { + Some(c) => object.validate_with_config(&c, &mut errors), + None => object.validate(&mut errors), + } object.validate(&mut errors); for err in errors { From f57c77ef6ac8020b82245f3cf106f18817a03a22 Mon Sep 17 00:00:00 2001 From: David Brungardt <34960989+brungardtdb@users.noreply.github.com> Date: Sat, 13 Jan 2024 17:58:56 -0600 Subject: [PATCH 3/3] Responding to PR feedback Passed in ValidationConfig directly instead of cloning a reference. Removed optional ValidationConfig for ValidationService, use default or config passed in through alternate constructor. Deleted DerefMut implementation. --- crates/fj-core/src/services/mod.rs | 7 +++-- crates/fj-core/src/services/service.rs | 8 +----- crates/fj-core/src/services/validation.rs | 31 +++++++---------------- 3 files changed, 13 insertions(+), 33 deletions(-) diff --git a/crates/fj-core/src/services/mod.rs b/crates/fj-core/src/services/mod.rs index 07dfc3abe..0bdf554b9 100644 --- a/crates/fj-core/src/services/mod.rs +++ b/crates/fj-core/src/services/mod.rs @@ -43,11 +43,10 @@ impl Services { } /// Construct an instance of `Services` with a pre-defined configuration for the validation service - pub fn with_validation_config(config: &ValidationConfig) -> Self { + pub fn with_validation_config(config: ValidationConfig) -> Self { let objects = Service::::default(); - let mut validation = Service::::default(); - validation - .evolve(&ValidationEvent::ConfigurationDefined { config: *config }); + let validation = + Service::new(Validation::with_validation_config(config)); Self { objects, validation, diff --git a/crates/fj-core/src/services/service.rs b/crates/fj-core/src/services/service.rs index bacacd8b4..4317fa61d 100644 --- a/crates/fj-core/src/services/service.rs +++ b/crates/fj-core/src/services/service.rs @@ -1,4 +1,4 @@ -use std::ops::{Deref, DerefMut}; +use std::ops::Deref; /// A service that controls access to some state /// @@ -62,12 +62,6 @@ impl Deref for Service { } } -impl DerefMut for Service { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.state - } -} - impl Default for Service where S: Default, diff --git a/crates/fj-core/src/services/validation.rs b/crates/fj-core/src/services/validation.rs index 6f674a341..a8cc20e58 100644 --- a/crates/fj-core/src/services/validation.rs +++ b/crates/fj-core/src/services/validation.rs @@ -12,24 +12,23 @@ use super::State; pub struct Validation { /// All unhandled validation errors pub errors: BTreeMap, - /// Optional validation config - config: Option, + /// Validation configuration for the validation service + config: ValidationConfig, } impl Validation { - fn with_validation_config(&mut self, config: &ValidationConfig) -> &Self { - self.config = Some(*config); - self + /// A constructor for the validation service that allows a validation configuration to be set for the service + pub fn with_validation_config(config: ValidationConfig) -> Self { + let errors = BTreeMap::new(); + Self { errors, config } } } impl Default for Validation { fn default() -> Self { let errors = BTreeMap::new(); - Self { - errors, - config: None, - } + let config: ValidationConfig = ValidationConfig::default(); + Self { errors, config } } } @@ -72,11 +71,7 @@ impl State for Validation { match command { ValidationCommand::ValidateObject { object } => { - match self.config { - Some(c) => object.validate_with_config(&c, &mut errors), - None => object.validate(&mut errors), - } - object.validate(&mut errors); + object.validate_with_config(&self.config, &mut errors); for err in errors { events.push(ValidationEvent::ValidationFailed { @@ -93,9 +88,6 @@ impl State for Validation { ValidationEvent::ValidationFailed { object, err } => { self.errors.insert(object.id(), err.clone()); } - ValidationEvent::ConfigurationDefined { config } => { - self.with_validation_config(config); - } } } } @@ -120,9 +112,4 @@ pub enum ValidationEvent { /// The validation error err: ValidationError, }, - /// A validation configuration has been defined - ConfigurationDefined { - /// The predefined validation configuration - config: ValidationConfig, - }, }