Skip to main content

dynamics_parse/
errors.rs

1//! Define error types for parsing URDF files.
2
3use dynamics_model::model::ModelError;
4
5use std::{fmt::Display, io};
6
7#[derive(Debug)]
8/// Error types that can occur while parsing an URDF file.
9pub enum ParseError {
10    /// IO error occurred while reading the file.
11    IoError(io::Error, String),
12    /// Error occurred while parsing XML.
13    XmlError(roxmltree::Error),
14    /// The URDF file does not contain a \<robot\> tag.
15    NoRobotTag,
16    /// A \<visual\> tag is present without a corresponding \<geometry\> tag.
17    VisualWithoutGeometry(String),
18    /// A \<geometry\> tag is present without a corresponding shape tag.
19    GeometryWithoutShape(String),
20    /// The given required parameter is missing in the URDF.
21    MissingParameter(String),
22    /// The given parameter has an invalid value.
23    InvalidParameter(String),
24    /// A joint, link, or material is missing a name attribute.
25    NameMissing(String),
26    /// A material is defined without a color.
27    MaterialWithoutColor(String),
28    /// An unknown joint type was encountered.
29    UnknownJointType(String),
30    /// An unknown tag was encountered in the URDF.
31    UnknownTag(String),
32    /// An error occurred while building the model
33    ModelError(ModelError),
34    /// A link is referenced that does not exist in the model.
35    UnknownLinkName(String),
36    /// The file path provided for a mesh is invalid.
37    InvalidFilePath(String),
38    /// An inertial tag is present without inertia data.
39    InertialWithoutInertia(String),
40    /// An inertial tag is present without mass data.
41    InertialWithoutMass(String),
42    /// A frame references a parent that does not exist.
43    UnknownParent(String),
44}
45
46impl Display for ParseError {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        match self {
49            ParseError::IoError(e, path) => write!(f, "IO error: {e} (file: {path})"),
50            ParseError::XmlError(e) => write!(f, "XML parsing error: {e}"),
51            ParseError::NoRobotTag => write!(f, "No <robot> tag found in URDF."),
52            ParseError::VisualWithoutGeometry(visual) => {
53                write!(
54                    f,
55                    "the <visual> tag named '{visual}' does not have a corresponding <geometry> tag."
56                )
57            }
58            ParseError::GeometryWithoutShape(geometry) => {
59                write!(
60                    f,
61                    "the <geometry> tag named '{geometry}' does not have a corresponding shape tag."
62                )
63            }
64            ParseError::MissingParameter(param) => {
65                write!(f, "Missing required parameter: {param}")
66            }
67            ParseError::InvalidParameter(param) => {
68                write!(f, "Invalid value for parameter: {param}")
69            }
70            ParseError::NameMissing(entity) => write!(f, "Missing name attribute for {entity}."),
71            ParseError::MaterialWithoutColor(material) => {
72                write!(
73                    f,
74                    "the <material> tag named '{material}' does not have a corresponding <color> tag."
75                )
76            }
77            ParseError::UnknownJointType(joint_type) => {
78                write!(f, "unknown joint type ({joint_type}).")
79            }
80            ParseError::UnknownTag(tag) => write!(f, "Unknown tag encountered: {tag}"),
81            ParseError::ModelError(e) => write!(f, "Model error: {e}"),
82            ParseError::UnknownLinkName(name) => write!(f, "Unknown link name: {name}"),
83            ParseError::InvalidFilePath(path) => write!(f, "Invalid file path: {path}"),
84            ParseError::InertialWithoutInertia(link) => {
85                write!(f, "<inertial> tag in link '{link}' missing inertia data.")
86            }
87            ParseError::InertialWithoutMass(link) => {
88                write!(f, "<inertial> tag in link '{link}' missing mass data.")
89            }
90            ParseError::UnknownParent(parent) => {
91                write!(f, "Frame references unknown parent: {parent}")
92            }
93        }
94    }
95}