From b87b859e6d18481e40d90ae7b4cd1b885589b897 Mon Sep 17 00:00:00 2001 From: Raphael Nestler Date: Sat, 10 Dec 2022 15:45:09 +0100 Subject: [PATCH 1/2] feat: implement FromStr for Timeout Useful when wanting to pass it as a command line argument to an application. --- src/timeout.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/timeout.rs b/src/timeout.rs index c4d63d11a..4caba84a9 100644 --- a/src/timeout.rs +++ b/src/timeout.rs @@ -1,3 +1,5 @@ +use std::{num::ParseIntError, str::FromStr}; + /// Describes the timeout of a notification #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum Timeout { @@ -55,6 +57,18 @@ impl From for i32 { } } +impl FromStr for Timeout { + type Err = ParseIntError; + + fn from_str(s: &str) -> Result { + match s { + "default" => Ok(Timeout::Default), + "never" => Ok(Timeout::Never), + milliseconds => Ok(Timeout::Milliseconds(u32::from_str(milliseconds)?)), + } + } +} + pub struct TimeoutMessage(Timeout); impl From for TimeoutMessage { From 208d55dbf9147d5bde7d88faece7d791ab9d52c2 Mon Sep 17 00:00:00 2001 From: Hendrik Sollich Date: Sun, 11 Dec 2022 11:51:08 +0100 Subject: [PATCH 2/2] docs: add doc test for Timeout::from_str --- src/timeout.rs | 9 +++++++++ src/xdg/mod.rs | 8 ++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/timeout.rs b/src/timeout.rs index 4caba84a9..4458744a9 100644 --- a/src/timeout.rs +++ b/src/timeout.rs @@ -1,6 +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. diff --git a/src/xdg/mod.rs b/src/xdg/mod.rs index 11ed2637d..d9829d1c2 100644 --- a/src/xdg/mod.rs +++ b/src/xdg/mod.rs @@ -302,7 +302,7 @@ pub(crate) fn show_notification(notification: &Notification) -> Result Option { Some(DbusStack::Zbus) } -/// Get the currently dsed [`DbusStack`] +/// Get the currently used [`DbusStack`] /// /// (dbus-rs only) #[cfg(all(feature = "dbus", not(feature = "zbus")))] @@ -318,7 +318,7 @@ pub fn dbus_stack() -> Option { 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"))] @@ -330,7 +330,7 @@ pub fn dbus_stack() -> Option { }) } -/// 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")))]