broker parsing: just unwrap_or

This commit is contained in:
Robert Jördens 2021-08-03 07:23:56 +00:00
parent 8cc7d6b27e
commit 7902b3cb7d
3 changed files with 11 additions and 23 deletions

View File

@ -200,7 +200,7 @@ const APP: () = {
stabilizer.cycle_counter,
env!("CARGO_BIN_NAME"),
stabilizer.net.mac_address,
net::parse_or_default_broker(option_env!("BROKER")),
net::parse_ipv4(option_env!("BROKER").unwrap_or("10.34.16.10")),
);
let generator = network

View File

@ -241,7 +241,7 @@ const APP: () = {
stabilizer.cycle_counter,
env!("CARGO_BIN_NAME"),
stabilizer.net.mac_address,
net::parse_or_default_broker(option_env!("BROKER")),
net::parse_ipv4(option_env!("BROKER").unwrap_or("10.34.16.10")),
);
let generator = network.configure_streaming(

View File

@ -209,29 +209,17 @@ pub fn get_device_prefix(
prefix
}
/// Determine the broker IP address
///
/// # Note
/// If the broker IP is unspecified or unparseable, the default IP is returned.
/// Parse a string containing an IPv4 address.
/// This is a convenience wrapper around the `no_std_net` parser.
///
/// # Args
/// * `input` - The optionally-specified command-line broker IP address as a string.
/// * `input` - The IP address as a string.
///
/// # Returns
/// The broker IP address.
pub fn parse_or_default_broker(input: Option<&str>) -> IpAddr {
input.and_then(|data| {
data.parse::<minimq::embedded_nal::IpAddr>().map_or_else(
|err| {
log::error!(
"{:?}: Failed to parse broker IP ({:?}) - Falling back to default",
err,
data
);
None
},
Some,
)
})
.unwrap_or_else(|| DEFAULT_MQTT_BROKER.into())
/// The IP address.
///
/// # Panic
/// This panics if parsing fails.
pub fn parse_ipv4(input: &str) -> IpAddr {
input.parse::<minimq::embedded_nal::IpAddr>().unwrap()
}