Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't require dropping validation layer to handle validation errors #2216

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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