diff --git a/Cargo.lock b/Cargo.lock index a1e0a53..2e308d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,6 +98,12 @@ dependencies = [ "libc", ] +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "indexmap" version = "1.6.2" @@ -127,6 +133,7 @@ dependencies = [ "anyhow", "chrono", "clap", + "humantime", "posixmq", ] diff --git a/Cargo.toml b/Cargo.toml index 61c7b1f..a7b199d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ anyhow = "1.0" clap = "3.0.0-beta.2" posixmq = "1.0" chrono = "0.4" +humantime = "2.1" [package.metadata.deb] extended-description = "`mqrs` is a small cli application to handle POSIX message queues." diff --git a/README.md b/README.md index 23249a3..f0fce16 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,7 @@ optional arguments are supported: (format: `%Y-%m-%d %H:%M:%S`) - `-p`, `--priority `: Use priority PRIO, PRIO >= 0 [default: 0] -- `-o`, `--timeout `: Timeout in `[s]` (default) or - `ms` +- `-o`, `--timeout `: As for example in "5h 23min 42ms" ### Receive a message from a queue Use the `recv` command to receive one or more messages from a message @@ -84,5 +83,4 @@ queue. Following optional arguments are supported: - `-t`, `--timestamp`: Print a timestamp before each message - `-d`, `--deadline `: Deadline until messages are received (format: `%Y-%m-%d %H:%M:%S`) -- `-o,` `--timeout `: Timeout in `[s]` (default) or - `ms` +- `-o,` `--timeout `: As for example in "5h 23min 42ms" diff --git a/mqrs.1 b/mqrs.1 index f4af9fc..e2fb028 100644 --- a/mqrs.1 +++ b/mqrs.1 @@ -125,7 +125,7 @@ Produce verbose output Deadline until messages are received (format: "%Y-%m-%d %H:%M:%S") .TP 8 .B \-o, \-\-timeout \fI\fP -Timeout in "[s]" (default) or "ms" +Timeout as for example in "5h 23min 42ms" .RE .SS send [FLAGS] [OPTIONS] \fI\fP \fI\fP Send a message to a message queue. @@ -163,7 +163,7 @@ Deadline until messages are received (format: "%Y-%m-%d %H:%M:%S") Use priority PRIO, PRIO >= 0 [default: 0] .TP 8 .B \-o, \-\-timeout \fI\fP -Timeout in "[s]" (default) or "ms" +Timeout as for example in "5h 23min 42ms" .RE .SS unlink [FLAGS] \fI\fP Deletes an existing POSIX message queue. diff --git a/src/main.rs b/src/main.rs index 8cf86b7..a8a6481 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,6 @@ mod info; mod recv; mod send; mod unlink; -mod utils; #[derive(Clap, Debug)] #[clap( diff --git a/src/recv.rs b/src/recv.rs index 8c045d5..afb6758 100644 --- a/src/recv.rs +++ b/src/recv.rs @@ -1,7 +1,7 @@ -use crate::utils; use anyhow::Result; use chrono::{DateTime, Local}; use clap::Clap; +use humantime::Duration; use posixmq::PosixMq; use std::str; @@ -17,7 +17,7 @@ pub struct Recv { /// Print a timestamp before each message #[clap(short, long)] pub timestamp: bool, - /// Timeout in "[s]" (default) or "ms" + /// Timeout, example "5h 23min 42ms" #[clap(short = 'o', long, conflicts_with = "deadline")] pub timeout: Option, /// Deadline until messages are received (format: "%Y-%m-%d %H:%M:%S") @@ -45,7 +45,7 @@ impl Recv { let mut buf = vec![0; mq.attributes()?.max_msg_len]; if let Some(timeout) = &self.timeout { - let (prio, len) = mq.recv_timeout(&mut buf, utils::parse_duration(timeout)?)?; + let (prio, len) = mq.recv_timeout(&mut buf, *timeout.parse::()?)?; print_message(verbose, prio, len, self.timestamp, str::from_utf8(&buf)?); } else if let Some(deadline) = &self.deadline { diff --git a/src/send.rs b/src/send.rs index cd368c1..e9a8d5e 100644 --- a/src/send.rs +++ b/src/send.rs @@ -1,7 +1,7 @@ -use crate::utils; use anyhow::Result; use chrono::DateTime; use clap::Clap; +use humantime::Duration; /// Send a message to a message queue #[derive(Clap, Debug)] @@ -12,7 +12,7 @@ pub struct Send { /// Do not block #[clap(short, long)] pub non_blocking: bool, - /// Timeout in "[s]" (default) or "ms" + /// Timeout, example "5h 23min 42ms" #[clap(short = 'o', long, conflicts_with = "deadline")] pub timeout: Option, /// Deadline until messages are sent (format: "%Y-%m-%d %H:%M:%S") @@ -38,7 +38,7 @@ impl Send { mq.open(&self.queue)?.send_timeout( self.priority, &self.msg.as_bytes(), - utils::parse_duration(timeout)?, + *timeout.parse::()?, )?; if verbose { diff --git a/src/utils.rs b/src/utils.rs deleted file mode 100644 index f810f1e..0000000 --- a/src/utils.rs +++ /dev/null @@ -1,16 +0,0 @@ -use anyhow::Result; -use std::{str, time::Duration}; - -pub fn parse_duration(timeout: &str) -> Result { - if timeout.ends_with("ms") { - Ok(Duration::from_millis( - timeout[0..timeout.len() - 2].parse::()?, - )) - } else if timeout.ends_with("s") { - Ok(Duration::from_secs( - timeout[0..timeout.len() - 1].parse::()?, - )) - } else { - Ok(Duration::from_secs(timeout.parse::()?)) - } -}