From 4dde60b9f9fe37cbf996ae0223f63734afa8b89c Mon Sep 17 00:00:00 2001 From: Andreas Hartmann Date: Mon, 16 May 2022 11:05:36 +0200 Subject: [PATCH] lib: Print better error for invalid baudrate The way the serial crate in use is written it will accept any arbitrary baudrate and try to configure the interface with it. This can fail if the underlying hardware doesn't support the given baudrate. Previously the code would simply output an `Error: Invalid argument`, which leaves the user guessing which part of their input was invalid. This patch adds an additional check on the error type returned by configuring the serial device. If it returns an `InputError`, we assume that in fact the baud rate was invalid (Because that is the only setting we change) and print a better error message to the user. --- espmonitor/src/lib.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/espmonitor/src/lib.rs b/espmonitor/src/lib.rs index 01b8fc5..503622b 100644 --- a/espmonitor/src/lib.rs +++ b/espmonitor/src/lib.rs @@ -124,8 +124,16 @@ fn run_child(args: AppArgs) -> Result<(), Box> { let mut dev = serial::open(&args.serial)?; dev.set_timeout(Duration::from_millis(200))?; - dev.reconfigure(&|settings| { - settings.set_baud_rate(speed) + + // The only thing we reconfigure and that could thus cause an error is the baud rate setting. + // Hence we can explicitly handle this case here and give the user a better idea of which part + // of their input was actually invalid. + dev.reconfigure(&|settings| settings.set_baud_rate(speed)).map_err(|err| { + if let serial::ErrorKind::InvalidInput = err.kind() { + format!("Baud rate {} not supported by hardware", speed.speed()) + } else { + format!("{}", err) + } })?; let bin_data = args.bin.as_ref().and_then(|bin_name| match fs::read(bin_name) {