Skip to content

Commit

Permalink
simplify anyhow::ensure calls
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecarow committed May 21, 2024
1 parent 1ca3e36 commit f6957a1
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions fastsim-core/src/utils/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ impl Interpolator {
// Check that grid points are monotonically increasing
match self {
Self::Interp1D(interp) => {
anyhow::ensure!(
ensure!(
interp.x.windows(2).all(|w| w[0] < w[1]),
"Supplied x-values must be sorted and non-repeating"
);
}
Self::Interp2D(interp) => {
anyhow::ensure!(
ensure!(
interp.x.windows(2).all(|w| w[0] < w[1])
&& interp.y.windows(2).all(|w| w[0] < w[1]),
"Supplied values must be sorted and non-repeating"
);
}
Self::Interp3D(interp) => {
anyhow::ensure!(
ensure!(
interp.x.windows(2).all(|w| w[0] < w[1])
&& interp.y.windows(2).all(|w| w[0] < w[1])
&& interp.z.windows(2).all(|w| w[0] < w[1]),
Expand All @@ -97,7 +97,7 @@ impl Interpolator {
}
Self::InterpND(interp) => {
for i in 0..n {
anyhow::ensure!(
ensure!(
interp.grid[i].windows(2).all(|w| w[0] < w[1]),
"Supplied `grid` coordinates must be sorted and non-repeating: dimension {i}, {:?}",
interp.grid[i]
Expand All @@ -109,20 +109,20 @@ impl Interpolator {
// Check that grid and values are compatible shapes
match self {
Self::Interp1D(interp) => {
anyhow::ensure!(
ensure!(
interp.x.len() == interp.f_x.len(),
"Supplied grid and values are not compatible shapes"
);
}
Self::Interp2D(interp) => {
anyhow::ensure!(
ensure!(
interp.x.len() == interp.f_xy.len() && interp.y.len() == interp.f_xy[0].len(),
// TODO: protect against different length y-data
"Supplied grid and values are not compatible shapes"
);
}
Self::Interp3D(interp) => {
anyhow::ensure!(
ensure!(
interp.x.len() == interp.f_xyz.len()
&& interp.y.len() == interp.f_xyz[0].len()
&& interp.z.len() == interp.f_xyz[0][0].len(),
Expand All @@ -132,7 +132,7 @@ impl Interpolator {
}
Self::InterpND(interp) => {
for i in 0..n {
anyhow::ensure!(
ensure!(
interp.grid[i].len() == interp.values.shape()[i],
"Supplied grid and values are not compatible shapes: dimension {i}, lengths {} != {}",
interp.grid[i].len(),
Expand All @@ -145,14 +145,14 @@ impl Interpolator {
// Check that point is within grid in each dimension
match self {
Self::Interp1D(interp) => {
anyhow::ensure!(
ensure!(
interp.x[0] <= point[0] && point[0] <= *interp.x.last().unwrap(),
"Supplied point must be within grid: point = {point:?}, x = {:?}",
interp.x
);
}
Self::Interp2D(interp) => {
anyhow::ensure!(
ensure!(
(interp.x[0] <= point[0] && point[0] <= *interp.x.last().unwrap())
&& (interp.y[0] <= point[1] && point[1] <= *interp.y.last().unwrap()),
"Supplied point must be within grid: point = {point:?}, x = {:?}, y = {:?}",
Expand All @@ -161,7 +161,7 @@ impl Interpolator {
);
}
Self::Interp3D(interp) => {
anyhow::ensure!(
ensure!(
(interp.x[0] <= point[0] && point[0] <= *interp.x.last().unwrap())
&& (interp.y[0] <= point[1] && point[1] <= *interp.y.last().unwrap())
&& (interp.z[0] <= point[2] && point[2] <= *interp.z.last().unwrap()),
Expand All @@ -173,7 +173,7 @@ impl Interpolator {
}
Self::InterpND(interp) => {
for i in 0..n {
anyhow::ensure!(
ensure!(
interp.grid[i][0] <= point[i] && point[i] <= *interp.grid[i].last().unwrap(),
"Supplied point must be within grid for dimension {i}: point[{i}] = {:?}, grid[{i}] = {:?}",
point[i],
Expand All @@ -191,7 +191,7 @@ impl Interpolator {
} else {
interp.grid.len()
};
anyhow::ensure!(
ensure!(
grid_len == n,
"Length of supplied `grid` must be same as `values` dimensionality: {:?} is not {n}-dimensional",
interp.grid
Expand Down Expand Up @@ -451,7 +451,7 @@ impl InterpND {
let l = index_permutations[i].as_slice();
let u = index_permutations[next_idxs.len() + i].as_slice();
if dim == 0 {
anyhow::ensure!(
ensure!(
!interp_vals[l].is_nan() && !interp_vals[u].is_nan(),
"Surrounding value(s) cannot be NaN:\npoint = {point:?},\ngrid = {grid:?},\nvalues = {:?}",
self.values
Expand Down

0 comments on commit f6957a1

Please sign in to comment.