Skip to content

Commit

Permalink
Add std::error::Error impls to Error enums of parameter module (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
alluring-mushroom authored Oct 9, 2024
1 parent ad9667f commit 06ee9bf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
33 changes: 33 additions & 0 deletions rclrs/src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,18 @@ pub enum ParameterValueError {
ReadOnly,
}

impl std::fmt::Display for ParameterValueError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ParameterValueError::OutOfRange => write!(f, "parameter value was out of the parameter's range"),
ParameterValueError::TypeMismatch => write!(f, "parameter was stored in a static type and an operation on a different type was attempted"),
ParameterValueError::ReadOnly => write!(f, "a write on a read-only parameter was attempted"),
}
}
}

impl std::error::Error for ParameterValueError {}

/// Error that can be generated when doing operations on parameters.
#[derive(Debug)]
pub enum DeclarationError {
Expand All @@ -644,6 +656,27 @@ pub enum DeclarationError {
InvalidRange,
}

impl std::fmt::Display for DeclarationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DeclarationError::AlreadyDeclared => write!(f, "parameter was already declared, but a new declaration was attempted"),
DeclarationError::NoValueAvailable => {
write!(f, "parameter was declared as non-optional but no value was available, either through a user specified default, a command-line override, or a previously set value")
},
DeclarationError::OverrideValueTypeMismatch => {
write!(f, "the override value that was provided has the wrong type")
},
DeclarationError::PriorValueTypeMismatch => {
write!(f, "the value that the parameter was already set to has the wrong type")
},
DeclarationError::InitialValueOutOfRange => write!(f, "the initial value that was selected is out of range"),
DeclarationError::InvalidRange => write!(f, "an invalid range was provided to a parameter declaration (i.e. lower bound > higher bound)"),
}
}
}

impl std::error::Error for DeclarationError {}

impl<'a> Parameters<'a> {
/// Tries to read a parameter of the requested type.
///
Expand Down
13 changes: 13 additions & 0 deletions rclrs/src/parameter/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,24 @@ impl From<ParameterValue> for RmwParameterValue {

/// An error that occured when trying to convert a parameter from an
/// `rcl_interfaces::msg::ParameterValue`
#[derive(Debug)]
pub enum RmwParameterConversionError {
/// The parameter type was not valid.
InvalidParameterType,
}

impl std::fmt::Display for RmwParameterConversionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RmwParameterConversionError::InvalidParameterType => {
write!(f, "the parameter type was not valid")
}
}
}
}

impl std::error::Error for RmwParameterConversionError {}

impl TryFrom<RmwParameterValue> for ParameterValue {
type Error = RmwParameterConversionError;

Expand Down

0 comments on commit 06ee9bf

Please sign in to comment.