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, stabilizer.cycle_counter,
env!("CARGO_BIN_NAME"), env!("CARGO_BIN_NAME"),
stabilizer.net.mac_address, 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 let generator = network

View File

@ -241,7 +241,7 @@ const APP: () = {
stabilizer.cycle_counter, stabilizer.cycle_counter,
env!("CARGO_BIN_NAME"), env!("CARGO_BIN_NAME"),
stabilizer.net.mac_address, 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( let generator = network.configure_streaming(

View File

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