Skip to content

Commit

Permalink
Accept durations as timeout arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nicos68 committed Dec 18, 2022
1 parent de6495c commit 7bbbe64
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/timeout.rs
@@ -1,7 +1,7 @@
use std::{num::ParseIntError, str::FromStr};
use std::{convert::TryInto, num::ParseIntError, str::FromStr, time::Duration, u128};

/// Describes the timeout of a notification
///
///
/// # `FromStr`
/// You can also parse a `Timeout` from a `&str`.
/// ```
Expand Down Expand Up @@ -56,6 +56,21 @@ impl From<i32> for Timeout {
}
}

impl From<Duration> for Timeout {
fn from(duration: Duration) -> Timeout {
if duration.is_zero() {
Timeout::Never
} else {
match duration.as_millis().cmp(&(u32::MAX as u128)) {
std::cmp::Ordering::Less => {
Timeout::Milliseconds(duration.as_millis().try_into().unwrap_or(u32::MAX))
}
_ => Timeout::Default,
}
}
}
}

impl From<Timeout> for i32 {
fn from(timeout: Timeout) -> Self {
match timeout {
Expand Down
12 changes: 12 additions & 0 deletions tests/realworld.rs
Expand Up @@ -4,6 +4,8 @@ extern crate notify_rust;
#[cfg(test)]
mod realworld {

use std::time::Duration;

#[cfg(all(feature = "images", unix, not(target_os = "macos")))]
use notify_rust::Image;
use notify_rust::*;
Expand Down Expand Up @@ -112,6 +114,16 @@ mod realworld {
.unwrap();
}

#[test]
fn should_allow_timeout_with_duration() {
let mut notification = Notification::new();
notification.timeout(Duration::from_secs(15));
assert!(matches!(
notification.timeout,
Timeout::Milliseconds(15_000)
));
}

#[test]
#[cfg(all(unix, not(target_os = "macos")))]
fn urgency() {
Expand Down

0 comments on commit 7bbbe64

Please sign in to comment.