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

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>()?))
}
}