Skip to content

Commit

Permalink
Merge pull request #2216 from hannobraun/validation
Browse files Browse the repository at this point in the history
Don't require dropping validation layer to handle validation errors
  • Loading branch information
hannobraun authored Feb 15, 2024
2 parents 0b3ac30 + 2f24509 commit b5d0c79
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
44 changes: 35 additions & 9 deletions crates/fj-core/src/layers/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@ use crate::{
use super::{objects::InsertObject, Command, Event, Layer};

impl Layer<Validation> {
/// Consume the validation layer, returning any validation errors
pub fn into_result(self) -> Result<(), ValidationErrors> {
let errors = self.into_state().into_errors();

if errors.0.is_empty() {
Ok(())
} else {
Err(errors)
}
/// Take all errors stored in the validation layer
pub fn take_errors(&mut self) -> Result<(), ValidationErrors> {
self.process(TakeErrors, &mut Vec::new())
}
}

Expand All @@ -39,6 +33,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
5 changes: 0 additions & 5 deletions crates/fj-core/src/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ impl Validation {
let errors = HashMap::new();
Self { errors, config }
}

/// Drop this instance, returning the errors it contained
pub fn into_errors(mut self) -> ValidationErrors {
ValidationErrors(self.errors.drain().map(|(_, error)| error).collect())
}
}

impl Drop for Validation {
Expand Down
8 changes: 4 additions & 4 deletions crates/fj-core/src/validate/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ mod tests {
valid_solid.validate_and_return_first_error()?;

// Ignore remaining validation errors.
let _ = core.layers.validation.into_result();
let _ = core.layers.validation.take_errors();

Ok(())
}
Expand Down Expand Up @@ -289,7 +289,7 @@ mod tests {
valid_solid.validate_and_return_first_error()?;

// Ignore remaining validation errors.
let _ = core.layers.validation.into_result();
let _ = core.layers.validation.take_errors();

Ok(())
}
Expand Down Expand Up @@ -339,7 +339,7 @@ mod tests {
valid_solid.validate_and_return_first_error()?;

// Ignore remaining validation errors.
let _ = core.layers.validation.into_result();
let _ = core.layers.validation.take_errors();

Ok(())
}
Expand Down Expand Up @@ -379,7 +379,7 @@ mod tests {
valid_solid.validate_and_return_first_error()?;

// Ignore remaining validation errors.
let _ = core.layers.validation.into_result();
let _ = core.layers.validation.take_errors();

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/fj/src/handle_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::Args;
///
/// This function is used by Fornjot's own testing infrastructure, but is useful
/// beyond that, when using Fornjot directly to define a model.
pub fn handle_model<M>(model: &M, core: Instance) -> Result
pub fn handle_model<M>(model: &M, mut core: Instance) -> Result
where
for<'r> (&'r M, Tolerance): Triangulate,
M: BoundingVolume<3>,
Expand All @@ -36,7 +36,7 @@ where
let args = Args::parse();

if !args.ignore_validation {
core.layers.validation.into_result()?;
core.layers.validation.take_errors()?;
}

let aabb = model.aabb().unwrap_or(Aabb {
Expand Down

0 comments on commit b5d0c79

Please sign in to comment.