Skip to content

Commit

Permalink
Merge pull request #1436 from hannobraun/validation
Browse files Browse the repository at this point in the history
Clean up validation
  • Loading branch information
hannobraun authored Dec 11, 2022
2 parents ac8a8da + bf2d49f commit ba70938
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::objects::Objects;
pub use self::{
objects::ServiceObjectsExt,
service::{Service, State},
validation::{Validation, ValidationEvent},
validation::{Validation, ValidationFailed},
};

/// The kernel services
Expand Down
36 changes: 15 additions & 21 deletions crates/fj-kernel/src/services/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use super::{objects::ObjectToInsert, State};

/// Errors that occurred while validating the objects inserted into the stores
#[derive(Default)]
pub struct Validation(
pub BTreeMap<ObjectId, (Object<BehindHandle>, ValidationError)>,
);
pub struct Validation(pub BTreeMap<ObjectId, ValidationFailed>);

impl Drop for Validation {
fn drop(&mut self) {
Expand All @@ -23,8 +21,8 @@ impl Drop for Validation {
errors:"
);

for (_, err) in self.0.values() {
println!("{err}");
for event in self.0.values() {
println!("{}", event.err);
}

if !thread::panicking() {
Expand All @@ -36,32 +34,28 @@ impl Drop for Validation {

impl State for Validation {
type Command = ObjectToInsert;
type Event = ValidationEvent;
type Event = ValidationFailed;

fn decide(&self, command: Self::Command, events: &mut Vec<Self::Event>) {
let err = command.object.validate().err();
events.push(ValidationEvent {
object: command.object.into(),
err,
});
if let Err(err) = command.object.validate() {
events.push(ValidationFailed {
object: command.object.into(),
err,
});
}
}

fn evolve(&mut self, event: &Self::Event) {
if let Some(err) = &event.err {
self.0
.insert(event.object.id(), (event.object.clone(), err.clone()));
}
self.0.insert(event.object.id(), event.clone());
}
}

/// An event produced by the validation service
#[derive(Clone)]
pub struct ValidationEvent {
/// The object for which validation has been attempted
pub struct ValidationFailed {
/// The object for which validation failed
pub object: Object<BehindHandle>,

/// The validation error, if the validation resulted in one
///
/// If this is `None`, the object has been validated successfully.
pub err: Option<ValidationError>,
/// The validation error
pub err: ValidationError,
}

0 comments on commit ba70938

Please sign in to comment.