Added simple_state_handler macro
This commit is contained in:
		@@ -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<Bot>,
 | 
			
		||||
            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
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,32 +1,24 @@
 | 
			
		||||
use crate::prelude::*;
 | 
			
		||||
 | 
			
		||||
/// Function to handle GetLogin state
 | 
			
		||||
pub async fn get_login(
 | 
			
		||||
    bot: Throttle<Bot>,
 | 
			
		||||
    msg: Message,
 | 
			
		||||
    db: DatabaseConnection,
 | 
			
		||||
    dialogue: MainDialogue,
 | 
			
		||||
    next: PackagedHandler<String>,
 | 
			
		||||
) -> 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<Bot>,
 | 
			
		||||
    msg: &Message,
 | 
			
		||||
    _: &DatabaseConnection,
 | 
			
		||||
    login: &str,
 | 
			
		||||
) -> crate::Result<Option<Message>> {
 | 
			
		||||
    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"
 | 
			
		||||
);
 | 
			
		||||
 
 | 
			
		||||
@@ -39,21 +39,8 @@ async fn check_master_pass(
 | 
			
		||||
    Ok(None)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn get_master_pass(
 | 
			
		||||
    bot: Throttle<Bot>,
 | 
			
		||||
    msg: Message,
 | 
			
		||||
    db: DatabaseConnection,
 | 
			
		||||
    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
 | 
			
		||||
}
 | 
			
		||||
crate::simple_state_handler!(
 | 
			
		||||
    get_master_pass,
 | 
			
		||||
    check_master_pass,
 | 
			
		||||
    "Couldn't get the text of the message. Send the master password again"
 | 
			
		||||
);
 | 
			
		||||
 
 | 
			
		||||
@@ -35,6 +35,7 @@ fn process_validity(validity: PasswordValidity) -> Result<(), String> {
 | 
			
		||||
async fn check_new_master_pass(
 | 
			
		||||
    bot: &Throttle<Bot>,
 | 
			
		||||
    msg: &Message,
 | 
			
		||||
    _: &DatabaseConnection,
 | 
			
		||||
    password: &str,
 | 
			
		||||
) -> crate::Result<Option<Message>> {
 | 
			
		||||
    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<Bot>,
 | 
			
		||||
    msg: Message,
 | 
			
		||||
    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
 | 
			
		||||
}
 | 
			
		||||
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"
 | 
			
		||||
);
 | 
			
		||||
 
 | 
			
		||||
@@ -25,22 +25,8 @@ async fn check_new_account_name(
 | 
			
		||||
    Ok(None)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Handles GetNewName state
 | 
			
		||||
pub async fn get_new_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, 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"
 | 
			
		||||
);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,32 +1,24 @@
 | 
			
		||||
use crate::prelude::*;
 | 
			
		||||
 | 
			
		||||
/// Function to handle GetPassword state
 | 
			
		||||
pub async fn get_password(
 | 
			
		||||
    bot: Throttle<Bot>,
 | 
			
		||||
    msg: Message,
 | 
			
		||||
    db: DatabaseConnection,
 | 
			
		||||
    dialogue: MainDialogue,
 | 
			
		||||
    next: PackagedHandler<String>,
 | 
			
		||||
) -> 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<Bot>,
 | 
			
		||||
    msg: &Message,
 | 
			
		||||
    _: &DatabaseConnection,
 | 
			
		||||
    login: &str,
 | 
			
		||||
) -> crate::Result<Option<Message>> {
 | 
			
		||||
    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"
 | 
			
		||||
);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user