From ade9532c36ee6f2a39870ae9ac48d22558458446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 9 Apr 2023 17:54:07 +0200 Subject: [PATCH] Centralize the use of ActionResult --- src/action.rs | 14 +++++++++++--- src/entry.rs | 9 ++++----- src/main.rs | 3 --- src/message.rs | 5 ++--- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/action.rs b/src/action.rs index 3be22f3..845fccb 100644 --- a/src/action.rs +++ b/src/action.rs @@ -17,18 +17,26 @@ pub enum ActionResult { EndOfStream, KeyRotation, MessageSent(String), - MessageSentError(String), NewEntry(crate::entry::Entry), NewEntryError(String), } pub async fn new_action(action: Action<'_>) -> ActionResult { match action { - Action::ReadLine(reader_lock) => read_entry(reader_lock).await, + Action::ReadLine(reader_lock) => match read_entry(reader_lock).await { + Some(r) => match r { + Ok(entry) => ActionResult::NewEntry(entry), + Err(err) => ActionResult::NewEntryError(err), + }, + None => ActionResult::EndOfStream, + }, Action::RotateKeys((db, cnf)) => { key_rotation(db, cnf).await; ActionResult::KeyRotation } - Action::SendMessage((msg, cnf)) => msg.sign_and_return(cnf).await, + Action::SendMessage((msg, cnf)) => { + let msg_id = msg.sign_and_return(cnf).await; + ActionResult::MessageSent(msg_id) + } } } diff --git a/src/entry.rs b/src/entry.rs index 701393c..efd4b19 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -1,4 +1,3 @@ -use crate::action::ActionResult; use crate::stdin_reader::StdinReader; use nom::bytes::streaming::{tag, take_till, take_while1}; use nom::IResult; @@ -39,7 +38,7 @@ impl Entry { } } -pub async fn read_entry(reader_lock: Arc>) -> ActionResult { +pub async fn read_entry(reader_lock: Arc>) -> Option> { let mut reader = reader_lock.write().await; log::trace!("reader lock on stdin locked"); let line_res = reader.read_line().await; @@ -47,10 +46,10 @@ pub async fn read_entry(reader_lock: Arc>) -> ActionResult { log::trace!("reader lock on stdin released"); match line_res { Some(line) => match Entry::from_bytes(&line) { - Ok(entry) => ActionResult::NewEntry(entry), - Err(err) => ActionResult::NewEntryError(err), + Ok(entry) => Some(Ok(entry)), + Err(err) => Some(Err(err)), }, - None => ActionResult::EndOfStream, + None => None, } } diff --git a/src/main.rs b/src/main.rs index 139faf9..ea35628 100644 --- a/src/main.rs +++ b/src/main.rs @@ -105,9 +105,6 @@ async fn main_loop(cnf: &config::Config, db: &SqlitePool) { ActionResult::MessageSent(msg_id) => { log::debug!("message removed: {msg_id}"); } - ActionResult::MessageSentError(err) => { - log::error!("{err}"); - } ActionResult::NewEntry(entry) => { let msg_id = entry.get_msg_id(); match messages.get_mut(&msg_id) { diff --git a/src/message.rs b/src/message.rs index 925b46b..bb31d32 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,4 +1,3 @@ -use crate::action::ActionResult; use crate::config::Config; use crate::entry::Entry; use crate::parsed_message::ParsedMessage; @@ -52,7 +51,7 @@ impl Message { self.nb_lines } - pub async fn sign_and_return(&self, cnf: &Config) -> ActionResult { + pub async fn sign_and_return(&self, cnf: &Config) -> String { log::trace!("content: {}", crate::display_bytes!(&self.content)); match ParsedMessage::from_bytes(&self.content) { Ok(parsed_msg) => { @@ -82,7 +81,7 @@ impl Message { } } self.print_msg().await; - ActionResult::MessageSent(get_msg_id(&self.session_id, &self.token)) + get_msg_id(&self.session_id, &self.token) } async fn print_msg(&self) {