2023-04-09 15:13:18 +02:00
|
|
|
use crate::config::Config;
|
|
|
|
use crate::entry::read_entry;
|
2023-04-09 17:21:17 +02:00
|
|
|
use crate::key::key_rotation;
|
2023-04-09 15:13:18 +02:00
|
|
|
use crate::message::Message;
|
|
|
|
use crate::stdin_reader::StdinReader;
|
2023-04-09 17:21:17 +02:00
|
|
|
use sqlx::SqlitePool;
|
2023-04-09 15:13:18 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
2023-04-09 17:44:17 +02:00
|
|
|
pub enum Action<'a> {
|
|
|
|
ReadLine(Arc<RwLock<StdinReader>>),
|
|
|
|
RotateKeys((&'a SqlitePool, &'a Config)),
|
|
|
|
SendMessage((Message, &'a Config)),
|
|
|
|
}
|
|
|
|
|
2023-04-09 15:13:18 +02:00
|
|
|
pub enum ActionResult {
|
|
|
|
EndOfStream,
|
2023-04-09 17:21:17 +02:00
|
|
|
KeyRotation,
|
2023-04-09 15:13:18 +02:00
|
|
|
MessageSent(String),
|
|
|
|
NewEntry(crate::entry::Entry),
|
|
|
|
NewEntryError(String),
|
|
|
|
}
|
|
|
|
|
2023-04-09 17:44:17 +02:00
|
|
|
pub async fn new_action(action: Action<'_>) -> ActionResult {
|
|
|
|
match action {
|
2023-04-09 17:54:07 +02:00
|
|
|
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,
|
|
|
|
},
|
2023-04-09 17:44:17 +02:00
|
|
|
Action::RotateKeys((db, cnf)) => {
|
|
|
|
key_rotation(db, cnf).await;
|
|
|
|
ActionResult::KeyRotation
|
2023-04-09 17:21:17 +02:00
|
|
|
}
|
2023-04-09 17:54:07 +02:00
|
|
|
Action::SendMessage((msg, cnf)) => {
|
|
|
|
let msg_id = msg.sign_and_return(cnf).await;
|
|
|
|
ActionResult::MessageSent(msg_id)
|
|
|
|
}
|
2023-04-09 15:13:18 +02:00
|
|
|
}
|
|
|
|
}
|