Skip to content

Commit

Permalink
store both referenced and referencing objects for better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-folsom committed Jan 14, 2024
1 parent 6cbb078 commit 7d27b90
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
20 changes: 7 additions & 13 deletions crates/fj-core/src/validate/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,27 @@ use std::hash::Hash;

use crate::storage::Handle;

use super::ValidationError;

#[derive(Default)]
pub struct ReferenceCounter<T>(HashMap<Handle<T>, i32>);
pub struct ReferenceCounter<T, U>(HashMap<Handle<T>, Vec<Handle<U>>>);

impl<T: Eq + PartialEq + Hash> ReferenceCounter<T> {
impl<T: Eq + PartialEq + Hash, U> ReferenceCounter<T, U> {
pub fn new() -> Self {
Self(HashMap::new())
}

pub fn add_count(&mut self, object: Handle<T>) {
pub fn add_count(&mut self, object: Handle<T>, found: Handle<U>) {
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<T> {
fn validate(&self, errors: &mut Vec<ValidationError>);
}

/// 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;)*) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/validate/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
})
})
});
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 @@ -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());
})
})
})
Expand Down

0 comments on commit 7d27b90

Please sign in to comment.