Skip to main content

nabled_dynamics/
lib.rs

1//! Robot dynamics.
2
3#![allow(clippy::missing_errors_doc, clippy::too_many_arguments, clippy::similar_names)]
4
5use nabled_core::errors::{IntoNabledError, NabledError, ShapeError};
6
7pub mod config;
8pub mod crba;
9pub mod fd;
10pub mod id;
11pub mod rnea;
12pub mod spatial;
13pub mod tree;
14
15pub use config::{DynamicsConfig, ForwardDynamicsMethod};
16
17#[derive(Debug, Clone, PartialEq)]
18pub enum DynamicsError {
19    EmptyModel,
20    DimensionMismatch,
21    InvalidInput(String),
22    NotImplemented,
23}
24
25impl std::fmt::Display for DynamicsError {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        match self {
28            DynamicsError::EmptyModel => write!(f, "dynamics model cannot be empty"),
29            DynamicsError::DimensionMismatch => write!(f, "input dimensions are incompatible"),
30            DynamicsError::InvalidInput(message) => write!(f, "invalid input: {message}"),
31            DynamicsError::NotImplemented => write!(f, "dynamics routine not yet implemented"),
32        }
33    }
34}
35
36impl std::error::Error for DynamicsError {}
37
38impl IntoNabledError for DynamicsError {
39    fn into_nabled_error(self) -> NabledError {
40        match self {
41            DynamicsError::EmptyModel => NabledError::Shape(ShapeError::EmptyInput),
42            DynamicsError::DimensionMismatch => NabledError::Shape(ShapeError::DimensionMismatch),
43            DynamicsError::InvalidInput(message) => NabledError::InvalidInput(message),
44            DynamicsError::NotImplemented => {
45                NabledError::Other("dynamics routine not implemented".to_string())
46            }
47        }
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use nabled_core::errors::{IntoNabledError, NabledError, ShapeError};
54
55    use super::*;
56
57    #[test]
58    fn dynamics_errors_display_and_map_to_shared_taxonomy() {
59        assert_eq!(DynamicsError::EmptyModel.to_string(), "dynamics model cannot be empty");
60        assert_eq!(
61            DynamicsError::DimensionMismatch.to_string(),
62            "input dimensions are incompatible"
63        );
64        assert_eq!(
65            DynamicsError::InvalidInput("bad q".to_string()).to_string(),
66            "invalid input: bad q"
67        );
68        assert_eq!(
69            DynamicsError::NotImplemented.to_string(),
70            "dynamics routine not yet implemented"
71        );
72
73        assert!(matches!(
74            DynamicsError::EmptyModel.into_nabled_error(),
75            NabledError::Shape(ShapeError::EmptyInput)
76        ));
77        assert!(matches!(
78            DynamicsError::DimensionMismatch.into_nabled_error(),
79            NabledError::Shape(ShapeError::DimensionMismatch)
80        ));
81        assert!(matches!(
82            DynamicsError::InvalidInput("x".to_string()).into_nabled_error(),
83            NabledError::InvalidInput(_)
84        ));
85        assert!(matches!(DynamicsError::NotImplemented.into_nabled_error(), NabledError::Other(_)));
86    }
87}