Added LocaleString type that can be converted to String to reduce the ammount of as_refs

This commit is contained in:
2024-04-22 15:17:51 +03:00
parent 86c7b4d0c4
commit 305b796d51
35 changed files with 202 additions and 192 deletions

View File

@ -58,7 +58,7 @@ async fn get_master_pass(
ids.alter_message(
&bot,
locale.success_choose_account_to_view.as_ref(),
&locale.success_choose_account_to_view,
menu_markup("get", user_id, &db).await?,
None,
)
@ -89,18 +89,18 @@ pub async fn alter(
return Ok(());
};
let text = match field {
let text: &str = match field {
Name => {
change_state!(dialogue, ids, (name, field), State::GetNewName, get_field);
locale.send_new_name.as_ref()
&locale.send_new_name
}
Login => {
change_state!(dialogue, ids, (name, field), State::GetLogin, get_field);
locale.send_new_login.as_ref()
&locale.send_new_login
}
Pass => {
change_state!(dialogue, ids, (name, field), State::GetPassword, get_field);
locale.send_new_password.as_ref()
&locale.send_new_password
}
};

View File

@ -21,7 +21,7 @@ pub async fn change_locale(
if !is_successful {
ids.alter_message(
&bot,
locale.something_went_wrong.as_ref(),
&locale.something_went_wrong,
deletion_markup(locale),
None,
)
@ -30,13 +30,8 @@ pub async fn change_locale(
}
locale = new_locale.get_locale();
ids.alter_message(
&bot,
locale.choose_language.as_ref(),
language_markup(),
None,
)
.await?;
ids.alter_message(&bot, &locale.choose_language, language_markup(), None)
.await?;
Ok(())
}

View File

@ -19,7 +19,7 @@ async fn get_master_pass(
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let Some(account) = Account::get(user_id, &name, &db).await? else {
bot.send_message(msg.chat.id, locale.no_accounts_found.as_ref())
bot.send_message(msg.chat.id, &locale.no_accounts_found)
.reply_markup(deletion_markup(locale))
.await?;
return Ok(());
@ -53,14 +53,14 @@ pub async fn decrypt(
let user_id = q.from.id.0;
let Some(name) = Account::get_name_by_hash(user_id, &hash, &db).await? else {
bot.send_message(ids.0, locale.no_accounts_found.as_ref())
bot.send_message(ids.0, &locale.no_accounts_found)
.reply_markup(deletion_markup(locale))
.await?;
bot.answer_callback_query(q.id).await?;
return Ok(());
};
ids.alter_message(&bot, locale.send_master_password.as_ref(), None, None)
ids.alter_message(&bot, &locale.send_master_password, None, None)
.await?;
bot.answer_callback_query(q.id).await?;

View File

@ -17,7 +17,7 @@ async fn get_master_pass(
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
Account::delete(user_id, &name, &db).await?;
ids.alter_message(&bot, locale.success.as_ref(), deletion_markup(locale), None)
ids.alter_message(&bot, &locale.success, deletion_markup(locale), None)
.await?;
Ok(())
@ -32,19 +32,19 @@ pub async fn delete(
locale: LocaleRef,
(hash, is_command): (super::NameHash, bool),
) -> crate::Result<()> {
let text = locale.send_master_pass_to_delete_account.as_ref();
let mut ids: MessageIds = q.message.as_ref().unwrap().into();
let user_id = q.from.id.0;
let Some(name) = Account::get_name_by_hash(user_id, &hash, &db).await? else {
bot.send_message(ids.0, locale.no_accounts_found.as_ref())
bot.send_message(ids.0, &locale.no_accounts_found)
.reply_markup(deletion_markup(locale))
.await?;
bot.answer_callback_query(q.id).await?;
return Ok(());
};
let text: &str = &locale.send_master_pass_to_delete_account;
if is_command {
ids.alter_message(&bot, text, None, None).await?;
} else {

View File

@ -9,7 +9,7 @@ pub async fn delete_message(
) -> crate::Result<()> {
if let Some(msg) = q.message {
if bot.delete_message(msg.chat.id, msg.id).await.is_err() {
bot.send_message(msg.chat.id, locale.error_deleting_message.as_ref())
bot.send_message(msg.chat.id, &locale.error_deleting_message)
.reply_markup(deletion_markup(locale))
.await?;
}

View File

@ -13,7 +13,7 @@ pub async fn get(
let mut ids: MessageIds = q.message.as_ref().unwrap().into();
let Some(name) = Account::get_name_by_hash(user_id, &hash, &db).await? else {
bot.send_message(ids.0, locale.account_not_found.as_ref())
bot.send_message(ids.0, &locale.account_not_found)
.reply_markup(deletion_markup(locale))
.await?;
bot.answer_callback_query(q.id).await?;

View File

@ -14,7 +14,7 @@ pub async fn get_menu(
if markup.inline_keyboard.is_empty() {
ids.alter_message(
&bot,
locale.no_accounts_found.as_ref(),
&locale.no_accounts_found,
deletion_markup(locale),
None,
)
@ -22,7 +22,7 @@ pub async fn get_menu(
return Ok(());
}
ids.alter_message(&bot, locale.choose_account.as_ref(), markup, None)
ids.alter_message(&bot, &locale.choose_account, markup, None)
.await?;
bot.answer_callback_query(q.id).await?;
Ok(())

View File

@ -31,7 +31,7 @@ async fn get_master_pass(
.await?;
account.insert(&db).await?;
ids.alter_message(&bot, locale.success.as_ref(), deletion_markup(locale), None)
ids.alter_message(&bot, &locale.success, deletion_markup(locale), None)
.await?;
Ok(())
}

View File

@ -3,7 +3,7 @@ use crate::prelude::*;
/// Handles /cancel command when there's no active state
#[inline]
pub async fn cancel(bot: Throttle<Bot>, msg: Message, locale: LocaleRef) -> crate::Result<()> {
bot.send_message(msg.chat.id, locale.nothing_to_cancel.as_ref())
bot.send_message(msg.chat.id, &locale.nothing_to_cancel)
.reply_markup(deletion_markup(locale))
.await?;
Ok(())

View File

@ -6,7 +6,7 @@ pub async fn change_language(
msg: Message,
locale: LocaleRef,
) -> crate::Result<()> {
bot.send_message(msg.chat.id, locale.choose_language.as_ref())
bot.send_message(msg.chat.id, &locale.choose_language)
.reply_markup(language_markup())
.await?;
Ok(())

View File

@ -12,13 +12,13 @@ pub async fn delete(
let markup = menu_markup("delete1", user_id, &db).await?;
if markup.inline_keyboard.is_empty() {
bot.send_message(msg.chat.id, locale.no_accounts_found.as_ref())
bot.send_message(msg.chat.id, &locale.no_accounts_found)
.reply_markup(deletion_markup(locale))
.await?;
return Ok(());
}
bot.send_message(msg.chat.id, locale.choose_account.as_ref())
bot.send_message(msg.chat.id, &locale.choose_account)
.reply_markup(markup)
.await?;
Ok(())

View File

@ -24,12 +24,12 @@ async fn get_master_pass(
let text = match result {
(Ok(()), Ok(())) => {
txn.commit().await?;
locale.everything_was_deleted.as_ref()
&locale.everything_was_deleted
}
(Err(err), _) | (_, Err(err)) => {
error!("{}", crate::Error::from(err));
txn.rollback().await?;
locale.something_went_wrong.as_ref()
&locale.something_went_wrong
}
};
ids.alter_message(&bot, text, deletion_markup(locale), None)

View File

@ -12,13 +12,13 @@ pub async fn get_account(
let markup = menu_markup("decrypt", user_id, &db).await?;
if markup.inline_keyboard.is_empty() {
bot.send_message(msg.chat.id, locale.no_accounts_found.as_ref())
bot.send_message(msg.chat.id, &locale.no_accounts_found)
.reply_markup(deletion_markup(locale))
.await?;
return Ok(());
}
bot.send_message(msg.chat.id, locale.choose_account.as_ref())
bot.send_message(msg.chat.id, &locale.choose_account)
.reply_markup(markup)
.await?;
Ok(())

View File

@ -14,7 +14,7 @@ pub async fn get_accounts(
let mut account_names = Account::get_names(user_id, &db);
let Some(mut text) = account_names.try_next().await? else {
bot.send_message(msg.chat.id, locale.no_accounts_found.as_ref())
bot.send_message(msg.chat.id, &locale.no_accounts_found)
.reply_markup(deletion_markup(locale))
.await?;
return Ok(());

View File

@ -3,7 +3,7 @@ use crate::prelude::*;
/// Handles the help command by sending the passwords descryptions
#[inline]
pub async fn help(bot: Throttle<Bot>, msg: Message, locale: LocaleRef) -> crate::Result<()> {
bot.send_message(msg.chat.id, locale.help_command.as_ref())
bot.send_message(msg.chat.id, &locale.help_command)
.reply_markup(deletion_markup(locale))
.await?;
Ok(())

View File

@ -55,7 +55,7 @@ async fn get_master_pass(
}
let text = if failed.is_empty() {
locale.success.as_ref().to_owned()
locale.success.to_owned()
} else {
format!(
"{}:\n{}",

View File

@ -12,13 +12,13 @@ pub async fn menu(
let markup = menu_markup("get", user_id, &db).await?;
if markup.inline_keyboard.is_empty() {
bot.send_message(msg.chat.id, locale.no_accounts_found.as_ref())
bot.send_message(msg.chat.id, &locale.no_accounts_found)
.reply_markup(deletion_markup(locale))
.await?;
return Ok(());
}
bot.send_message(msg.chat.id, locale.choose_account.as_ref())
bot.send_message(msg.chat.id, &locale.choose_account)
.reply_markup(markup)
.await?;
Ok(())

View File

@ -22,7 +22,7 @@ async fn get_master_pass2(
if !hash.verify(master_pass.as_bytes()) {
ids.alter_message(
&bot,
locale.master_password_dont_match.as_ref(),
&locale.master_password_dont_match,
deletion_markup(locale),
None,
)
@ -43,7 +43,7 @@ async fn get_master_pass2(
.unwrap_or_default();
model.insert(&db, locale_type).await?;
ids.alter_message(&bot, locale.success.as_ref(), deletion_markup(locale), None)
ids.alter_message(&bot, &locale.success, deletion_markup(locale), None)
.await?;
Ok(())
@ -61,7 +61,7 @@ async fn get_master_pass(
) -> crate::Result<()> {
let hash = spawn_blocking(move || HashedBytes::new(master_pass.as_bytes())).await?;
ids.alter_message(&bot, locale.send_master_password_again.as_ref(), None, None)
ids.alter_message(&bot, &locale.send_master_password_again, None, None)
.await?;
change_state!(
@ -86,13 +86,13 @@ pub async fn set_master_pass(
) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
if MasterPass::exists(user_id, &db).await? {
bot.send_message(msg.chat.id, locale.master_password_is_set.as_ref())
bot.send_message(msg.chat.id, &locale.master_password_is_set)
.reply_markup(deletion_markup(locale))
.await?;
return Ok(());
}
let previous = bot
.send_message(msg.chat.id, locale.send_new_master_password.as_ref())
.send_message(msg.chat.id, &locale.send_new_master_password)
.await?;
change_state!(

View File

@ -3,7 +3,6 @@ use crate::prelude::*;
/// Handles /start command by sending the greeting message
#[inline]
pub async fn start(bot: Throttle<Bot>, msg: Message, locale: LocaleRef) -> crate::Result<()> {
bot.send_message(msg.chat.id, locale.start_command.as_ref())
.await?;
bot.send_message(msg.chat.id, &locale.start_command).await?;
Ok(())
}

View File

@ -3,7 +3,7 @@ use crate::prelude::*;
/// Handles the messages which weren't matched by any commands or states
#[inline]
pub async fn default(bot: Throttle<Bot>, msg: Message, locale: LocaleRef) -> crate::Result<()> {
bot.send_message(msg.chat.id, locale.unknown_command_use_help.as_ref())
bot.send_message(msg.chat.id, &locale.unknown_command_use_help)
.reply_markup(deletion_markup(locale))
.await?;
Ok(())

View File

@ -15,7 +15,7 @@ async fn notify_about_no_user_info(
state: State,
locale: LocaleRef,
) -> crate::Result<()> {
let text = locale.couldnt_get_user_info_send_again.as_ref();
let text = &locale.couldnt_get_user_info_send_again;
match state {
State::Start => {

View File

@ -26,75 +26,92 @@ impl LocaleStore {
}
}
#[derive(serde::Deserialize, derive_more::Display, derive_more::Deref)]
#[deref(forward)]
#[repr(transparent)]
pub struct LocaleString(Box<str>);
impl LocaleString {
pub const fn as_str(&self) -> &str {
&self.0
}
}
impl From<&LocaleString> for String {
fn from(value: &LocaleString) -> Self {
value.0.as_ref().to_owned()
}
}
#[derive(serde::Deserialize)]
pub struct Locale {
pub master_password_is_not_set: Box<str>,
pub hide_button: Box<str>,
pub change_name_button: Box<str>,
pub change_login_button: Box<str>,
pub change_password_button: Box<str>,
pub delete_account_button: Box<str>,
pub couldnt_get_user_info_send_again: Box<str>,
pub unknown_command_use_help: Box<str>,
pub help_command: Box<str>,
pub no_file_send: Box<str>,
pub file_too_large: Box<str>,
pub couldnt_get_file_name: Box<str>,
pub following_accounts_have_problems: Box<str>,
pub duplicate_names: Box<str>,
pub accounts_already_in_db: Box<str>,
pub invalid_fields: Box<str>,
pub fix_that_and_send_again: Box<str>,
pub error_downloading_file: Box<str>,
pub error_getting_account_names: Box<str>,
pub error_parsing_json_file: Box<str>,
pub successfully_canceled: Box<str>,
pub invalid_password: Box<str>,
pub couldnt_get_message_text: Box<str>,
pub invalid_file_name: Box<str>,
pub account_already_exists: Box<str>,
pub master_password_too_weak: Box<str>,
pub no_lowercase: Box<str>,
pub no_uppercase: Box<str>,
pub no_numbers: Box<str>,
pub master_pass_too_short: Box<str>,
pub change_master_password_and_send_again: Box<str>,
pub wrong_master_password: Box<str>,
pub invalid_login: Box<str>,
pub start_command: Box<str>,
pub master_password_dont_match: Box<str>,
pub success: Box<str>,
pub send_master_password_again: Box<str>,
pub master_password_is_set: Box<str>,
pub send_new_master_password: Box<str>,
pub no_accounts_found: Box<str>,
pub choose_account: Box<str>,
pub couldnt_create_following_accounts: Box<str>,
pub send_master_password: Box<str>,
pub something_went_wrong: Box<str>,
pub nothing_to_cancel: Box<str>,
pub send_account_name: Box<str>,
pub send_login: Box<str>,
pub error_deleting_message: Box<str>,
pub account_not_found: Box<str>,
pub send_new_name: Box<str>,
pub send_new_login: Box<str>,
pub send_new_password: Box<str>,
pub success_choose_account_to_view: Box<str>,
pub send_json_file: Box<str>,
pub send_master_pass_to_delete_everything: Box<str>,
pub everything_was_deleted: Box<str>,
pub send_password: Box<str>,
pub send_master_pass_to_delete_account: Box<str>,
pub no_special_characters: Box<str>,
pub invalid_name: Box<str>,
pub decrypt_button: Box<str>,
pub delete_message_button: Box<str>,
pub menu_button: Box<str>,
pub choose_language: Box<str>,
word_name: Box<str>,
word_login: Box<str>,
word_password: Box<str>,
pub master_password_is_not_set: LocaleString,
pub hide_button: LocaleString,
pub change_name_button: LocaleString,
pub change_login_button: LocaleString,
pub change_password_button: LocaleString,
pub delete_account_button: LocaleString,
pub couldnt_get_user_info_send_again: LocaleString,
pub unknown_command_use_help: LocaleString,
pub help_command: LocaleString,
pub no_file_send: LocaleString,
pub file_too_large: LocaleString,
pub couldnt_get_file_name: LocaleString,
pub following_accounts_have_problems: LocaleString,
pub duplicate_names: LocaleString,
pub accounts_already_in_db: LocaleString,
pub invalid_fields: LocaleString,
pub fix_that_and_send_again: LocaleString,
pub error_downloading_file: LocaleString,
pub error_getting_account_names: LocaleString,
pub error_parsing_json_file: LocaleString,
pub successfully_canceled: LocaleString,
pub invalid_password: LocaleString,
pub couldnt_get_message_text: LocaleString,
pub invalid_file_name: LocaleString,
pub account_already_exists: LocaleString,
pub master_password_too_weak: LocaleString,
pub no_lowercase: LocaleString,
pub no_uppercase: LocaleString,
pub no_numbers: LocaleString,
pub master_pass_too_short: LocaleString,
pub change_master_password_and_send_again: LocaleString,
pub wrong_master_password: LocaleString,
pub invalid_login: LocaleString,
pub start_command: LocaleString,
pub master_password_dont_match: LocaleString,
pub success: LocaleString,
pub send_master_password_again: LocaleString,
pub master_password_is_set: LocaleString,
pub send_new_master_password: LocaleString,
pub no_accounts_found: LocaleString,
pub choose_account: LocaleString,
pub couldnt_create_following_accounts: LocaleString,
pub send_master_password: LocaleString,
pub something_went_wrong: LocaleString,
pub nothing_to_cancel: LocaleString,
pub send_account_name: LocaleString,
pub send_login: LocaleString,
pub error_deleting_message: LocaleString,
pub account_not_found: LocaleString,
pub send_new_name: LocaleString,
pub send_new_login: LocaleString,
pub send_new_password: LocaleString,
pub success_choose_account_to_view: LocaleString,
pub send_json_file: LocaleString,
pub send_master_pass_to_delete_everything: LocaleString,
pub everything_was_deleted: LocaleString,
pub send_password: LocaleString,
pub send_master_pass_to_delete_account: LocaleString,
pub no_special_characters: LocaleString,
pub invalid_name: LocaleString,
pub decrypt_button: LocaleString,
pub delete_message_button: LocaleString,
pub menu_button: LocaleString,
pub choose_language: LocaleString,
word_name: LocaleString,
word_login: LocaleString,
word_password: LocaleString,
}
impl Locale {

View File

@ -20,9 +20,7 @@ macro_rules! first_handler {
dialogue: MainDialogue,
locale: LocaleRef,
) -> $crate::Result<()> {
let previous = bot
.send_message(msg.chat.id, locale.$message.as_ref())
.await?;
let previous = bot.send_message(msg.chat.id, &locale.$message).await?;
$crate::change_state!(dialogue, &previous, (), $next_state, $next_func);
@ -45,7 +43,7 @@ macro_rules! handler {
locale: LocaleRef,
$($param: $type),*
) -> $crate::Result<()> {
ids.alter_message(&bot, locale.$message.as_ref(), None, None).await?;
ids.alter_message(&bot, &locale.$message, None, None).await?;
$crate::change_state!(dialogue, ids, ($($param),*), $next_state, $next_func);

View File

@ -57,23 +57,23 @@ pub fn account_markup(name: &str, is_encrypted: bool, locale: LocaleRef) -> Inli
let hash = std::str::from_utf8(&hash).unwrap();
let encryption_button = if is_encrypted {
(locale.decrypt_button.as_ref(), "decrypt")
(&locale.decrypt_button, "decrypt")
} else {
(locale.hide_button.as_ref(), "get")
(&locale.hide_button, "get")
};
let main_buttons = [
(locale.change_name_button.as_ref(), "an"),
(locale.change_login_button.as_ref(), "al"),
(locale.change_password_button.as_ref(), "ap"),
(locale.delete_account_button.as_ref(), "delete0"),
(&locale.change_name_button, "an"),
(&locale.change_login_button, "al"),
(&locale.change_password_button, "ap"),
(&locale.delete_account_button, "delete0"),
encryption_button,
]
.into_iter()
.map(|(text, command)| make_button(text, command, hash))
.chunks(2);
let menu_button = InlineKeyboardButton::callback(locale.menu_button.as_ref(), "get_menu");
let menu_button = InlineKeyboardButton::callback(&locale.menu_button, "get_menu");
InlineKeyboardMarkup::new(&main_buttons).append_row([menu_button])
}
@ -91,7 +91,6 @@ pub fn language_markup() -> InlineKeyboardMarkup {
/// This markup should be added for all messages that won't be deleted afterwards
#[inline]
pub fn deletion_markup(locale: LocaleRef) -> InlineKeyboardMarkup {
let button =
InlineKeyboardButton::callback(locale.delete_message_button.as_ref(), "delete_message");
let button = InlineKeyboardButton::callback(&locale.delete_message_button, "delete_message");
InlineKeyboardMarkup::new([[button]])
}

View File

@ -38,7 +38,7 @@ async fn notify_about_no_master_pass(
}
bot.send_message(
update.chat_id().unwrap(),
locale.master_password_is_not_set.as_ref(),
&locale.master_password_is_not_set,
)
.reply_markup(deletion_markup(locale))
.await?;

View File

@ -30,7 +30,7 @@ where
let Some(text) = msg.text().map(str::trim) else {
handler
.previous
.alter_message(&bot, locale.couldnt_get_message_text.as_ref(), None, None)
.alter_message(&bot, &locale.couldnt_get_message_text, None, None)
.await?;
return Ok(());
};
@ -41,7 +41,7 @@ where
.previous
.alter_message(
&bot,
locale.successfully_canceled.as_ref(),
&locale.successfully_canceled,
deletion_markup(locale),
None,
)

View File

@ -9,7 +9,7 @@ async fn check_login(
) -> crate::Result<Option<String>> {
let is_valid = validate_field(login);
if !is_valid {
return Ok(Some(locale.invalid_login.as_ref().into()));
return Ok(Some(locale.invalid_login.to_owned()));
}
Ok(None)
}

View File

@ -14,7 +14,7 @@ async fn check_master_pass(
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let Some(model) = MasterPass::get(user_id, db).await? else {
error!("User was put into the GetMasterPass state with no master password set");
return Ok(Some(locale.master_password_is_not_set.as_ref().into()));
return Ok(Some(locale.master_password_is_not_set.to_owned()));
};
let is_valid = {
@ -24,7 +24,7 @@ async fn check_master_pass(
};
if !is_valid {
return Ok(Some(locale.wrong_master_password.as_ref().into()));
return Ok(Some(locale.wrong_master_password.to_owned()));
}
Ok(None)
}

View File

@ -8,7 +8,7 @@ fn process_validity(validity: PasswordValidity, locale: LocaleRef) -> Result<(),
return Ok(());
}
let mut error_text = locale.master_password_too_weak.as_ref().to_owned();
let mut error_text = locale.master_password_too_weak.to_owned();
if validity.contains(PasswordValidity::NO_LOWERCASE) {
write!(error_text, "\n* {}", locale.no_lowercase).unwrap();

View File

@ -11,9 +11,9 @@ async fn check_new_account_name(
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
if Account::exists(user_id, name, db).await? {
Ok(Some(locale.account_already_exists.as_ref().into()))
Ok(Some(locale.account_already_exists.to_owned()))
} else if !validate_field(name) {
Ok(Some(locale.invalid_name.as_ref().into()))
Ok(Some(locale.invalid_name.to_owned()))
} else {
Ok(None)
}

View File

@ -9,7 +9,7 @@ async fn check_password(
) -> crate::Result<Option<String>> {
let is_valid = validate_field(password);
if !is_valid {
return Ok(Some(locale.invalid_password.as_ref().into()));
return Ok(Some(locale.invalid_password.to_owned()));
}
Ok(None)
}

View File

@ -18,7 +18,7 @@ enum InvalidDocument {
}
impl InvalidDocument {
const fn into_str(self, locale: LocaleRef) -> &'static str {
fn into_str(self, locale: LocaleRef) -> &'static str {
match self {
Self::NoFileSend => &locale.no_file_send,
Self::FileTooLarge => &locale.file_too_large,
@ -102,7 +102,7 @@ fn process_accounts(
return Ok(Ok(()));
}
let mut error_text = locale.following_accounts_have_problems.as_ref().to_owned();
let mut error_text = locale.following_accounts_have_problems.to_owned();
if !duplicates.is_empty() {
write!(
@ -162,11 +162,11 @@ async fn user_from_document(
let file = &validate_document(document)
.map_err(|err| err.into_str(locale))?
.file;
let data = download_file(bot, file).map_err(|_| locale.error_downloading_file.as_ref());
let data = download_file(bot, file).map_err(|_| locale.error_downloading_file.as_str());
let existing_names = Account::get_names(user_id, db)
.try_collect()
.map_err(|_| locale.error_getting_account_names.as_ref());
.map_err(|_| locale.error_getting_account_names.as_str());
try_join!(data, existing_names)?
};
@ -201,7 +201,7 @@ pub async fn get_user(
.previous
.alter_message(
&bot,
locale.successfully_canceled.as_ref(),
&locale.successfully_canceled,
deletion_markup(locale),
None,
)

View File

@ -1,12 +1,12 @@
use crate::prelude::*;
use futures::{future::BoxFuture, TryFutureExt};
use futures::future::BoxFuture;
use std::{mem, sync::Arc};
use teloxide::{
errors::{ApiError, RequestError},
requests::HasPayload,
types::{InlineKeyboardMarkup, MessageId, ParseMode},
RequestError,
};
use tokio::{sync::Mutex, try_join};
use tokio::{join, sync::Mutex};
#[derive(Clone, Copy)]
pub struct MessageIds(pub ChatId, pub MessageId);
@ -14,10 +14,10 @@ pub struct MessageIds(pub ChatId, pub MessageId);
impl MessageIds {
// Tries to delete the message while ignoring API errors
#[inline]
pub async fn delete(self, bot: &Throttle<Bot>) -> crate::Result<()> {
pub async fn delete(self, bot: &Throttle<Bot>) -> Result<(), RequestError> {
match bot.delete_message(self.0, self.1).await {
Ok(_) | Err(RequestError::Api(_)) => Ok(()),
Err(err) => Err(err.into()),
Err(err) => Err(err),
}
}
@ -29,27 +29,27 @@ impl MessageIds {
text: impl Into<String> + Send,
markup: impl Into<Option<InlineKeyboardMarkup>> + Send,
parse_mode: impl Into<Option<ParseMode>> + Send,
) -> crate::Result<()> {
let mut edit = bot.edit_message_text(self.0, self.1, text);
edit.parse_mode = parse_mode.into();
edit.reply_markup = markup.into();
) -> Result<(), RequestError> {
let mut edit_request = bot.edit_message_text(self.0, self.1, text);
edit_request.parse_mode = parse_mode.into();
edit_request.reply_markup = markup.into();
match edit.send_ref().await {
Ok(_) => return Ok(()),
match edit_request.send_ref().await {
Ok(_) | Err(RequestError::Api(ApiError::MessageNotModified)) => return Ok(()),
Err(RequestError::Api(_)) => (),
Err(err) => return Err(err.into()),
Err(err) => return Err(err),
};
let send = {
let mut send_request = bot.send_message(self.0, mem::take(&mut edit.text));
let payload = send_request.payload_mut();
payload.parse_mode = edit.parse_mode;
payload.reply_markup = edit.reply_markup.take().map(Into::into);
send_request.send().map_err(Into::into)
};
let mut send_request = bot.send_message(self.0, mem::take(&mut edit_request.text));
let payload = send_request.payload_mut();
payload.parse_mode = edit_request.parse_mode;
payload.reply_markup = edit_request.reply_markup.take().map(Into::into);
let msg = try_join!(self.delete(bot), send)?.1;
*self = Self::from(&msg);
let result = join!(self.delete(bot), send_request.send());
if let Err(err) = result.0 {
log::error!("{err}");
}
*self = Self::from(&result.1?);
Ok(())
}
}