diff --git a/crates/fj-core/src/validate/cycle.rs b/crates/fj-core/src/validate/cycle.rs index a1d628605..dc9b125ec 100644 --- a/crates/fj-core/src/validate/cycle.rs +++ b/crates/fj-core/src/validate/cycle.rs @@ -1,6 +1,9 @@ use crate::{ objects::Cycle, - validation::{ValidationCheck, ValidationConfig, ValidationError}, + validation::{ + checks::AdjacentHalfEdgesNotConnected, ValidationCheck, + ValidationConfig, ValidationError, + }, }; use super::Validate; @@ -11,6 +14,8 @@ impl Validate for Cycle { config: &ValidationConfig, errors: &mut Vec, ) { - errors.extend(self.check(config).map(Into::into)); + errors.extend( + AdjacentHalfEdgesNotConnected::check(self, config).map(Into::into), + ); } } diff --git a/crates/fj-core/src/validation/checks/half_edge_connection.rs b/crates/fj-core/src/validation/checks/half_edge_connection.rs index 296f5ac86..04f45b2e9 100644 --- a/crates/fj-core/src/validation/checks/half_edge_connection.rs +++ b/crates/fj-core/src/validation/checks/half_edge_connection.rs @@ -37,12 +37,12 @@ pub struct AdjacentHalfEdgesNotConnected { pub unconnected_half_edges: [Handle; 2], } -impl ValidationCheck for Cycle { +impl ValidationCheck for AdjacentHalfEdgesNotConnected { fn check( - &self, + object: &Cycle, config: &ValidationConfig, - ) -> impl Iterator { - self.half_edges().pairs().filter_map(|(first, second)| { + ) -> impl Iterator { + object.half_edges().pairs().filter_map(|(first, second)| { let end_pos_of_first_half_edge = { let [_, end] = first.boundary().inner; first.path().point_from_path_coords(end) @@ -80,12 +80,14 @@ mod tests { Core, }; + use super::AdjacentHalfEdgesNotConnected; + #[test] fn adjacent_half_edges_connected() -> anyhow::Result<()> { let mut core = Core::new(); let valid = Cycle::polygon([[0., 0.], [1., 0.], [1., 1.]], &mut core); - valid.check_and_return_first_error()?; + AdjacentHalfEdgesNotConnected::check_and_return_first_error(&valid)?; let invalid = valid.update_half_edge( valid.half_edges().first(), @@ -94,7 +96,7 @@ mod tests { }, &mut core, ); - invalid.check_and_expect_one_error(); + AdjacentHalfEdgesNotConnected::check_and_expect_one_error(&invalid); Ok(()) } diff --git a/crates/fj-core/src/validation/validation_check.rs b/crates/fj-core/src/validation/validation_check.rs index 5e30c92a5..9e75b1aef 100644 --- a/crates/fj-core/src/validation/validation_check.rs +++ b/crates/fj-core/src/validation/validation_check.rs @@ -6,19 +6,22 @@ use super::ValidationConfig; /// /// This trait is implemented once per validation check and object it applies /// to. `Self` is the object, while `T` identifies the validation check. -pub trait ValidationCheck { +pub trait ValidationCheck: Sized { /// Run the validation check on the implementing object - fn check(&self, config: &ValidationConfig) -> impl Iterator; + fn check( + object: &T, + config: &ValidationConfig, + ) -> impl Iterator; /// Convenience method to run the check return the first error /// /// This method is designed for convenience over flexibility (it is intended /// for use in unit tests), and thus always uses the default configuration. - fn check_and_return_first_error(&self) -> Result<(), T> { - let errors = - self.check(&ValidationConfig::default()).collect::>(); + fn check_and_return_first_error(object: &T) -> Result<(), Self> { + let config = ValidationConfig::default(); + let mut errors = Self::check(object, &config); - if let Some(err) = errors.into_iter().next() { + if let Some(err) = errors.next() { return Err(err); } @@ -29,14 +32,14 @@ pub trait ValidationCheck { /// /// This method is designed for convenience over flexibility (it is intended /// for use in unit tests), and thus always uses the default configuration. - fn check_and_expect_one_error(&self) + fn check_and_expect_one_error(object: &T) -> Self where - T: Display, + Self: Display, { let config = ValidationConfig::default(); - let mut errors = self.check(&config).peekable(); + let mut errors = Self::check(object, &config).peekable(); - errors + let err = errors .next() .expect("Expected one validation error; none found"); @@ -49,5 +52,7 @@ pub trait ValidationCheck { panic!("Expected only one validation error") } + + err } }