Simplified Handler::new

This commit is contained in:
StNicolay 2023-07-16 22:59:16 +03:00
parent 010ac62a74
commit 4a1aa7203b
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
3 changed files with 19 additions and 15 deletions

View File

@ -7,14 +7,13 @@ async fn get_master_pass(
msg: Message, msg: Message,
db: DatabaseConnection, db: DatabaseConnection,
dialogue: MainDialogue, dialogue: MainDialogue,
master_password: String, master_pass: String,
) -> crate::Result<()> { ) -> crate::Result<()> {
dialogue.exit().await?; dialogue.exit().await?;
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let model = spawn_blocking(move || { let model =
master_pass::ActiveModel::from_unencrypted(user_id, &master_password) spawn_blocking(move || master_pass::ActiveModel::from_unencrypted(user_id, &master_pass))
}) .await?;
.await?;
model.insert(&db).await?; model.insert(&db).await?;
bot.send_message(msg.chat.id, "Success") bot.send_message(msg.chat.id, "Success")
.reply_markup(deletion_markup()) .reply_markup(deletion_markup())
@ -41,7 +40,9 @@ pub async fn set_master_pass(
.await?; .await?;
dialogue dialogue
.update(State::GetNewMasterPass(Handler::new( .update(State::GetNewMasterPass(Handler::new(
get_master_pass, |bot, msg, db, dialogue, master_pass| {
Box::pin(get_master_pass(bot, msg, db, dialogue, master_pass))
},
&previous, &previous,
))) )))
.await?; .await?;

View File

@ -12,7 +12,7 @@ macro_rules! handler {
let previous = bot.send_message(msg.chat.id, $message).await?; let previous = bot.send_message(msg.chat.id, $message).await?;
dialogue dialogue
.update($next_state(Handler::new( .update($next_state(Handler::new(
move |bot, msg, db, dialogue, param| $next_func(bot, msg, db, dialogue, $($param,)* param), move |bot, msg, db, dialogue, param| Box::pin($next_func(bot, msg, db, dialogue, $($param,)* param)),
&previous, &previous,
))) )))
.await?; .await?;
@ -45,7 +45,7 @@ macro_rules! ask_name_handler {
.await?; .await?;
dialogue dialogue
.update(State::GetExistingName(Handler::new( .update(State::GetExistingName(Handler::new(
$next_func, move |bot, msg, db, dialogue, param| Box::pin($next_func(bot, msg, db, dialogue, $($param,)* param)),
&previous, &previous,
))) )))
.await?; .await?;

View File

@ -1,6 +1,6 @@
use crate::prelude::*; use crate::prelude::*;
use futures::future::BoxFuture; use futures::future::BoxFuture;
use std::{future::Future, sync::Arc}; use std::sync::Arc;
use teloxide::types::MessageId; use teloxide::types::MessageId;
use tokio::sync::Mutex; use tokio::sync::Mutex;
@ -43,17 +43,20 @@ pub type PackagedHandler<T> = Arc<Mutex<Handler<T>>>;
impl<T> Handler<T> { impl<T> Handler<T> {
/// Convinience method to convert a simple async function and a previous message into PackagedHandler /// Convinience method to convert a simple async function and a previous message into PackagedHandler
#[inline] #[inline]
pub fn new<H, F>(f: H, previous: impl Into<MessageIds>) -> PackagedHandler<T> pub fn new<H>(f: H, previous: impl Into<MessageIds>) -> PackagedHandler<T>
where where
H: FnOnce(Throttle<Bot>, Message, DatabaseConnection, MainDialogue, T) -> F H: FnOnce(
Throttle<Bot>,
Message,
DatabaseConnection,
MainDialogue,
T,
) -> BoxFuture<'static, crate::Result<()>>
+ Send + Send
+ 'static, + 'static,
F: Future<Output = crate::Result<()>> + Send + 'static,
{ {
let handler = Self { let handler = Self {
func: Some(Box::new(|bot, msg, db, dialogue, val| { func: Some(Box::new(f)),
Box::pin(f(bot, msg, db, dialogue, val))
})),
previous: previous.into(), previous: previous.into(),
}; };
Arc::new(Mutex::new(handler)) Arc::new(Mutex::new(handler))