diff --git a/src/macros.rs b/src/macros.rs index 155c2a3..9723bb8 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -24,6 +24,7 @@ macro_rules! handler { #[macro_export] macro_rules! ask_name_handler { ($v: vis $function_name: ident ($($param: ident: $type: ty),*), $message: literal, $next_func: ident) => { + #[inline] $v async fn $function_name( bot: Throttle, 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, + msg: Message, + db: DatabaseConnection, + dialogue: MainDialogue, + next: PackagedHandler, + ) -> $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 + } + }; +} diff --git a/src/state/get_login.rs b/src/state/get_login.rs index 2f88907..c76782e 100644 --- a/src/state/get_login.rs +++ b/src/state/get_login.rs @@ -1,32 +1,24 @@ use crate::prelude::*; -/// Function to handle GetLogin state -pub async fn get_login( - bot: Throttle, - msg: Message, - db: DatabaseConnection, - dialogue: MainDialogue, - next: PackagedHandler, -) -> crate::Result<()> { - super::generic::generic( - bot, - msg, - db, - dialogue, - |bot, msg, _, login| { - Box::pin(async move { - 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 +#[inline] +async fn check_login( + bot: &Throttle, + msg: &Message, + _: &DatabaseConnection, + login: &str, +) -> crate::Result> { + 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) } + +crate::simple_state_handler!( + get_login, + check_login, + "Couldn't get the text of the message. Send the login again" +); diff --git a/src/state/get_master_pass.rs b/src/state/get_master_pass.rs index 4122f68..e32e748 100644 --- a/src/state/get_master_pass.rs +++ b/src/state/get_master_pass.rs @@ -39,21 +39,8 @@ async fn check_master_pass( Ok(None) } -pub async fn get_master_pass( - bot: Throttle, - msg: Message, - db: DatabaseConnection, - dialogue: MainDialogue, - next: PackagedHandler, -) -> 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 -} +crate::simple_state_handler!( + get_master_pass, + check_master_pass, + "Couldn't get the text of the message. Send the master password again" +); diff --git a/src/state/get_new_master_pass.rs b/src/state/get_new_master_pass.rs index c1a3b3f..9c0bbc1 100644 --- a/src/state/get_new_master_pass.rs +++ b/src/state/get_new_master_pass.rs @@ -35,6 +35,7 @@ fn process_validity(validity: PasswordValidity) -> Result<(), String> { async fn check_new_master_pass( bot: &Throttle, msg: &Message, + _: &DatabaseConnection, password: &str, ) -> crate::Result> { let validity = check_master_pass(password); @@ -48,22 +49,8 @@ async fn check_new_master_pass( } } -/// Handles GetNewMasterPass state -pub async fn get_new_master_pass( - bot: Throttle, - msg: Message, - db: DatabaseConnection, - dialogue: MainDialogue, - next: PackagedHandler, -) -> 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 -} +crate::simple_state_handler!( + get_new_master_pass, + check_new_master_pass, + "Couldn't get the text of the message. Send the master password again" +); diff --git a/src/state/get_new_name.rs b/src/state/get_new_name.rs index c067bbc..b40a66e 100644 --- a/src/state/get_new_name.rs +++ b/src/state/get_new_name.rs @@ -25,22 +25,8 @@ async fn check_new_account_name( Ok(None) } -/// Handles GetNewName state -pub async fn get_new_name( - bot: Throttle, - msg: Message, - db: DatabaseConnection, - dialogue: MainDialogue, - next: PackagedHandler, -) -> 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 -} +crate::simple_state_handler!( + get_new_name, + check_new_account_name, + "Couldn't get the text of the message. Send the name of the new account again" +); diff --git a/src/state/get_password.rs b/src/state/get_password.rs index 0d7a69b..70c8699 100644 --- a/src/state/get_password.rs +++ b/src/state/get_password.rs @@ -1,32 +1,24 @@ use crate::prelude::*; -/// Function to handle GetPassword state -pub async fn get_password( - bot: Throttle, - msg: Message, - db: DatabaseConnection, - dialogue: MainDialogue, - next: PackagedHandler, -) -> crate::Result<()> { - super::generic::generic( - bot, - msg, - db, - dialogue, - |bot, msg, _, password| { - Box::pin(async move { - 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 +#[inline] +async fn check_password( + bot: &Throttle, + msg: &Message, + _: &DatabaseConnection, + login: &str, +) -> crate::Result> { + let is_valid = validate_field(login); + if !is_valid { + let msg = bot + .send_message(msg.chat.id, "Invalid password. Try again") + .await?; + return Ok(Some(msg)); + } + Ok(None) } + +crate::simple_state_handler!( + get_password, + check_password, + "Couldn't get the text of the message. Send the password again" +);