diff --git a/crates/fj-kernel/src/objects/object.rs b/crates/fj-kernel/src/objects/object.rs index 22c9d16f20..a538400090 100644 --- a/crates/fj-kernel/src/objects/object.rs +++ b/crates/fj-kernel/src/objects/object.rs @@ -31,6 +31,15 @@ macro_rules! object { )* } } + + /// Validate the object + pub fn validate(&self, errors: &mut Vec) { + match self { + $( + Self::$ty(object) => object.validate(errors), + )* + } + } } impl Object { @@ -47,15 +56,6 @@ macro_rules! object { )* } } - - /// Validate the object - pub fn validate(&self, errors: &mut Vec) { - match self { - $( - Self::$ty((_, object)) => object.validate(errors), - )* - } - } } impl From> for Object { diff --git a/crates/fj-kernel/src/services/mod.rs b/crates/fj-kernel/src/services/mod.rs index 561a5acfc7..79482136d2 100644 --- a/crates/fj-kernel/src/services/mod.rs +++ b/crates/fj-kernel/src/services/mod.rs @@ -11,7 +11,7 @@ use crate::objects::{Object, Objects, WithHandle}; pub use self::{ objects::{InsertObject, Operation}, service::{Service, State}, - validation::{Validation, ValidationFailed}, + validation::{Validation, ValidationCommand, ValidationEvent}, }; /// The kernel services @@ -46,7 +46,10 @@ impl Services { .execute(Operation::InsertObject { object }, &mut object_events); for object_event in object_events { - self.validation.execute(object_event, &mut Vec::new()); + let command = ValidationCommand::ValidateObject { + object: object_event.object.into(), + }; + self.validation.execute(command, &mut Vec::new()); } } } diff --git a/crates/fj-kernel/src/services/validation.rs b/crates/fj-kernel/src/services/validation.rs index 5e0717a8f5..326ff87383 100644 --- a/crates/fj-kernel/src/services/validation.rs +++ b/crates/fj-kernel/src/services/validation.rs @@ -6,23 +6,25 @@ use crate::{ validate::ValidationError, }; -use super::{objects::InsertObject, State}; +use super::State; /// Errors that occurred while validating the objects inserted into the stores #[derive(Default)] -pub struct Validation(pub BTreeMap); +pub struct Validation { + errors: BTreeMap, +} impl Drop for Validation { fn drop(&mut self) { - let num_errors = self.0.len(); + let num_errors = self.errors.len(); if num_errors > 0 { println!( "Dropping `Validation` with {num_errors} unhandled validation \ errors:" ); - for event in self.0.values() { - println!("{}", event.err); + for err in self.errors.values() { + println!("{}", err); } if !thread::panicking() { @@ -33,32 +35,50 @@ impl Drop for Validation { } impl State for Validation { - type Command = InsertObject; - type Event = ValidationFailed; + type Command = ValidationCommand; + type Event = ValidationEvent; fn decide(&self, command: Self::Command, events: &mut Vec) { + let ValidationCommand::ValidateObject { object } = command; + let mut errors = Vec::new(); - command.object.validate(&mut errors); + object.validate(&mut errors); for err in errors { - events.push(ValidationFailed { - object: command.object.clone().into(), + events.push(ValidationEvent::ValidationFailed { + object: object.clone(), err, }); } } fn evolve(&mut self, event: &Self::Event) { - self.0.insert(event.object.id(), event.clone()); + match event { + ValidationEvent::ValidationFailed { object, err } => { + self.errors.insert(object.id(), err.clone()); + } + } } } -/// An event produced by the validation service +/// The command accepted by the validation service +pub enum ValidationCommand { + /// Validate the provided object + ValidateObject { + /// The object to validate + object: Object, + }, +} + +/// The event produced by the validation service #[derive(Clone)] -pub struct ValidationFailed { - /// The object for which validation failed - pub object: Object, +pub enum ValidationEvent { + /// Validation of an object failed + ValidationFailed { + /// The object for which validation failed + object: Object, - /// The validation error - pub err: ValidationError, + /// The validation error + err: ValidationError, + }, }