Skip to content

Commit

Permalink
feat: Accept durations as timeout arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nicos68 committed Dec 19, 2022
1 parent de6495c commit 1cbee4a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
16 changes: 14 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,18 @@ impl From<i32> for Timeout {
}
}

impl From<Duration> for Timeout {
fn from(duration: Duration) -> Timeout {
if duration.is_zero() {
Timeout::Never
} else if duration.as_millis() > u32::MAX.into() {
Timeout::Default
} else {
Timeout::Milliseconds(duration.as_millis().try_into().unwrap_or(u32::MAX))
}
}
}

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 1cbee4a

Please sign in to comment.