Use a fn for verbose output when sending

This commit is contained in:
finga 2021-06-27 02:58:56 +02:00
parent f0fb50bc24
commit 606b4de524

View file

@ -26,6 +26,12 @@ pub struct Send {
pub msg: String,
}
fn print_verbose(verbose: bool, msg: &str, queue: &str) {
if verbose {
println!("Sent message: \"{}\" to queue: {}", &msg, &queue);
}
}
impl Send {
pub fn run(&self, verbose: bool) -> Result<()> {
let mq = &mut posixmq::OpenOptions::writeonly();
@ -41,9 +47,7 @@ impl Send {
*timeout.parse::<Duration>()?,
)?;
if verbose {
println!("Sent message: \"{}\" to queue: {}", &self.msg, &self.queue);
}
print_verbose(verbose, &self.msg, &self.queue);
} else if let Some(deadline) = &self.deadline {
mq.open(&self.queue)?.send_deadline(
self.priority,
@ -51,16 +55,12 @@ impl Send {
DateTime::parse_from_str(deadline, "%Y-%m-%d %H:%M:%S")?.into(),
)?;
if verbose {
println!("Sent message: \"{}\" to queue: {}", &self.msg, &self.queue);
}
print_verbose(verbose, &self.msg, &self.queue);
} else {
mq.open(&self.queue)?
.send(self.priority, &self.msg.as_bytes())?;
if verbose {
println!("Sent message: \"{}\" to queue: {}", &self.msg, &self.queue);
}
print_verbose(verbose, &self.msg, &self.queue);
}
Ok(())