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