use std::{error::Error, fmt};
#[derive(Debug)]
pub enum DynamicMessageError {
RequiredPrefixNotSourced {
package: String,
},
InvalidMessageTypeSyntax {
input: String,
},
InvalidMessageType,
MessageTypeMismatch,
LibraryLoadingError(libloading::Error),
}
impl fmt::Display for DynamicMessageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RequiredPrefixNotSourced { package } => {
write!(f, "Package '{}' was not found in any prefix", package)
}
Self::InvalidMessageTypeSyntax { input } => write!(
f,
"The message type '{}' does not have the form <package>/msg/<msg_name>",
input
),
Self::InvalidMessageType => write!(f, "The message type was not found in the package"),
Self::MessageTypeMismatch => write!(
f,
"The operation expected a dynamic message of a different type"
),
Self::LibraryLoadingError(_) => write!(f, "Loading the type support library failed"),
}
}
}
impl Error for DynamicMessageError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
DynamicMessageError::LibraryLoadingError(lle) => Some(lle).map(|e| e as &dyn Error),
_ => None,
}
}
}
impl PartialEq for DynamicMessageError {
fn eq(&self, other: &Self) -> bool {
if std::mem::discriminant(self) != std::mem::discriminant(other) {
return false;
}
return self.to_string() == other.to_string();
}
}
impl Eq for DynamicMessageError {}