Moved dispatching into a sepparate module

This commit is contained in:
StNicolay 2024-05-07 12:29:13 +03:00
parent 5871943c01
commit 913d90f077
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
9 changed files with 119 additions and 119 deletions

66
Cargo.lock generated
View File

@ -57,9 +57,9 @@ checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f"
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.82" version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" checksum = "25bdb32cbbdce2b519a9cd7df3a678443100e265d5e25ca763b7572a5104f5f3"
dependencies = [ dependencies = [
"backtrace", "backtrace",
] ]
@ -175,9 +175,9 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.96" version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "065a29261d53ba54260972629f9ca6bffa69bac13cd1fed61420f7fa68b9f8bd" checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
@ -578,7 +578,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.60", "syn 2.0.61",
] ]
[[package]] [[package]]
@ -623,9 +623,9 @@ dependencies = [
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.14" version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
@ -1174,9 +1174,9 @@ dependencies = [
[[package]] [[package]]
name = "paste" name = "paste"
version = "1.0.14" version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]] [[package]]
name = "pbkdf2" name = "pbkdf2"
@ -1222,7 +1222,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.60", "syn 2.0.61",
] ]
[[package]] [[package]]
@ -1307,9 +1307,9 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.81" version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d1597b0c024618f09a9c3b8655b7e430397a36d23fdafec26d6965e9eec3eba" checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -1584,9 +1584,9 @@ dependencies = [
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.17" version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]] [[package]]
name = "salsa20" name = "salsa20"
@ -1627,9 +1627,9 @@ dependencies = [
[[package]] [[package]]
name = "semver" name = "semver"
version = "1.0.22" version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca" checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
[[package]] [[package]]
name = "serde" name = "serde"
@ -1648,7 +1648,7 @@ checksum = "856f046b9400cee3c8c94ed572ecdb752444c24528c035cd35882aad6f492bcb"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.60", "syn 2.0.61",
] ]
[[package]] [[package]]
@ -2042,9 +2042,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.60" version = "2.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "909518bc7b1c9b779f1bbf07f2929d35af9f0f37e47c6e9ef7f9dddc1e1821f3" checksum = "c993ed8ccba56ae856363b1845da7266a7cb78e1d146c8a32d54b45a8b831fc9"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2174,22 +2174,22 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.59" version = "1.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0126ad08bff79f29fc3ae6a55cc72352056dfff61e3ff8bb7129476d44b23aa" checksum = "579e9083ca58dd9dcf91a9923bb9054071b9ebbd800b342194c9feb0ee89fc18"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.59" version = "1.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1cd413b5d558b4c5bf3680e324a6fa5014e7b7c067a51e69dbdf47eb7148b66" checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.60", "syn 2.0.61",
] ]
[[package]] [[package]]
@ -2244,7 +2244,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.60", "syn 2.0.61",
] ]
[[package]] [[package]]
@ -2307,7 +2307,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.60", "syn 2.0.61",
] ]
[[package]] [[package]]
@ -2525,7 +2525,7 @@ dependencies = [
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.60", "syn 2.0.61",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -2559,7 +2559,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.60", "syn 2.0.61",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -2782,22 +2782,22 @@ dependencies = [
[[package]] [[package]]
name = "zerocopy" name = "zerocopy"
version = "0.7.33" version = "0.7.34"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "087eca3c1eaf8c47b94d02790dd086cd594b912d2043d4de4bfdd466b3befb7c" checksum = "ae87e3fcd617500e5d106f0380cf7b77f3c6092aae37191433159dda23cfb087"
dependencies = [ dependencies = [
"zerocopy-derive", "zerocopy-derive",
] ]
[[package]] [[package]]
name = "zerocopy-derive" name = "zerocopy-derive"
version = "0.7.33" version = "0.7.34"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f4b6c273f496d8fd4eaf18853e6b448760225dc030ff2c485a786859aea6393" checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.60", "syn 2.0.61",
] ]
[[package]] [[package]]

80
src/dispatching.rs Normal file
View File

@ -0,0 +1,80 @@
mod default;
mod delete_mesage_handler;
mod filter_user_info;
mod master_password_check;
use crate::{
callbacks::{self, CallbackCommand},
commands::{self, Command},
errors, locales,
prelude::*,
state::{self, State},
};
use teloxide::{
adaptors::throttle::Limits,
dispatching::{dialogue::InMemStorage, filter_command, DefaultKey},
dptree::{case, deps},
};
pub fn get_dispatcher(
token: String,
db: Pool,
) -> Dispatcher<Throttle<Bot>, crate::Error, DefaultKey> {
let bot = Bot::new(token).throttle(Limits::default());
let command_handler = filter_command::<Command, _>()
.branch(case![Command::Start].endpoint(commands::start))
.branch(case![Command::Help].endpoint(commands::help))
.branch(case![Command::SetMasterPass].endpoint(commands::set_master_pass))
.branch(case![Command::GenPassword].endpoint(commands::gen_password))
.branch(case![Command::Cancel].endpoint(commands::cancel))
// This branch filters out the users that don't have a master password set
.branch(master_password_check::get_handler())
.branch(case![Command::Menu].endpoint(commands::menu))
.branch(case![Command::AddAccount].endpoint(commands::add_account))
.branch(case![Command::GetAccount].endpoint(commands::get_account))
.branch(case![Command::GetAccounts].endpoint(commands::get_accounts))
.branch(case![Command::Delete].endpoint(commands::delete))
.branch(case![Command::DeleteAll].endpoint(commands::delete_all))
.branch(case![Command::Export].endpoint(commands::export))
.branch(case![Command::Import].endpoint(commands::import))
.chain(case![Command::ChangeLanguage].endpoint(commands::change_language));
let message_handler = Update::filter_message()
.map_async(delete_mesage_handler::delete_message)
// Filters out the messages without user information
.branch(filter_user_info::get_handler())
.branch(case![State::GetNewName(next)].endpoint(state::get_new_name))
.branch(case![State::GetMasterPass(next)].endpoint(state::get_master_pass))
.branch(case![State::GetNewMasterPass(next)].endpoint(state::get_new_master_pass))
.branch(case![State::GetLogin(next)].endpoint(state::get_login))
.branch(case![State::GetPassword(next)].endpoint(state::get_password))
.branch(case![State::GetUser(next)].endpoint(state::get_user))
.branch(command_handler)
.endpoint(default::default);
let callback_handler = Update::filter_callback_query()
.filter_map(CallbackCommand::from_query)
.branch(case![CallbackCommand::DeleteMessage].endpoint(callbacks::delete_message))
.branch(master_password_check::get_handler())
.branch(case![CallbackCommand::GetMenu].endpoint(callbacks::get_menu))
.branch(case![CallbackCommand::Get(hash)].endpoint(callbacks::get))
.branch(case![CallbackCommand::Decrypt(hash)].endpoint(callbacks::decrypt))
.branch(
case![CallbackCommand::DeleteAccount { name, is_command }].endpoint(callbacks::delete),
)
.branch(case![CallbackCommand::Alter(hash, field)].endpoint(callbacks::alter))
.chain(case![CallbackCommand::ChangeLocale(locale)].endpoint(callbacks::change_locale));
let handler = dptree::entry()
.map_async(locales::Locale::from_update)
.enter_dialogue::<Update, InMemStorage<State>, State>()
.branch(message_handler)
.chain(callback_handler);
Dispatcher::builder(bot, handler)
.dependencies(deps![db, InMemStorage::<State>::new()])
.error_handler(std::sync::Arc::from(errors::ErrorHandler))
.enable_ctrlc_handler()
.build()
}

View File

@ -8,7 +8,6 @@ fn has_no_user_info(msg: Message) -> bool {
} }
#[inline] #[inline]
#[allow(clippy::significant_drop_tightening)]
async fn notify_about_no_user_info( async fn notify_about_no_user_info(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
@ -28,15 +27,17 @@ async fn notify_about_no_user_info(
| State::GetNewMasterPass(handler) | State::GetNewMasterPass(handler)
| State::GetLogin(handler) | State::GetLogin(handler)
| State::GetPassword(handler) => { | State::GetPassword(handler) => {
let mut handler = handler.lock().await;
handler handler
.lock()
.await
.previous .previous
.alter_message(&bot, text, None, None) .alter_message(&bot, text, None, None)
.await?; .await?;
} }
State::GetUser(handler) => { State::GetUser(handler) => {
let mut handler = handler.lock().await;
handler handler
.lock()
.await
.previous .previous
.alter_message(&bot, text, None, None) .alter_message(&bot, text, None, None)
.await?; .await?;

View File

@ -1,99 +1,18 @@
mod callbacks; mod callbacks;
mod commands; mod commands;
mod cryptography; mod cryptography;
mod default; mod dispatching;
mod delete_mesage_handler;
mod entity; mod entity;
mod errors; mod errors;
mod filter_user_info;
mod locales; mod locales;
mod macros; mod macros;
mod markups; mod markups;
mod master_password_check;
mod models; mod models;
mod prelude; mod prelude;
mod state; mod state;
use anyhow::{Error, Result}; use anyhow::{Error, Result};
fn get_dispatcher(
token: String,
db: prelude::Pool,
) -> teloxide::prelude::Dispatcher<
teloxide::adaptors::Throttle<teloxide::Bot>,
crate::Error,
teloxide::dispatching::DefaultKey,
> {
use callbacks::CallbackCommand;
use commands::Command;
use state::State;
use teloxide::{
adaptors::throttle::Limits,
dispatching::dialogue::InMemStorage,
dptree::{case, deps},
filter_command,
prelude::*,
};
let bot = Bot::new(token).throttle(Limits::default());
let command_handler = filter_command::<Command, _>()
.branch(case![Command::Start].endpoint(commands::start))
.branch(case![Command::Help].endpoint(commands::help))
.branch(case![Command::SetMasterPass].endpoint(commands::set_master_pass))
.branch(case![Command::GenPassword].endpoint(commands::gen_password))
.branch(case![Command::Cancel].endpoint(commands::cancel))
// This branch filters out the users that don't have a master password set
.branch(master_password_check::get_handler())
.branch(case![Command::Menu].endpoint(commands::menu))
.branch(case![Command::AddAccount].endpoint(commands::add_account))
.branch(case![Command::GetAccount].endpoint(commands::get_account))
.branch(case![Command::GetAccounts].endpoint(commands::get_accounts))
.branch(case![Command::Delete].endpoint(commands::delete))
.branch(case![Command::DeleteAll].endpoint(commands::delete_all))
.branch(case![Command::Export].endpoint(commands::export))
.branch(case![Command::Import].endpoint(commands::import))
.branch(case![Command::ChangeLanguage].endpoint(commands::change_language));
let message_handler = Update::filter_message()
.map_async(delete_mesage_handler::delete_message)
// Filters out the messages without user information
.branch(filter_user_info::get_handler())
.branch(case![State::GetNewName(next)].endpoint(state::get_new_name))
.branch(case![State::GetMasterPass(next)].endpoint(state::get_master_pass))
.branch(case![State::GetNewMasterPass(next)].endpoint(state::get_new_master_pass))
.branch(case![State::GetLogin(next)].endpoint(state::get_login))
.branch(case![State::GetPassword(next)].endpoint(state::get_password))
.branch(case![State::GetUser(next)].endpoint(state::get_user))
.branch(command_handler)
.endpoint(default::default);
let callback_handler = Update::filter_callback_query()
.filter_map(CallbackCommand::from_query)
.branch(case![CallbackCommand::DeleteMessage].endpoint(callbacks::delete_message))
.branch(master_password_check::get_handler())
.branch(case![CallbackCommand::GetMenu].endpoint(callbacks::get_menu))
.branch(case![CallbackCommand::Get(hash)].endpoint(callbacks::get))
.branch(case![CallbackCommand::Decrypt(hash)].endpoint(callbacks::decrypt))
.branch(
case![CallbackCommand::DeleteAccount { name, is_command }].endpoint(callbacks::delete),
)
.branch(case![CallbackCommand::Alter(hash, field)].endpoint(callbacks::alter))
.branch(case![CallbackCommand::ChangeLocale(locale)].endpoint(callbacks::change_locale));
let handler = dptree::entry()
.map_async(locales::Locale::from_update)
.enter_dialogue::<Update, InMemStorage<State>, State>()
.branch(message_handler)
.branch(callback_handler);
Dispatcher::builder(bot, handler)
.dependencies(deps![db, InMemStorage::<State>::new()])
.error_handler(std::sync::Arc::from(errors::ErrorHandler))
.enable_ctrlc_handler()
.build()
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
use std::env; use std::env;
@ -109,6 +28,6 @@ async fn main() -> Result<()> {
entity::migrate(&pool).await?; entity::migrate(&pool).await?;
get_dispatcher(token, pool).dispatch().await; dispatching::get_dispatcher(token, pool).dispatch().await;
Ok(()) Ok(())
} }