Change conditional panic to expect
All checks were successful
/ test (pull_request) Successful in 2m21s

Use `expect` to communicate that every element should have a column
index when `pose` is called. Rewrite the panic messages in the style
recommended by the `expect` documentation.
This commit is contained in:
Aaron Fenyes 2025-04-21 14:47:26 -07:00
parent 99a9c3ec55
commit 5eeb0935ca

View file

@ -134,12 +134,11 @@ impl Element {
impl ProblemPoser for Element {
fn pose(&self, problem: &mut ConstraintProblem, _elts: &Slab<Element>) {
if let Some(index) = self.column_index {
problem.gram.push_sym(index, index, 1.0);
problem.guess.set_column(index, &self.representation.get_clone_untracked());
} else {
panic!("Tried to write problem data from an unindexed element: \"{}\"", self.id);
}
let index = self.column_index.expect(
format!("Element \"{}\" should be indexed before writing problem data", self.id).as_str()
);
problem.gram.push_sym(index, index, 1.0);
problem.guess.set_column(index, &self.representation.get_clone_untracked());
}
}
@ -202,14 +201,12 @@ impl ProblemPoser for InversiveDistanceRegulator {
fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab<Element>) {
self.set_point.with_untracked(|set_pt| {
if let Some(val) = set_pt.value {
let subject_column_indices = self.subjects.map(
|subj| elts[subj].column_index
let [row, col] = self.subjects.map(
|subj| elts[subj].column_index.expect(
"Subjects should be indexed before inversive distance regulator writes problem data"
)
);
if let [Some(row), Some(col)] = subject_column_indices {
problem.gram.push_sym(row, col, val);
} else {
panic!("Tried to write problem data from a regulator with an unindexed subject");
}
problem.gram.push_sym(row, col, val);
}
});
}
@ -268,11 +265,10 @@ impl ProblemPoser for HalfCurvatureRegulator {
fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab<Element>) {
self.set_point.with_untracked(|set_pt| {
if let Some(val) = set_pt.value {
if let Some(col) = elts[self.subject].column_index {
problem.frozen.push(Element::CURVATURE_COMPONENT, col, val);
} else {
panic!("Tried to write problem data from a regulator with an unindexed subject");
}
let col = elts[self.subject].column_index.expect(
"Subject should be indexed before half-curvature regulator writes problem data"
);
problem.frozen.push(Element::CURVATURE_COMPONENT, col, val);
}
});
}
@ -611,7 +607,7 @@ mod tests {
use super::*;
#[test]
#[should_panic(expected = "Tried to write problem data from an unindexed element: \"sphere\"")]
#[should_panic(expected = "Element \"sphere\" should be indexed before writing problem data")]
fn unindexed_element_test() {
let _ = create_root(|| {
Element::new(
@ -624,8 +620,8 @@ mod tests {
}
#[test]
#[should_panic(expected = "Tried to write problem data from a regulator with an unindexed subject")]
fn unindexed_subject_test() {
#[should_panic(expected = "Subjects should be indexed before inversive distance regulator writes problem data")]
fn unindexed_subject_test_inversive_distance() {
let _ = create_root(|| {
let mut elts = Slab::new();
let subjects = [0, 1].map(|k| {