From 52109ab5f7c336b55c81bccafb3adbfc81514553 Mon Sep 17 00:00:00 2001 From: Lyndon Sanche Date: Wed, 26 Jan 2022 12:05:22 -0700 Subject: [PATCH] Check for out-of-bounds timeout and correct it --- src/modules/cmd_duration.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/modules/cmd_duration.rs b/src/modules/cmd_duration.rs index 6811803d2..c495e3c57 100644 --- a/src/modules/cmd_duration.rs +++ b/src/modules/cmd_duration.rs @@ -79,12 +79,24 @@ fn undistract_me<'a, 'b>( unstyle(&ANSIStrings(&module.ansi_strings())) ); + let timeout: u32 = match u32::try_from(config.notification_timeout) { + Ok(v) => v, + Err(_) => { + let v: u32 = CmdDurationConfig::default().notification_timeout as u32; + log::trace!( + "Configured notification timeout not within bounds. Defaulting to {} ms", + v + ); + v + } + }; + let mut notification = Notification::new(); notification .summary("Command finished") .body(&body) .icon("utilities-terminal") - .timeout(Timeout::Milliseconds(config.notification_timeout as u32)); + .timeout(Timeout::Milliseconds(timeout)); if let Err(err) = notification.show() { log::trace!("Cannot show notification: {}", err); @@ -96,6 +108,7 @@ fn undistract_me<'a, 'b>( #[cfg(test)] mod tests { + use crate::configs::cmd_duration::CmdDurationConfig; use crate::test::ModuleRenderer; use ansi_term::Color; @@ -174,4 +187,14 @@ mod tests { let expected = Some(format!("underwent {} ", Color::Yellow.bold().paint("5s"))); assert_eq!(expected, actual); } + + #[test] + fn config_notification_timeout_within_bounds() { + assert!( + match u32::try_from(CmdDurationConfig::default().notification_timeout) { + Ok(_) => true, + Err(_) => false, + } + ); + } }