Added simple_state_handler macro

This commit is contained in:
StNicolay 2023-07-16 23:12:37 +03:00
parent 4a1aa7203b
commit 255a794b0f
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
6 changed files with 84 additions and 114 deletions

View File

@ -24,6 +24,7 @@ macro_rules! handler {
#[macro_export] #[macro_export]
macro_rules! ask_name_handler { macro_rules! ask_name_handler {
($v: vis $function_name: ident ($($param: ident: $type: ty),*), $message: literal, $next_func: ident) => { ($v: vis $function_name: ident ($($param: ident: $type: ty),*), $message: literal, $next_func: ident) => {
#[inline]
$v async fn $function_name( $v async fn $function_name(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
@ -53,3 +54,28 @@ macro_rules! ask_name_handler {
} }
}; };
} }
#[macro_export]
macro_rules! simple_state_handler {
($function_name: ident, $check: ident, $no_text_message: literal) => {
#[inline]
pub async fn $function_name(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
next: PackagedHandler<String>,
) -> $crate::Result<()> {
super::generic::generic(
bot,
msg,
db,
dialogue,
|bot, msg, db, param| Box::pin($check(bot, msg, db, param)),
$no_text_message,
next,
)
.await
}
};
}

View File

@ -1,32 +1,24 @@
use crate::prelude::*; use crate::prelude::*;
/// Function to handle GetLogin state #[inline]
pub async fn get_login( async fn check_login(
bot: Throttle<Bot>, bot: &Throttle<Bot>,
msg: Message, msg: &Message,
db: DatabaseConnection, _: &DatabaseConnection,
dialogue: MainDialogue, login: &str,
next: PackagedHandler<String>, ) -> crate::Result<Option<Message>> {
) -> crate::Result<()> { let is_valid = validate_field(login);
super::generic::generic( if !is_valid {
bot, let msg = bot
msg, .send_message(msg.chat.id, "Invalid login. Try again")
db, .await?;
dialogue, return Ok(Some(msg));
|bot, msg, _, login| { }
Box::pin(async move { Ok(None)
let is_valid = validate_field(login);
if !is_valid {
let msg = bot
.send_message(msg.chat.id, "Invalid login. Try again")
.await?;
return Ok(Some(msg));
}
Ok(None)
})
},
"Couldn't get the text of the message. Send the login again",
next,
)
.await
} }
crate::simple_state_handler!(
get_login,
check_login,
"Couldn't get the text of the message. Send the login again"
);

View File

@ -39,21 +39,8 @@ async fn check_master_pass(
Ok(None) Ok(None)
} }
pub async fn get_master_pass( crate::simple_state_handler!(
bot: Throttle<Bot>, get_master_pass,
msg: Message, check_master_pass,
db: DatabaseConnection, "Couldn't get the text of the message. Send the master password again"
dialogue: MainDialogue, );
next: PackagedHandler<String>,
) -> crate::Result<()> {
super::generic::generic(
bot,
msg,
db,
dialogue,
|bot, msg, db, text| Box::pin(check_master_pass(bot, msg, db, text)),
"Couldn't get the text of the message. Send the master password again",
next,
)
.await
}

View File

@ -35,6 +35,7 @@ fn process_validity(validity: PasswordValidity) -> Result<(), String> {
async fn check_new_master_pass( async fn check_new_master_pass(
bot: &Throttle<Bot>, bot: &Throttle<Bot>,
msg: &Message, msg: &Message,
_: &DatabaseConnection,
password: &str, password: &str,
) -> crate::Result<Option<Message>> { ) -> crate::Result<Option<Message>> {
let validity = check_master_pass(password); let validity = check_master_pass(password);
@ -48,22 +49,8 @@ async fn check_new_master_pass(
} }
} }
/// Handles GetNewMasterPass state crate::simple_state_handler!(
pub async fn get_new_master_pass( get_new_master_pass,
bot: Throttle<Bot>, check_new_master_pass,
msg: Message, "Couldn't get the text of the message. Send the master password again"
db: DatabaseConnection, );
dialogue: MainDialogue,
next: PackagedHandler<String>,
) -> crate::Result<()> {
super::generic::generic(
bot,
msg,
db,
dialogue,
|bot, msg, _, password| Box::pin(check_new_master_pass(bot, msg, password)),
"Couldn't get the text of the message. Send the master password again",
next,
)
.await
}

View File

@ -25,22 +25,8 @@ async fn check_new_account_name(
Ok(None) Ok(None)
} }
/// Handles GetNewName state crate::simple_state_handler!(
pub async fn get_new_name( get_new_name,
bot: Throttle<Bot>, check_new_account_name,
msg: Message, "Couldn't get the text of the message. Send the name of the new account again"
db: DatabaseConnection, );
dialogue: MainDialogue,
next: PackagedHandler<String>,
) -> crate::Result<()> {
super::generic::generic(
bot,
msg,
db,
dialogue,
|bot, msg, db, name| Box::pin(check_new_account_name(bot, msg, db, name)),
"Couldn't get the text of the message. Send the name of the new account again",
next,
)
.await
}

View File

@ -1,32 +1,24 @@
use crate::prelude::*; use crate::prelude::*;
/// Function to handle GetPassword state #[inline]
pub async fn get_password( async fn check_password(
bot: Throttle<Bot>, bot: &Throttle<Bot>,
msg: Message, msg: &Message,
db: DatabaseConnection, _: &DatabaseConnection,
dialogue: MainDialogue, login: &str,
next: PackagedHandler<String>, ) -> crate::Result<Option<Message>> {
) -> crate::Result<()> { let is_valid = validate_field(login);
super::generic::generic( if !is_valid {
bot, let msg = bot
msg, .send_message(msg.chat.id, "Invalid password. Try again")
db, .await?;
dialogue, return Ok(Some(msg));
|bot, msg, _, password| { }
Box::pin(async move { Ok(None)
let is_valid = validate_field(password);
if !is_valid {
let msg = bot
.send_message(msg.chat.id, "Invalid password. Try again")
.await?;
return Ok(Some(msg));
}
Ok(None)
})
},
"Couldn't get the text of the message. Send the password again",
next,
)
.await
} }
crate::simple_state_handler!(
get_password,
check_password,
"Couldn't get the text of the message. Send the password again"
);