Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add std::error::Error impls to Error enums of parameter module #413

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading