Use the humantime crate to parse Duration

This commit is contained in:
finga 2021-06-27 02:39:22 +02:00
parent 851a387c82
commit 83e4ff36e5
8 changed files with 18 additions and 29 deletions

7
Cargo.lock generated
View file

@ -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",
]

View file

@ -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."

View file

@ -73,8 +73,7 @@ optional arguments are supported:
(format: `%Y-%m-%d %H:%M:%S`)
- `-p`, `--priority <priority>`: Use priority PRIO, PRIO >= 0
[default: 0]
- `-o`, `--timeout <timeout>`: Timeout in `<timeout>[s]` (default) or
`<timeout>ms`
- `-o`, `--timeout <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>`: Deadline until messages are received
(format: `%Y-%m-%d %H:%M:%S`)
- `-o,` `--timeout <timeout>`: Timeout in `<timeout>[s]` (default) or
`<timeout>ms`
- `-o,` `--timeout <timeout>`: As for example in "5h 23min 42ms"

4
mqrs.1
View file

@ -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<timeout>\fP
Timeout in "<timeout>[s]" (default) or "<timeout>ms"
Timeout as for example in "5h 23min 42ms"
.RE
.SS send [FLAGS] [OPTIONS] \fI<QUEUE>\fP \fI<MESSAGE>\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<timeout>\fP
Timeout in "<timeout>[s]" (default) or "<timeout>ms"
Timeout as for example in "5h 23min 42ms"
.RE
.SS unlink [FLAGS] \fI<QUEUE>\fP
Deletes an existing POSIX message queue.

View file

@ -7,7 +7,6 @@ mod info;
mod recv;
mod send;
mod unlink;
mod utils;
#[derive(Clap, Debug)]
#[clap(

View file

@ -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 "<timeout>[s]" (default) or "<timeout>ms"
/// Timeout, example "5h 23min 42ms"
#[clap(short = 'o', long, conflicts_with = "deadline")]
pub timeout: Option<String>,
/// 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::<Duration>()?)?;
print_message(verbose, prio, len, self.timestamp, str::from_utf8(&buf)?);
} else if let Some(deadline) = &self.deadline {

View file

@ -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 "<timeout>[s]" (default) or "<timeout>ms"
/// Timeout, example "5h 23min 42ms"
#[clap(short = 'o', long, conflicts_with = "deadline")]
pub timeout: Option<String>,
/// 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::<Duration>()?,
)?;
if verbose {

View file

@ -1,16 +0,0 @@
use anyhow::Result;
use std::{str, time::Duration};
pub fn parse_duration(timeout: &str) -> Result<Duration> {
if timeout.ends_with("ms") {
Ok(Duration::from_millis(
timeout[0..timeout.len() - 2].parse::<u64>()?,
))
} else if timeout.ends_with("s") {
Ok(Duration::from_secs(
timeout[0..timeout.len() - 1].parse::<u64>()?,
))
} else {
Ok(Duration::from_secs(timeout.parse::<u64>()?))
}
}