From 1225adefc2329b1337ab77c4839e4ede759659f3 Mon Sep 17 00:00:00 2001 From: StNicolay Date: Sun, 16 Jul 2023 17:28:40 +0300 Subject: [PATCH] Now forming MessageIds only from &Message to reduce the amount of moving this large type --- src/commands/add_account.rs | 8 ++++---- src/commands/delete.rs | 4 ++-- src/commands/delete_all.rs | 2 +- src/commands/export.rs | 2 +- src/commands/get_account.rs | 4 ++-- src/commands/import.rs | 4 ++-- src/commands/set_master_pass.rs | 2 +- src/prelude.rs | 2 +- src/state/generic.rs | 4 ++-- src/state/get_existing_name.rs | 4 ++-- src/state/get_user.rs | 8 ++++---- src/state/handler.rs | 7 +++---- src/state/mod.rs | 2 +- 13 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/commands/add_account.rs b/src/commands/add_account.rs index f6f132e..e6eed26 100644 --- a/src/commands/add_account.rs +++ b/src/commands/add_account.rs @@ -43,7 +43,7 @@ async fn get_password( move |bot, msg, db, dialogue, master_pass| { get_master_pass(bot, msg, db, dialogue, account, master_pass) }, - previous, + &previous, ))) .await?; Ok(()) @@ -64,7 +64,7 @@ async fn get_login( move |bot, msg, db, dialogue, password| { get_password(bot, msg, db, dialogue, name, login, password) }, - previous, + &previous, ))) .await?; Ok(()) @@ -82,7 +82,7 @@ async fn get_account_name( dialogue .update(State::GetLogin(Handler::new( move |bot, msg, db, dialogue, login| get_login(bot, msg, db, dialogue, name, login), - previous, + &previous, ))) .await?; Ok(()) @@ -96,7 +96,7 @@ pub async fn add_account( ) -> crate::Result<()> { let previous = bot.send_message(msg.chat.id, "Send account name").await?; dialogue - .update(State::GetNewName(Handler::new(get_account_name, previous))) + .update(State::GetNewName(Handler::new(get_account_name, &previous))) .await?; Ok(()) } diff --git a/src/commands/delete.rs b/src/commands/delete.rs index 294694d..2154ed5 100644 --- a/src/commands/delete.rs +++ b/src/commands/delete.rs @@ -32,7 +32,7 @@ async fn get_account_name( dialogue .update(State::GetMasterPass(Handler::new( move |bot, msg, db, dialogue, _| get_master_pass(bot, msg, db, dialogue, name), - previous, + &previous, ))) .await?; Ok(()) @@ -60,7 +60,7 @@ pub async fn delete( dialogue .update(State::GetExistingName(Handler::new( get_account_name, - previous, + &previous, ))) .await?; Ok(()) diff --git a/src/commands/delete_all.rs b/src/commands/delete_all.rs index 3096e61..3b79247 100644 --- a/src/commands/delete_all.rs +++ b/src/commands/delete_all.rs @@ -40,7 +40,7 @@ pub async fn delete_all( dialogue .update(State::GetMasterPass(Handler::new( get_master_pass, - previous, + &previous, ))) .await?; Ok(()) diff --git a/src/commands/export.rs b/src/commands/export.rs index 9f80ed1..f1c659d 100644 --- a/src/commands/export.rs +++ b/src/commands/export.rs @@ -61,7 +61,7 @@ pub async fn export(bot: Throttle, msg: Message, dialogue: MainDialogue) -> dialogue .update(State::GetMasterPass(Handler::new( get_master_pass, - previous, + &previous, ))) .await?; Ok(()) diff --git a/src/commands/get_account.rs b/src/commands/get_account.rs index dd101c7..f5c39d3 100644 --- a/src/commands/get_account.rs +++ b/src/commands/get_account.rs @@ -47,7 +47,7 @@ async fn get_account_name( move |bot, msg, db, dialogue, master_pass| { get_master_pass(bot, msg, db, dialogue, name, master_pass) }, - previous, + &previous, ))) .await?; Ok(()) @@ -75,7 +75,7 @@ pub async fn get_account( dialogue .update(State::GetExistingName(Handler::new( get_account_name, - previous, + &previous, ))) .await?; Ok(()) diff --git a/src/commands/import.rs b/src/commands/import.rs index 2708ec6..6a215cc 100644 --- a/src/commands/import.rs +++ b/src/commands/import.rs @@ -79,7 +79,7 @@ async fn get_document( move |bot, msg, db, dialogue, master_pass| { get_master_pass(bot, msg, db, dialogue, master_pass, user) }, - previous, + &previous, ))) .await?; Ok(()) @@ -94,7 +94,7 @@ pub async fn import(bot: Throttle, msg: Message, dialogue: MainDialogue) -> ) .await?; dialogue - .update(State::GetUser(Handler::new(get_document, previous))) + .update(State::GetUser(Handler::new(get_document, &previous))) .await?; Ok(()) } diff --git a/src/commands/set_master_pass.rs b/src/commands/set_master_pass.rs index a9a0eb9..e735c6a 100644 --- a/src/commands/set_master_pass.rs +++ b/src/commands/set_master_pass.rs @@ -42,7 +42,7 @@ pub async fn set_master_pass( dialogue .update(State::GetNewMasterPass(Handler::new( get_master_pass, - previous, + &previous, ))) .await?; Ok(()) diff --git a/src/prelude.rs b/src/prelude.rs index ab70bf6..5e27f66 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -4,7 +4,7 @@ pub(crate) use crate::{ markups::*, models::*, state::State, - state::{Handler, MainDialogue, PackagedHandler}, + state::{Handler, MainDialogue, MessageIds, PackagedHandler}, utils::*, }; pub(crate) use cryptography::prelude::*; diff --git a/src/state/generic.rs b/src/state/generic.rs index 6bef410..6f382f9 100644 --- a/src/state/generic.rs +++ b/src/state/generic.rs @@ -32,7 +32,7 @@ where Some(text) => text.trim(), None => { let msg = bot.send_message(msg.chat.id, no_text_message).await?; - handler.previous = msg.into(); + handler.previous = MessageIds::from(&msg); return Ok(()); } }; @@ -46,7 +46,7 @@ where } if let Some(failure_message) = check(&bot, &msg, &db, text).await? { - handler.previous = failure_message.into(); + handler.previous = MessageIds::from(&failure_message); return Ok(()); } diff --git a/src/state/get_existing_name.rs b/src/state/get_existing_name.rs index 4a21ac7..9ff342c 100644 --- a/src/state/get_existing_name.rs +++ b/src/state/get_existing_name.rs @@ -47,7 +47,7 @@ pub async fn get_existing_name( ) .reply_markup(account_markup(user_id, &db).await?) .await?; - handler.previous = msg.into(); + handler.previous = MessageIds::from(&msg); return Ok(()); } }; @@ -61,7 +61,7 @@ pub async fn get_existing_name( } if let Some(failure_message) = check_name(&bot, &msg, &db, text, user_id).await? { - handler.previous = failure_message.into(); + handler.previous = MessageIds::from(&failure_message); return Ok(()); } diff --git a/src/state/get_user.rs b/src/state/get_user.rs index 2c40b69..4c24187 100644 --- a/src/state/get_user.rs +++ b/src/state/get_user.rs @@ -137,7 +137,7 @@ pub async fn get_user( let msg = bot .send_message(msg.chat.id, "You didn't send a file. Try again") .await?; - handler.previous = msg.into(); + handler.previous = MessageIds::from(&msg); return Ok(()); } }; @@ -151,7 +151,7 @@ pub async fn get_user( "Invalid file name. You need to send a json file. Try again", ) .await?; - handler.previous = msg.into(); + handler.previous = MessageIds::from(&msg); return Ok(()); } } @@ -170,14 +170,14 @@ pub async fn get_user( Ok(Ok(user)) => user, Ok(Err(error_text)) => { let msg = bot.send_message(msg.chat.id, error_text).await?; - handler.previous = msg.into(); + handler.previous = MessageIds::from(&msg); return Ok(()); } Err(_) => { let msg = bot .send_message(msg.chat.id, "Error parsing the json file. Try again") .await?; - handler.previous = msg.into(); + handler.previous = MessageIds::from(&msg); return Ok(()); } }; diff --git a/src/state/handler.rs b/src/state/handler.rs index 30fae1d..8b51efd 100644 --- a/src/state/handler.rs +++ b/src/state/handler.rs @@ -1,6 +1,6 @@ use crate::prelude::*; use futures::future::BoxFuture; -use std::{borrow::Borrow, future::Future, sync::Arc}; +use std::{future::Future, sync::Arc}; use teloxide::types::MessageId; use tokio::sync::Mutex; @@ -14,10 +14,9 @@ impl MessageIds { } } -impl> From for MessageIds { +impl From<&Message> for MessageIds { #[inline] - fn from(value: T) -> Self { - let value: &Message = value.borrow(); + fn from(value: &Message) -> Self { Self(value.chat.id, value.id) } } diff --git a/src/state/mod.rs b/src/state/mod.rs index 7259deb..eaf432e 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -17,7 +17,7 @@ pub use get_new_master_pass::get_new_master_pass; pub use get_new_name::get_new_name; pub use get_password::get_password; pub use get_user::get_user; -pub use handler::{Handler, PackagedHandler}; +pub use handler::{Handler, MessageIds, PackagedHandler}; use crate::prelude::*; use teloxide::dispatching::dialogue::InMemStorage;