From d966d2a9355229dfe7ac1eff4c8218ac2c6bb365 Mon Sep 17 00:00:00 2001 From: James Carl Date: Sat, 12 Oct 2024 17:47:57 -0400 Subject: [PATCH] Add example for get_parameter. --- r2r/examples/parameters.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/r2r/examples/parameters.rs b/r2r/examples/parameters.rs index 1af41bda5..9f098bda5 100644 --- a/r2r/examples/parameters.rs +++ b/r2r/examples/parameters.rs @@ -18,6 +18,24 @@ fn main() -> Result<(), Box> { let ctx = r2r::Context::create()?; let mut node = r2r::Node::create(ctx, "to_be_replaced", "to_be_replaced")?; + // if you only need to load a parameter once at startup, it can be done like this. + // errors can be propigated with the ? operator and enhanced with the `thiserror` and `anyhow` crates. + // we do not use the ? operator here because we want the program to continue, even if the value is not set. + let serial_interface_path = node.get_parameter::("serial_interface"); + match serial_interface_path { + Ok(serial_interface) => println!("Serial interface: {serial_interface}"), + Err(error) => println!("Failed to get name of serial interface: {error}"), + } + + // tou can also get parameters are optional types. + // this will be None if the parameter is not set. If the parameter is set but to the wrong type, this will + // will produce an error. + let baud_rate: Option = node.get_parameter("baud_rate")?; + + // because the baud_rate is an optional type, we can use `unwrap_or` to provide a default value. + let baud_rate = baud_rate.unwrap_or(115200); + println!("Baud rate: {baud_rate}"); + // make a parameter handler (once per node). // the parameter handler is optional, only spawn one if you need it. let (paramater_handler, parameter_events) = node.make_parameter_handler()?;