Skip to content

Commit 0ffa1bf

Browse files
authored
chore(lambda-rs): Update math API to return errors instead of panicking
2 parents d0abc73 + a4cfb85 commit 0ffa1bf

File tree

9 files changed

+271
-123
lines changed

9 files changed

+271
-123
lines changed

crates/lambda-rs/examples/reflective_room.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ impl Component<ComponentResult, String> for ReflectiveRoomExample {
317317
lambda::math::matrix::identity_matrix(4, 4),
318318
[1.0, 0.0, 0.0],
319319
-self.camera_pitch_turns,
320-
);
320+
)
321+
.expect("rotation axis must be a unit axis vector");
321322
let view = rot_x.multiply(&compute_view_matrix(camera.position));
322323
let projection = compute_perspective_projection(
323324
camera.field_of_view_in_turns,
@@ -360,7 +361,8 @@ impl Component<ComponentResult, String> for ReflectiveRoomExample {
360361
model_floor,
361362
[1.0, 0.0, 0.0],
362363
self.floor_tilt_turns,
363-
);
364+
)
365+
.expect("rotation axis must be a unit axis vector");
364366
let mvp_floor = projection.multiply(&view).multiply(&model_floor);
365367

366368
let viewport = ViewportBuilder::new().build(self.width, self.height);

crates/lambda-rs/examples/textured_cube.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,14 @@ impl Component<ComponentResult, String> for TexturedCubeExample {
411411
model,
412412
[0.0, 1.0, 0.0],
413413
angle_y_turns,
414-
);
414+
)
415+
.expect("rotation axis must be a unit axis vector");
415416
model = lambda::math::matrix::rotate_matrix(
416417
model,
417418
[1.0, 0.0, 0.0],
418419
angle_x_turns,
419-
);
420+
)
421+
.expect("rotation axis must be a unit axis vector");
420422

421423
let view = compute_view_matrix(camera.position);
422424
let projection = compute_perspective_projection(

crates/lambda-rs/src/math/error.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//! Math error types returned from fallible operations.
2+
3+
use std::fmt;
4+
5+
/// Errors returned by fallible math operations in `lambda-rs`.
6+
#[derive(Debug, Clone, PartialEq)]
7+
pub enum MathError {
8+
/// Cross product requires exactly 3 dimensions.
9+
CrossProductDimension { actual: usize },
10+
/// Cross product requires both vectors to have the same dimension.
11+
MismatchedVectorDimensions { left: usize, right: usize },
12+
/// Rotation axis must be a unit axis vector (one of `[1,0,0]`, `[0,1,0]`,
13+
/// `[0,0,1]`). A zero axis (`[0,0,0]`) is treated as "no rotation".
14+
InvalidRotationAxis { axis: [f32; 3] },
15+
/// Rotation requires a 4x4 matrix.
16+
InvalidRotationMatrixSize { rows: usize, cols: usize },
17+
/// Determinant requires a square matrix.
18+
NonSquareMatrix { rows: usize, cols: usize },
19+
/// Determinant cannot be computed for an empty matrix.
20+
EmptyMatrix,
21+
/// Cannot normalize a zero-length vector.
22+
ZeroLengthVector,
23+
}
24+
25+
impl fmt::Display for MathError {
26+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27+
match self {
28+
MathError::CrossProductDimension { actual } => {
29+
return write!(f, "Cross product requires 3D vectors, got {}D", actual);
30+
}
31+
MathError::MismatchedVectorDimensions { left, right } => {
32+
return write!(
33+
f,
34+
"Vectors must have matching dimensions (left {}D, right {}D)",
35+
left, right
36+
);
37+
}
38+
MathError::InvalidRotationAxis { axis } => {
39+
return write!(f, "Rotation axis {:?} is not a unit axis vector", axis);
40+
}
41+
MathError::InvalidRotationMatrixSize { rows, cols } => {
42+
return write!(
43+
f,
44+
"Rotation requires a 4x4 matrix, got {}x{}",
45+
rows, cols
46+
);
47+
}
48+
MathError::NonSquareMatrix { rows, cols } => {
49+
return write!(
50+
f,
51+
"Determinant requires square matrix, got {}x{}",
52+
rows, cols
53+
);
54+
}
55+
MathError::EmptyMatrix => {
56+
return write!(f, "Determinant requires a non-empty matrix");
57+
}
58+
MathError::ZeroLengthVector => {
59+
return write!(f, "Cannot normalize a zero-length vector");
60+
}
61+
}
62+
}
63+
}
64+
65+
impl std::error::Error for MathError {}

0 commit comments

Comments
 (0)