Skip to content

Commit

Permalink
Add TakeErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Feb 15, 2024
1 parent 0b3ac30 commit 61ad7ba
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/fj-core/src/layers/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ impl Command<Validation> for InsertObject {
}
}

/// Take all errors stored in the validation layer
///
/// Serves both as a command for and event produced by `Layer<Validation>`.
pub struct TakeErrors;

impl Command<Validation> for TakeErrors {
type Result = Result<(), ValidationErrors>;
type Event = Self;

fn decide(
self,
state: &Validation,
events: &mut Vec<Self::Event>,
) -> Self::Result {
let errors = ValidationErrors(state.errors.values().cloned().collect());

events.push(self);

if errors.0.is_empty() {
Ok(())
} else {
Err(errors)
}
}
}

impl Event<Validation> for TakeErrors {
fn evolve(&self, state: &mut Validation) {
state.errors.clear();
}
}

/// Validation of an object failed
///
/// Event produced by `Layer<Validation>`.
Expand Down

0 comments on commit 61ad7ba

Please sign in to comment.