From 61ad7ba9ea51ed9a9fbcaf19a918b9149f29f74e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 15 Feb 2024 12:43:38 +0100 Subject: [PATCH] Add `TakeErrors` --- crates/fj-core/src/layers/validation.rs | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) 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`.