From 7d27b903845a38e81faab36dd9709487a628fb4f Mon Sep 17 00:00:00 2001 From: Nathan Folsom Date: Fri, 29 Dec 2023 15:23:46 -0800 Subject: [PATCH] store both referenced and referencing objects for better error messages --- crates/fj-core/src/validate/references.rs | 20 +++++++------------- crates/fj-core/src/validate/sketch.rs | 4 ++-- crates/fj-core/src/validate/solid.rs | 8 ++++---- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/crates/fj-core/src/validate/references.rs b/crates/fj-core/src/validate/references.rs index d39ab786a5..2277034fdb 100644 --- a/crates/fj-core/src/validate/references.rs +++ b/crates/fj-core/src/validate/references.rs @@ -3,33 +3,27 @@ use std::hash::Hash; use crate::storage::Handle; -use super::ValidationError; - #[derive(Default)] -pub struct ReferenceCounter(HashMap, i32>); +pub struct ReferenceCounter(HashMap, Vec>>); -impl ReferenceCounter { +impl ReferenceCounter { pub fn new() -> Self { Self(HashMap::new()) } - pub fn add_count(&mut self, object: Handle) { + pub fn add_count(&mut self, object: Handle, found: Handle) { self.0 .entry(object) - .and_modify(|count| *count += 1) - .or_insert(1); + .and_modify(|references| references.push(found.clone())) + .or_insert(vec![found]); } pub fn has_multiple(&self) -> bool { - self.0.iter().any(|(_, count)| *count > 1) + self.0.iter().any(|(_, references)| references.len() > 1) } } -pub trait ValidateReferences { - fn validate(&self, errors: &mut Vec); -} - -/// Find errors and convert to [`ValidationError`] +/// Find errors and convert to [`crate::validate::ValidationError`] #[macro_export] macro_rules! validate_references { ($errors:ident, $error_ty:ty;$($counter:ident, $err:expr;)*) => { diff --git a/crates/fj-core/src/validate/sketch.rs b/crates/fj-core/src/validate/sketch.rs index 7c9be34aea..ca44803eaf 100644 --- a/crates/fj-core/src/validate/sketch.rs +++ b/crates/fj-core/src/validate/sketch.rs @@ -42,9 +42,9 @@ impl SketchValidationError { sketch.regions().iter().for_each(|r| { r.all_cycles().for_each(|c| { - referenced_cycles.add_count(c.clone()); + referenced_cycles.add_count(c.clone(), r.clone()); c.half_edges().into_iter().for_each(|e| { - referenced_edges.add_count(e.clone()); + referenced_edges.add_count(e.clone(), c.clone()); }) }) }); diff --git a/crates/fj-core/src/validate/solid.rs b/crates/fj-core/src/validate/solid.rs index 170c6f8c7d..93f130e7ca 100644 --- a/crates/fj-core/src/validate/solid.rs +++ b/crates/fj-core/src/validate/solid.rs @@ -151,12 +151,12 @@ impl SolidValidationError { solid.shells().iter().for_each(|s| { s.faces().into_iter().for_each(|f| { - referenced_faces.add_count(f.clone()); - referenced_regions.add_count(f.region().clone()); + referenced_faces.add_count(f.clone(), s.clone()); + referenced_regions.add_count(f.region().clone(), f.clone()); f.region().all_cycles().for_each(|c| { - referenced_cycles.add_count(c.clone()); + referenced_cycles.add_count(c.clone(), f.region().clone()); c.half_edges().into_iter().for_each(|e| { - referenced_edges.add_count(e.clone()); + referenced_edges.add_count(e.clone(), c.clone()); }) }) })