Centralize the use of ActionResult

This commit is contained in:
Rodolphe Bréard 2023-04-09 17:54:07 +02:00
parent 977555bbee
commit ade9532c36
4 changed files with 17 additions and 14 deletions

View file

@ -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)
}
}
}

View file

@ -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<RwLock<StdinReader>>) -> ActionResult {
pub async fn read_entry(reader_lock: Arc<RwLock<StdinReader>>) -> Option<Result<Entry, String>> {
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<RwLock<StdinReader>>) -> 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,
}
}

View file

@ -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) {

View file

@ -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) {