Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Accept durations as timeout arguments #172

Merged
merged 2 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/timeout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{num::ParseIntError, str::FromStr};
use std::{convert::TryInto, num::ParseIntError, str::FromStr, time::Duration};

/// 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good idea to convert Duration 0 to Never just because the 0 value means never.
If you handover a parameter of type Duration then this behavior would be very unexpected.
What if we convert a timeout of Duration of 0ms into a None timeout.

Copy link
Contributor Author

@nicos68 nicos68 Jan 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we have to return a Timeout object, we can't return Option<Timeout> (or at least I don't see how). Then either we take the user at their word and return Milliseconds(0) or we try to be smarter then them and return Default. What do you think ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes, you're right

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's just weird API. then let's leave it like this. Thanks for the change, I'll just add a bit more documentation to make sure this is explained to the user.

} 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
Original file line number Diff line number Diff line change
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