Skip to main content

nabled_model/
lib.rs

1//! Robot model representation.
2
3#![allow(clippy::missing_errors_doc, let_underscore_drop, clippy::missing_panics_doc)]
4
5use nabled_core::errors::{IntoNabledError, NabledError, ShapeError};
6
7pub mod dh;
8pub mod fixture;
9pub mod joint;
10pub mod link;
11pub mod origin;
12pub mod robot;
13pub mod tree_model;
14pub mod urdf;
15
16#[derive(Debug, Clone, PartialEq)]
17pub enum ModelError {
18    EmptyModel,
19    DimensionMismatch,
20    InvalidInput(String),
21    ParseError(String),
22}
23
24impl std::fmt::Display for ModelError {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            ModelError::EmptyModel => write!(f, "robot model cannot be empty"),
28            ModelError::DimensionMismatch => write!(f, "input dimensions are incompatible"),
29            ModelError::InvalidInput(message) => write!(f, "invalid input: {message}"),
30            ModelError::ParseError(message) => write!(f, "parse error: {message}"),
31        }
32    }
33}
34
35impl std::error::Error for ModelError {}
36
37impl IntoNabledError for ModelError {
38    fn into_nabled_error(self) -> NabledError {
39        match self {
40            ModelError::EmptyModel => NabledError::Shape(ShapeError::EmptyInput),
41            ModelError::DimensionMismatch => NabledError::Shape(ShapeError::DimensionMismatch),
42            ModelError::InvalidInput(message) | ModelError::ParseError(message) => {
43                NabledError::InvalidInput(message)
44            }
45        }
46    }
47}
48
49#[cfg(test)]
50mod error_mapping {
51    use nabled_core::errors::{IntoNabledError, NabledError, ShapeError};
52
53    use crate::ModelError;
54
55    #[test]
56    fn into_nabled_error_covers_all_variants() {
57        assert!(matches!(
58            ModelError::EmptyModel.into_nabled_error(),
59            NabledError::Shape(ShapeError::EmptyInput)
60        ));
61        assert!(matches!(
62            ModelError::DimensionMismatch.into_nabled_error(),
63            NabledError::Shape(ShapeError::DimensionMismatch)
64        ));
65        assert!(matches!(
66            ModelError::InvalidInput("bad".into()).into_nabled_error(),
67            NabledError::InvalidInput(message) if message == "bad"
68        ));
69        assert!(matches!(
70            ModelError::ParseError("xml".into()).into_nabled_error(),
71            NabledError::InvalidInput(message) if message == "xml"
72        ));
73    }
74}