From abcc7d69fe10fa8ff6730a03e81a1fc90bd0a616 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 10:44:45 +0100 Subject: [PATCH 01/15] Remove unused `Service::replay` --- crates/fj-core/src/services/service.rs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/crates/fj-core/src/services/service.rs b/crates/fj-core/src/services/service.rs index 8069898c53..93e22089b3 100644 --- a/crates/fj-core/src/services/service.rs +++ b/crates/fj-core/src/services/service.rs @@ -40,18 +40,6 @@ impl Service { self.state.evolve(event); } } - - /// Replay the provided events on the given state - pub fn replay<'event>( - state: &mut S, - events: impl IntoIterator, - ) where - ::Event: 'event, - { - for event in events { - state.evolve(event); - } - } } impl Deref for Service { From e9b9ac2a910faad672858782c48e5c1bbf89f9fe Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 10:54:51 +0100 Subject: [PATCH 02/15] Add `Service::into_state` --- crates/fj-core/src/services/service.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/fj-core/src/services/service.rs b/crates/fj-core/src/services/service.rs index 93e22089b3..b3e3d3cabb 100644 --- a/crates/fj-core/src/services/service.rs +++ b/crates/fj-core/src/services/service.rs @@ -40,6 +40,11 @@ impl Service { self.state.evolve(event); } } + + /// Drop this instance, returning the wrapped state + pub fn into_state(self) -> S { + self.state + } } impl Deref for Service { From 71249c8dc32ee0cc0f455d42cc0f26c6f077ad2c Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 11:03:41 +0100 Subject: [PATCH 03/15] Improve formatting --- crates/fj-core/src/services/validation.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/fj-core/src/services/validation.rs b/crates/fj-core/src/services/validation.rs index 66b9167619..ed7a03794d 100644 --- a/crates/fj-core/src/services/validation.rs +++ b/crates/fj-core/src/services/validation.rs @@ -12,6 +12,7 @@ use super::State; pub struct Validation { /// All unhandled validation errors pub errors: BTreeMap, + /// Validation configuration for the validation service config: ValidationConfig, } From b42c7014f1ad72406603b93b2ad063b7ffa464f2 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 11:05:15 +0100 Subject: [PATCH 04/15] Refactor to prepare for follow-on change --- crates/fj-core/src/services/validation.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/fj-core/src/services/validation.rs b/crates/fj-core/src/services/validation.rs index ed7a03794d..9b21d2643f 100644 --- a/crates/fj-core/src/services/validation.rs +++ b/crates/fj-core/src/services/validation.rs @@ -1,4 +1,4 @@ -use std::{collections::BTreeMap, error::Error, thread}; +use std::{collections::HashMap, error::Error, thread}; use crate::{ objects::{AnyObject, Stored}, @@ -11,7 +11,7 @@ use super::State; /// Errors that occurred while validating the objects inserted into the stores pub struct Validation { /// All unhandled validation errors - pub errors: BTreeMap, + pub errors: HashMap, /// Validation configuration for the validation service config: ValidationConfig, @@ -20,14 +20,14 @@ pub struct Validation { impl Validation { /// 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(); + let errors = HashMap::new(); Self { errors, config } } } impl Default for Validation { fn default() -> Self { - let errors = BTreeMap::new(); + let errors = HashMap::new(); let config: ValidationConfig = ValidationConfig::default(); Self { errors, config } } From c0311cb32523fb64aac368aeec5697368ea2e6ff Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 11:09:53 +0100 Subject: [PATCH 05/15] Add `Validation::into_errors` --- crates/fj-core/src/services/validation.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/fj-core/src/services/validation.rs b/crates/fj-core/src/services/validation.rs index 9b21d2643f..c61adea362 100644 --- a/crates/fj-core/src/services/validation.rs +++ b/crates/fj-core/src/services/validation.rs @@ -3,7 +3,7 @@ use std::{collections::HashMap, error::Error, thread}; use crate::{ objects::{AnyObject, Stored}, storage::ObjectId, - validate::{ValidationConfig, ValidationError}, + validate::{ValidationConfig, ValidationError, ValidationErrors}, }; use super::State; @@ -23,6 +23,11 @@ impl Validation { let errors = HashMap::new(); Self { errors, config } } + + /// Drop this instance, returning the errors it contained + pub fn into_errors(mut self) -> ValidationErrors { + ValidationErrors(self.errors.drain().map(|(_, error)| error).collect()) + } } impl Default for Validation { From b03523fe75fad18e225df4e8786a197369861d5c Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 11:10:10 +0100 Subject: [PATCH 06/15] Don't allow panic after `drop_and_validate` --- crates/fj-core/src/services/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/fj-core/src/services/mod.rs b/crates/fj-core/src/services/mod.rs index 9698869b2b..256c83c21c 100644 --- a/crates/fj-core/src/services/mod.rs +++ b/crates/fj-core/src/services/mod.rs @@ -69,9 +69,7 @@ impl Services { /// Drop `Services`; return any unhandled validation error pub fn drop_and_validate(self) -> Result<(), ValidationErrors> { - let errors = ValidationErrors( - self.validation.errors.values().cloned().collect(), - ); + let errors = self.validation.into_state().into_errors(); if errors.0.is_empty() { Ok(()) From f53d1ca559e54cc456b9ad167e7b168b6e79d7aa Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 11:11:11 +0100 Subject: [PATCH 07/15] Refactor to prepare for follow-on change --- crates/fj-core/src/validate/solid.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/fj-core/src/validate/solid.rs b/crates/fj-core/src/validate/solid.rs index ebaadf15b4..6ab5c17696 100644 --- a/crates/fj-core/src/validate/solid.rs +++ b/crates/fj-core/src/validate/solid.rs @@ -235,7 +235,8 @@ mod tests { let valid_solid = Solid::new(vec![]).insert(&mut core); valid_solid.validate_and_return_first_error()?; - core.services.validation.errors.clear(); + // Ignore remaining validation errors. + let _ = core.services.drop_and_validate(); Ok(()) } @@ -287,7 +288,8 @@ mod tests { let valid_solid = Solid::new(vec![]).insert(&mut core); valid_solid.validate_and_return_first_error()?; - core.services.validation.errors.clear(); + // Ignore remaining validation errors. + let _ = core.services.drop_and_validate(); Ok(()) } @@ -336,7 +338,8 @@ mod tests { let valid_solid = Solid::new(vec![]).insert(&mut core); valid_solid.validate_and_return_first_error()?; - core.services.validation.errors.clear(); + // Ignore remaining validation errors. + let _ = core.services.drop_and_validate(); Ok(()) } @@ -375,7 +378,8 @@ mod tests { let valid_solid = Solid::new(vec![]).insert(&mut core); valid_solid.validate_and_return_first_error()?; - core.services.validation.errors.clear(); + // Ignore remaining validation errors. + let _ = core.services.drop_and_validate(); Ok(()) } From 33827c7c0c346fb7d471d3811df81cf1c0192612 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 11:11:22 +0100 Subject: [PATCH 08/15] Remove unused code --- crates/fj-core/src/services/service.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/crates/fj-core/src/services/service.rs b/crates/fj-core/src/services/service.rs index b3e3d3cabb..4268c223b2 100644 --- a/crates/fj-core/src/services/service.rs +++ b/crates/fj-core/src/services/service.rs @@ -55,13 +55,6 @@ impl Deref for Service { } } -#[cfg(test)] -impl std::ops::DerefMut for Service { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.state - } -} - impl Default for Service where S: Default, From 067c6373bcc17b696bf6d72c5897d75e667af27e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 11:11:38 +0100 Subject: [PATCH 09/15] Remove unnecessary `pub` --- crates/fj-core/src/services/validation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fj-core/src/services/validation.rs b/crates/fj-core/src/services/validation.rs index c61adea362..d79f0d3661 100644 --- a/crates/fj-core/src/services/validation.rs +++ b/crates/fj-core/src/services/validation.rs @@ -11,7 +11,7 @@ use super::State; /// Errors that occurred while validating the objects inserted into the stores pub struct Validation { /// All unhandled validation errors - pub errors: HashMap, + errors: HashMap, /// Validation configuration for the validation service config: ValidationConfig, From be4d4d6d7d18085cd54f52d87b1a4cf7a3288872 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 11:43:42 +0100 Subject: [PATCH 10/15] Remove outdated implementation note --- crates/fj-core/src/validate/mod.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/crates/fj-core/src/validate/mod.rs b/crates/fj-core/src/validate/mod.rs index 271d00a0c7..869b25d817 100644 --- a/crates/fj-core/src/validate/mod.rs +++ b/crates/fj-core/src/validate/mod.rs @@ -60,14 +60,6 @@ //! [`Validate::validate_with_config`] and [`ValidationConfig`]. //! //! -//! ## Implementation Note -//! -//! It is currently not possible to define this configuration for the background -//! validation done by the [`Services`] API. If this is a problem for you, -//! please comment on this issue: -//! -//! -//! //! [`fj-export`]: https://crates.io/crates/fj-export //! [issue tracker]: https://github.com/hannobraun/fornjot/issues //! [`Services`]: crate::services::Services From e67f733e0dd0d5812152665c385e0de4813706a8 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 11:57:51 +0100 Subject: [PATCH 11/15] Remove redundant type declarations --- crates/fj-core/src/services/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-core/src/services/mod.rs b/crates/fj-core/src/services/mod.rs index 256c83c21c..5da4a0b09d 100644 --- a/crates/fj-core/src/services/mod.rs +++ b/crates/fj-core/src/services/mod.rs @@ -33,7 +33,7 @@ pub struct Services { impl Services { /// Construct an instance of `Services` pub fn new() -> Self { - let objects = Service::::default(); + let objects = Service::default(); let validation = Service::default(); Self { @@ -44,7 +44,7 @@ impl Services { /// Construct an instance of `Services`, using the provided configuration pub fn with_validation_config(config: ValidationConfig) -> Self { - let objects = Service::::default(); + let objects = Service::default(); let validation = Service::new(Validation::with_validation_config(config)); Self { From 24fdffe475648dd3aefbb948b6eade00201f41d7 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 11:58:30 +0100 Subject: [PATCH 12/15] Improve formatting --- crates/fj-core/src/services/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/fj-core/src/services/mod.rs b/crates/fj-core/src/services/mod.rs index 5da4a0b09d..df46881a37 100644 --- a/crates/fj-core/src/services/mod.rs +++ b/crates/fj-core/src/services/mod.rs @@ -47,6 +47,7 @@ impl Services { let objects = Service::default(); let validation = Service::new(Validation::with_validation_config(config)); + Self { objects, validation, From 2be5dc7df1d80b6cba0222be3f2cc9c92416124b Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 12:01:22 +0100 Subject: [PATCH 13/15] Refactor to simplify --- crates/fj-core/src/services/mod.rs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/crates/fj-core/src/services/mod.rs b/crates/fj-core/src/services/mod.rs index df46881a37..dc7dd0f7de 100644 --- a/crates/fj-core/src/services/mod.rs +++ b/crates/fj-core/src/services/mod.rs @@ -18,6 +18,7 @@ pub use self::{ }; /// The kernel services +#[derive(Default)] pub struct Services { /// The objects service /// @@ -33,13 +34,7 @@ pub struct Services { impl Services { /// Construct an instance of `Services` pub fn new() -> Self { - let objects = Service::default(); - let validation = Service::default(); - - Self { - objects, - validation, - } + Self::default() } /// Construct an instance of `Services`, using the provided configuration @@ -79,9 +74,3 @@ impl Services { } } } - -impl Default for Services { - fn default() -> Self { - Self::new() - } -} From 22de97ca11ef7a2edbf28615f5e08df86a528377 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 12:16:51 +0100 Subject: [PATCH 14/15] Rename `Services::execute` to `process` This name better matches the nomenclature used in the linked article. --- crates/fj-core/src/services/mod.rs | 4 ++-- crates/fj-core/src/services/service.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/fj-core/src/services/mod.rs b/crates/fj-core/src/services/mod.rs index dc7dd0f7de..892bfb881c 100644 --- a/crates/fj-core/src/services/mod.rs +++ b/crates/fj-core/src/services/mod.rs @@ -53,13 +53,13 @@ impl Services { pub fn insert_object(&mut self, object: AnyObject) { let mut object_events = Vec::new(); self.objects - .execute(Operation::InsertObject { object }, &mut object_events); + .process(Operation::InsertObject { object }, &mut object_events); for object_event in object_events { let command = ValidationCommand::ValidateObject { object: object_event.object.into(), }; - self.validation.execute(command, &mut Vec::new()); + self.validation.process(command, &mut Vec::new()); } } diff --git a/crates/fj-core/src/services/service.rs b/crates/fj-core/src/services/service.rs index 4268c223b2..76471e8dab 100644 --- a/crates/fj-core/src/services/service.rs +++ b/crates/fj-core/src/services/service.rs @@ -33,7 +33,7 @@ impl Service { /// /// The command is executed synchronously. When this method returns, the /// state has been updated and any events have been logged. - pub fn execute(&mut self, command: S::Command, events: &mut Vec) { + pub fn process(&mut self, command: S::Command, events: &mut Vec) { self.state.decide(command, events); for event in events { From e5b9e077c5fe28d320dc87ee45b9f2699936d197 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 13 Feb 2024 12:50:22 +0100 Subject: [PATCH 15/15] Refactor to simplify --- crates/fj-core/src/services/validation.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/crates/fj-core/src/services/validation.rs b/crates/fj-core/src/services/validation.rs index d79f0d3661..0c41e492da 100644 --- a/crates/fj-core/src/services/validation.rs +++ b/crates/fj-core/src/services/validation.rs @@ -9,6 +9,7 @@ use crate::{ use super::State; /// Errors that occurred while validating the objects inserted into the stores +#[derive(Default)] pub struct Validation { /// All unhandled validation errors errors: HashMap, @@ -30,14 +31,6 @@ impl Validation { } } -impl Default for Validation { - fn default() -> Self { - let errors = HashMap::new(); - let config: ValidationConfig = ValidationConfig::default(); - Self { errors, config } - } -} - impl Drop for Validation { fn drop(&mut self) { let num_errors = self.errors.len();