Skip to main content

nabled_sensor/
lib.rs

1//! Sensor fusion and estimation.
2
3#![allow(clippy::missing_errors_doc, clippy::many_single_char_names)]
4
5use nabled_core::errors::{IntoNabledError, NabledError, ShapeError};
6
7pub mod camera;
8pub mod ekf;
9pub mod imu;
10pub mod kalman;
11
12#[derive(Debug, Clone, PartialEq)]
13pub enum SensorError {
14    EmptyInput,
15    DimensionMismatch,
16    InvalidInput(String),
17    NumericalInstability,
18}
19
20impl std::fmt::Display for SensorError {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            SensorError::EmptyInput => write!(f, "input cannot be empty"),
24            SensorError::DimensionMismatch => write!(f, "input dimensions are incompatible"),
25            SensorError::InvalidInput(message) => write!(f, "invalid input: {message}"),
26            SensorError::NumericalInstability => write!(f, "numerical instability detected"),
27        }
28    }
29}
30
31impl std::error::Error for SensorError {}
32
33impl IntoNabledError for SensorError {
34    fn into_nabled_error(self) -> NabledError {
35        match self {
36            SensorError::EmptyInput => NabledError::Shape(ShapeError::EmptyInput),
37            SensorError::DimensionMismatch => NabledError::Shape(ShapeError::DimensionMismatch),
38            SensorError::InvalidInput(message) => NabledError::InvalidInput(message),
39            SensorError::NumericalInstability => NabledError::NumericalInstability,
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use nabled_core::errors::{IntoNabledError, NabledError, ShapeError};
47
48    use super::*;
49
50    #[test]
51    fn sensor_errors_display_and_map_to_shared_taxonomy() {
52        assert_eq!(SensorError::EmptyInput.to_string(), "input cannot be empty");
53        assert_eq!(SensorError::DimensionMismatch.to_string(), "input dimensions are incompatible");
54        assert_eq!(
55            SensorError::InvalidInput("bad z".to_string()).to_string(),
56            "invalid input: bad z"
57        );
58        assert_eq!(SensorError::NumericalInstability.to_string(), "numerical instability detected");
59
60        assert!(matches!(
61            SensorError::EmptyInput.into_nabled_error(),
62            NabledError::Shape(ShapeError::EmptyInput)
63        ));
64        assert!(matches!(
65            SensorError::DimensionMismatch.into_nabled_error(),
66            NabledError::Shape(ShapeError::DimensionMismatch)
67        ));
68        assert!(matches!(
69            SensorError::InvalidInput("x".to_string()).into_nabled_error(),
70            NabledError::InvalidInput(_)
71        ));
72        assert!(matches!(
73            SensorError::NumericalInstability.into_nabled_error(),
74            NabledError::NumericalInstability
75        ));
76    }
77}