From 010ac62a74e70b19c624c4d27130200df40d54c1 Mon Sep 17 00:00:00 2001 From: StNicolay Date: Sun, 16 Jul 2023 22:54:01 +0300 Subject: [PATCH] Added ask_name_handler --- src/commands/delete.rs | 29 +---------------------------- src/commands/get_account.rs | 29 +---------------------------- src/macros.rs | 35 ++++++++++++++++++++++++++++++++++- src/prelude.rs | 1 + 4 files changed, 37 insertions(+), 57 deletions(-) diff --git a/src/commands/delete.rs b/src/commands/delete.rs index acbcb5d..0519365 100644 --- a/src/commands/delete.rs +++ b/src/commands/delete.rs @@ -25,31 +25,4 @@ handler!( State::GetMasterPass, get_master_pass ); - -/// Handles /delete command -pub async fn delete( - bot: Throttle, - msg: Message, - dialogue: MainDialogue, - db: DatabaseConnection, -) -> crate::Result<()> { - let user_id = msg.from().ok_or(NoUserInfo)?.id.0; - let markup = account_markup(user_id, &db).await?; - if markup.keyboard.is_empty() { - bot.send_message(msg.chat.id, "No accounts found") - .reply_markup(deletion_markup()) - .await?; - return Ok(()); - } - let previous = bot - .send_message(msg.chat.id, "Send the name of the account to delete") - .reply_markup(markup) - .await?; - dialogue - .update(State::GetExistingName(Handler::new( - get_account_name, - &previous, - ))) - .await?; - Ok(()) -} +ask_name_handler!(pub delete(), "Send the name of the account to delete", get_account_name); diff --git a/src/commands/get_account.rs b/src/commands/get_account.rs index bea2de7..3cfd8c8 100644 --- a/src/commands/get_account.rs +++ b/src/commands/get_account.rs @@ -32,31 +32,4 @@ async fn get_master_pass( } handler!(get_account_name(name:String), "Send master password", State::GetMasterPass, get_master_pass); - -/// Handles /get_account command -pub async fn get_account( - bot: Throttle, - msg: Message, - dialogue: MainDialogue, - db: DatabaseConnection, -) -> crate::Result<()> { - let user_id = msg.from().ok_or(NoUserInfo)?.id.0; - let markup = account_markup(user_id, &db).await?; - if markup.keyboard.is_empty() { - bot.send_message(msg.chat.id, "No accounts found") - .reply_markup(deletion_markup()) - .await?; - return Ok(()); - } - let previous = bot - .send_message(msg.chat.id, "Send the name of the account to get") - .reply_markup(markup) - .await?; - dialogue - .update(State::GetExistingName(Handler::new( - get_account_name, - &previous, - ))) - .await?; - Ok(()) -} +ask_name_handler!(pub get_account(), "Send the name of the account to get", get_account_name); diff --git a/src/macros.rs b/src/macros.rs index ca30a44..bc213f4 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -7,7 +7,7 @@ macro_rules! handler { msg: Message, _: DatabaseConnection, dialogue: MainDialogue, - $($param: $type,)* + $($param: $type),* ) -> $crate::Result<()> { let previous = bot.send_message(msg.chat.id, $message).await?; dialogue @@ -20,3 +20,36 @@ macro_rules! handler { } }; } + +#[macro_export] +macro_rules! ask_name_handler { + ($v: vis $function_name: ident ($($param: ident: $type: ty),*), $message: literal, $next_func: ident) => { + $v async fn $function_name( + bot: Throttle, + msg: Message, + dialogue: MainDialogue, + db: DatabaseConnection, + $($param: $type),* + ) -> $crate::Result<()> { + let user_id = msg.from().ok_or(NoUserInfo)?.id.0; + let markup = account_markup(user_id, &db).await?; + if markup.keyboard.is_empty() { + bot.send_message(msg.chat.id, "No accounts found") + .reply_markup(deletion_markup()) + .await?; + return Ok(()); + } + let previous = bot + .send_message(msg.chat.id, $message) + .reply_markup(markup) + .await?; + dialogue + .update(State::GetExistingName(Handler::new( + $next_func, + &previous, + ))) + .await?; + Ok(()) + } + }; +} diff --git a/src/prelude.rs b/src/prelude.rs index 03a0344..04ff7f6 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,4 +1,5 @@ pub(crate) use crate::{ + ask_name_handler, commands::Command, errors::*, handler,