diff --git a/crates/fj-core/src/layers/validation.rs b/crates/fj-core/src/layers/validation.rs index c43fca8a5..3205ad30d 100644 --- a/crates/fj-core/src/layers/validation.rs +++ b/crates/fj-core/src/layers/validation.rs @@ -39,6 +39,38 @@ impl Command for InsertObject { } } +/// Take all errors stored in the validation layer +/// +/// Serves both as a command for and event produced by `Layer`. +pub struct TakeErrors; + +impl Command for TakeErrors { + type Result = Result<(), ValidationErrors>; + type Event = Self; + + fn decide( + self, + state: &Validation, + events: &mut Vec, + ) -> Self::Result { + let errors = ValidationErrors(state.errors.values().cloned().collect()); + + events.push(self); + + if errors.0.is_empty() { + Ok(()) + } else { + Err(errors) + } + } +} + +impl Event for TakeErrors { + fn evolve(&self, state: &mut Validation) { + state.errors.clear(); + } +} + /// Validation of an object failed /// /// Event produced by `Layer`.