Skip to content

Commit

Permalink
Add get_parameter method.
Browse files Browse the repository at this point in the history
  • Loading branch information
IamTheCarl committed Oct 9, 2024
1 parent ba21613 commit a7093a8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
12 changes: 12 additions & 0 deletions r2r/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ pub enum Error {

#[error("Parameter {name} conversion failed: {msg}")]
ParameterValueConv { name: String, msg: String },

#[error("Expected parameter {name} is not set")]
ParameterNotSet { name: String },

#[error(
"Parameter {name} was expected to be of type {expected_type} but was of type {actual_type}"
)]
ParameterWrongType {
name: String,
expected_type: &'static str,
actual_type: &'static str,
},
}

impl Error {
Expand Down
24 changes: 24 additions & 0 deletions r2r/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,30 @@ impl Node {
future::ready(())
}

/// Fetch a single ROS parameter.
pub fn get_parameter<T>(&self, name: &str) -> Result<T>
where
ParameterValue: TryInto<T, Error = WrongParameterType>,
{
let params = self.params.lock().unwrap();
let parameter = params.get(name).ok_or(Error::ParameterNotSet {
name: name.to_string(),
})?;

let value: T =
parameter
.value
.clone()
.try_into()
.map_err(|error: WrongParameterType| Error::ParameterWrongType {
name: name.to_string(),
expected_type: error.expected_type_name,
actual_type: error.actual_type_name,
})?;

Ok(value)
}

/// Subscribe to a ROS topic.
///
/// This function returns a `Stream` of ros messages.
Expand Down
4 changes: 2 additions & 2 deletions r2r/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub enum ParameterValue {

#[derive(Debug)]
pub struct WrongParameterType {
expected_type_name: &'static str,
actual_type_name: &'static str,
pub expected_type_name: &'static str,
pub actual_type_name: &'static str,
}

impl std::error::Error for WrongParameterType {}
Expand Down

0 comments on commit a7093a8

Please sign in to comment.