Rationalize the new_action parameters

This commit is contained in:
Rodolphe Bréard 2023-04-09 17:44:17 +02:00
parent 558ffb2392
commit 977555bbee
2 changed files with 21 additions and 27 deletions

View file

@ -7,6 +7,12 @@ use sqlx::SqlitePool;
use std::sync::Arc;
use tokio::sync::RwLock;
pub enum Action<'a> {
ReadLine(Arc<RwLock<StdinReader>>),
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<Arc<RwLock<StdinReader>>>,
db_opt: Option<(&SqlitePool, &Config)>,
msg_tpl: Option<Message>,
) -> 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())
}

View file

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