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;
|
|
|
|
|
|
|
|
pub enum ActionResult {
|
|
|
|
EndOfStream,
|
2023-04-09 17:21:17 +02:00
|
|
|
KeyRotation,
|
2023-04-09 15:13:18 +02:00
|
|
|
MessageSent(String),
|
|
|
|
MessageSentError(String),
|
|
|
|
NewEntry(crate::entry::Entry),
|
|
|
|
NewEntryError(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn new_action(
|
|
|
|
reader_lock: Option<Arc<RwLock<StdinReader>>>,
|
2023-04-09 17:21:17 +02:00
|
|
|
db_opt: Option<(&SqlitePool, &Config)>,
|
|
|
|
msg_tpl: Option<Message>,
|
2023-04-09 15:13:18 +02:00
|
|
|
) -> ActionResult {
|
|
|
|
if let Some(reader_lock) = reader_lock {
|
|
|
|
return read_entry(reader_lock).await;
|
|
|
|
}
|
2023-04-09 17:21:17 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2023-04-09 15:13:18 +02:00
|
|
|
}
|
|
|
|
ActionResult::MessageSentError("new_action: invalid parameters".to_string())
|
|
|
|
}
|