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

Implement FromStr for Timeout #169

Merged
merged 2 commits into from Dec 13, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/timeout.rs
@@ -1,4 +1,15 @@
use std::{num::ParseIntError, str::FromStr};

/// Describes the timeout of a notification
///
/// # `FromStr`
/// You can also parse a `Timeout` from a `&str`.
/// ```
/// # use notify_rust::Timeout;
/// assert_eq!("default".parse(), Ok(Timeout::Default));
/// assert_eq!("never".parse(), Ok(Timeout::Never));
/// assert_eq!("42".parse(), Ok(Timeout::Milliseconds(42)));
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Timeout {
/// Expires according to server default.
Expand Down Expand Up @@ -55,6 +66,18 @@ impl From<Timeout> for i32 {
}
}

impl FromStr for Timeout {
type Err = ParseIntError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"default" => Ok(Timeout::Default),
"never" => Ok(Timeout::Never),
milliseconds => Ok(Timeout::Milliseconds(u32::from_str(milliseconds)?)),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This also allows to create Timeout::Milliseconds(0) by passing "0", but I don't think it is worth to handle the special case, since it is also possible to just construct a Timeout::Milliseconds(0) directly.

}
}
}

pub struct TimeoutMessage(Timeout);

impl From<Timeout> for TimeoutMessage {
Expand Down
8 changes: 4 additions & 4 deletions src/xdg/mod.rs
Expand Up @@ -302,23 +302,23 @@ pub(crate) fn show_notification(notification: &Notification) -> Result<Notificat
}
}

/// Get the currently dsed [`DbusStack`]
/// Get the currently used [`DbusStack`]
///
/// (zbus only)
#[cfg(all(feature = "zbus", not(feature = "dbus")))]
pub fn dbus_stack() -> Option<DbusStack> {
Some(DbusStack::Zbus)
}

/// Get the currently dsed [`DbusStack`]
/// Get the currently used [`DbusStack`]
///
/// (dbus-rs only)
#[cfg(all(feature = "dbus", not(feature = "zbus")))]
pub fn dbus_stack() -> Option<DbusStack> {
Some(DbusStack::Dbus)
}

/// Get the currently dsed [`DbusStack`]
/// Get the currently used [`DbusStack`]
///
/// both dbus-rs and zbus, switch via `$ZBUS_NOTIFICATION`
#[cfg(all(feature = "dbus", feature = "zbus"))]
Expand All @@ -330,7 +330,7 @@ pub fn dbus_stack() -> Option<DbusStack> {
})
}

/// Get the currently dsed [`DbusStack`]
/// Get the currently used [`DbusStack`]
///
/// neither zbus nor dbus-rs are configured
#[cfg(all(not(feature = "dbus"), not(feature = "zbus")))]
Expand Down