From 977555bbee4032a0be6aa15fe4382b8c3d7958c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 9 Apr 2023 17:44:17 +0200 Subject: [PATCH] Rationalize the `new_action` parameters --- src/action.rs | 31 +++++++++++++------------------ src/main.rs | 17 ++++++++--------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/action.rs b/src/action.rs index 29d906f..3be22f3 100644 --- a/src/action.rs +++ b/src/action.rs @@ -7,6 +7,12 @@ use sqlx::SqlitePool; use std::sync::Arc; use tokio::sync::RwLock; +pub enum Action<'a> { + ReadLine(Arc>), + RotateKeys((&'a SqlitePool, &'a Config)), + SendMessage((Message, &'a Config)), +} + pub enum ActionResult { EndOfStream, KeyRotation, @@ -16,24 +22,13 @@ pub enum ActionResult { NewEntryError(String), } -pub async fn new_action( - reader_lock: Option>>, - db_opt: Option<(&SqlitePool, &Config)>, - msg_tpl: Option, -) -> ActionResult { - if let Some(reader_lock) = reader_lock { - return read_entry(reader_lock).await; - } - if let Some((db, cnf)) = db_opt { - match msg_tpl { - Some(msg) => { - return msg.sign_and_return(cnf).await; - } - None => { - key_rotation(db, cnf).await; - return ActionResult::KeyRotation; - } +pub async fn new_action(action: Action<'_>) -> ActionResult { + match action { + Action::ReadLine(reader_lock) => read_entry(reader_lock).await, + Action::RotateKeys((db, cnf)) => { + key_rotation(db, cnf).await; + ActionResult::KeyRotation } + Action::SendMessage((msg, cnf)) => msg.sign_and_return(cnf).await, } - ActionResult::MessageSentError("new_action: invalid parameters".to_string()) } diff --git a/src/main.rs b/src/main.rs index c92b3fc..139faf9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ mod message; mod parsed_message; mod stdin_reader; -use action::{new_action, ActionResult}; +use action::{new_action, Action, ActionResult}; use algorithm::Algorithm; use canonicalization::CanonicalizationType; use futures::stream::FuturesUnordered; @@ -88,10 +88,9 @@ async fn main_loop(cnf: &config::Config, db: &SqlitePool) { handshake::register_filter(); log_messages!(messages); let reader_lock = Arc::new(RwLock::new(reader)); - actions.push(new_action(Some(reader_lock.clone()), None, None)); - actions.push(new_action(None, Some((db, cnf)), None)); + actions.push(new_action(Action::ReadLine(reader_lock.clone()))); + actions.push(new_action(Action::RotateKeys((db, cnf)))); loop { - log::debug!("Wat???? {}", actions.len()); if actions.len() <= 1 { break; } @@ -101,7 +100,7 @@ async fn main_loop(cnf: &config::Config, db: &SqlitePool) { log::debug!("end of input stream"); } ActionResult::KeyRotation => { - actions.push(new_action(None, Some((db, cnf)), None)); + actions.push(new_action(Action::RotateKeys((db, cnf)))); } ActionResult::MessageSent(msg_id) => { log::debug!("message removed: {msg_id}"); @@ -119,7 +118,7 @@ async fn main_loop(cnf: &config::Config, db: &SqlitePool) { } else { log::debug!("message ready: {msg_id}"); if let Some(m) = messages.remove(&msg_id) { - actions.push(new_action(None, Some((db, cnf)), Some(m))); + actions.push(new_action(Action::SendMessage((m, cnf)))); } } } @@ -129,16 +128,16 @@ async fn main_loop(cnf: &config::Config, db: &SqlitePool) { if !entry.is_end_of_message() { messages.insert(msg_id.clone(), msg); } else { - actions.push(new_action(None, Some((db, cnf)), Some(msg))); + actions.push(new_action(Action::SendMessage((msg, cnf)))); } } } log_messages!(messages); - actions.push(new_action(Some(reader_lock.clone()), None, None)); + actions.push(new_action(Action::ReadLine(reader_lock.clone()))); } ActionResult::NewEntryError(err) => { log::error!("invalid filter line: {err}"); - actions.push(new_action(Some(reader_lock.clone()), None, None)); + actions.push(new_action(Action::ReadLine(reader_lock.clone()))); } } }