diff --git a/r2r/src/error.rs b/r2r/src/error.rs index 5ad59ac0c..389653777 100644 --- a/r2r/src/error.rs +++ b/r2r/src/error.rs @@ -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 { diff --git a/r2r/src/nodes.rs b/r2r/src/nodes.rs index 7610535fd..cde555bf5 100644 --- a/r2r/src/nodes.rs +++ b/r2r/src/nodes.rs @@ -541,6 +541,30 @@ impl Node { future::ready(()) } + /// Fetch a single ROS parameter. + pub fn get_parameter(&self, name: &str) -> Result + where + ParameterValue: TryInto, + { + 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. diff --git a/r2r/src/parameters.rs b/r2r/src/parameters.rs index fa9cb0484..8c56b06c7 100644 --- a/r2r/src/parameters.rs +++ b/r2r/src/parameters.rs @@ -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 {}