urdf_rs/
errors.rs

1use thiserror::Error;
2
3/// Alias for a `Result` with the error type `UrdfError`.
4pub type Result<T> = std::result::Result<T, UrdfError>;
5
6#[derive(Debug, Error)]
7#[error(transparent)]
8pub struct UrdfError(#[from] ErrorKind);
9
10// Hiding error variants from a library's public error type to prevent
11// dependency updates from becoming breaking changes.
12// We can add `UrdfErrorKind` enum or `is_*` methods that indicate the kind of
13// error if needed, but don't expose dependencies' types directly in the
14// public API.
15#[derive(Debug, Error)]
16pub(crate) enum ErrorKind {
17    #[error(transparent)]
18    File(#[from] std::io::Error),
19    #[error(transparent)]
20    Xml(#[from] serde_xml_rs::Error),
21    #[error(transparent)]
22    RustyXml(#[from] xml::BuilderError),
23    #[error(transparent)]
24    QuickXml(#[from] quick_xml::DeError),
25    #[error("command error {}\n--- stdout\n{}\n--- stderr\n{}", .msg, .stdout, .stderr)]
26    Command {
27        msg: String,
28        stdout: String,
29        stderr: String,
30    },
31    #[error("{}", .0)]
32    Other(String),
33}
34
35impl UrdfError {
36    pub(crate) fn new(err: impl Into<ErrorKind>) -> Self {
37        Self(err.into())
38    }
39}
40
41impl From<std::io::Error> for UrdfError {
42    fn from(err: std::io::Error) -> UrdfError {
43        ErrorKind::File(err).into()
44    }
45}
46
47impl From<&str> for UrdfError {
48    fn from(err: &str) -> UrdfError {
49        ErrorKind::Other(err.to_owned()).into()
50    }
51}
52
53impl From<String> for UrdfError {
54    fn from(err: String) -> UrdfError {
55        ErrorKind::Other(err).into()
56    }
57}
58
59impl From<std::string::FromUtf8Error> for UrdfError {
60    fn from(err: std::string::FromUtf8Error) -> UrdfError {
61        ErrorKind::Other(err.to_string()).into()
62    }
63}
64
65// Note: These implementations are intentionally not-exist to prevent dependency
66// updates from becoming breaking changes.
67// impl From<serde_xml_rs::Error> for UrdfError
68// impl From<xml::BuilderError> for UrdfError