diff --git a/crates/fj-core/src/services/mod.rs b/crates/fj-core/src/services/mod.rs index 9698869b2b..892bfb881c 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,20 +34,15 @@ 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 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 { objects, validation, @@ -57,21 +53,19 @@ 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()); } } /// 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(()) @@ -80,9 +74,3 @@ impl Services { } } } - -impl Default for Services { - fn default() -> Self { - Self::new() - } -} diff --git a/crates/fj-core/src/services/service.rs b/crates/fj-core/src/services/service.rs index 8069898c53..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 { @@ -41,16 +41,9 @@ impl Service { } } - /// 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); - } + /// Drop this instance, returning the wrapped state + pub fn into_state(self) -> S { + self.state } } @@ -62,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, diff --git a/crates/fj-core/src/services/validation.rs b/crates/fj-core/src/services/validation.rs index 66b9167619..0c41e492da 100644 --- a/crates/fj-core/src/services/validation.rs +++ b/crates/fj-core/src/services/validation.rs @@ -1,17 +1,19 @@ -use std::{collections::BTreeMap, error::Error, thread}; +use std::{collections::HashMap, error::Error, thread}; use crate::{ objects::{AnyObject, Stored}, storage::ObjectId, - validate::{ValidationConfig, ValidationError}, + validate::{ValidationConfig, ValidationError, ValidationErrors}, }; 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, + errors: HashMap, + /// Validation configuration for the validation service config: ValidationConfig, } @@ -19,16 +21,13 @@ 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 config: ValidationConfig = ValidationConfig::default(); - 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()) } } 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 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(()) }