Check for out-of-bounds timeout and correct it

This commit is contained in:
Lyndon Sanche 2022-01-26 12:05:22 -07:00
parent 3f6e9daf7d
commit 52109ab5f7
No known key found for this signature in database
GPG Key ID: 6F8E82F60C799B18

View File

@ -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,
}
);
}
}