Skip to content

Commit

Permalink
feat: implement FromStr for Timeout
Browse files Browse the repository at this point in the history
Useful when wanting to pass it as a command line argument to an
application.
  • Loading branch information
rnestler committed Dec 12, 2022
1 parent bd8e7c1 commit 86f40f1
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/timeout.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -55,6 +57,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)?)),
}
}
}

pub struct TimeoutMessage(Timeout);

impl From<Timeout> for TimeoutMessage {
Expand Down

0 comments on commit 86f40f1

Please sign in to comment.