Removed library crate for pass_manager

This commit is contained in:
StNicolay 2023-06-23 11:38:54 +03:00
parent 9ef17f31fe
commit f1228ed80f
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
4 changed files with 75 additions and 77 deletions

View File

@ -1,71 +0,0 @@
mod callbacks;
mod commands;
mod default;
mod errors;
mod markups;
mod master_password_check;
mod models;
mod state;
mod utils;
use anyhow::{Error, Result};
use commands::Command;
use futures::future::BoxFuture as PinnedFuture;
use sea_orm::prelude::*;
use state::{Handler, MainDialogue, State};
use teloxide::{
adaptors::{throttle::Limits, Throttle},
dispatching::dialogue::InMemStorage,
filter_command,
prelude::*,
};
pub fn get_dispatcher(
token: String,
db: DatabaseConnection,
) -> Dispatcher<Throttle<Bot>, crate::Error, teloxide::dispatching::DefaultKey> {
use dptree::{case, deps};
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::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));
let message_handler = Update::filter_message()
.map_async(utils::delete_message)
.enter_dialogue::<Update, InMemStorage<State>, State>()
.branch(case![State::GetExistingName(next)].endpoint(state::get_existing_name))
.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().chain(callbacks::delete_message::get_handler());
let handler = dptree::entry()
.branch(message_handler)
.branch(callback_handler);
Dispatcher::builder(bot, handler)
.dependencies(deps![db, InMemStorage::<State>::new()])
.enable_ctrlc_handler()
.build()
}

View File

@ -1,9 +1,76 @@
use anyhow::Result;
mod callbacks;
mod commands;
mod default;
mod errors;
mod markups;
mod master_password_check;
mod models;
mod state;
mod utils;
use anyhow::{Error, Result};
use commands::Command;
use dotenv::dotenv;
use migration::{Migrator, MigratorTrait};
use pass_manager::get_dispatcher;
use sea_orm::Database;
use sea_orm::{prelude::*, Database};
use state::{Handler, MainDialogue, State};
use std::env;
use teloxide::{
adaptors::{throttle::Limits, Throttle},
dispatching::dialogue::InMemStorage,
filter_command,
prelude::*,
};
fn get_dispatcher(
token: String,
db: DatabaseConnection,
) -> Dispatcher<Throttle<Bot>, crate::Error, teloxide::dispatching::DefaultKey> {
use dptree::{case, deps};
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::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));
let message_handler = Update::filter_message()
.map_async(utils::delete_message)
.enter_dialogue::<Update, InMemStorage<State>, State>()
.branch(case![State::GetExistingName(next)].endpoint(state::get_existing_name))
.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().chain(callbacks::delete_message::get_handler());
let handler = dptree::entry()
.branch(message_handler)
.branch(callback_handler);
Dispatcher::builder(bot, handler)
.dependencies(deps![db, InMemStorage::<State>::new()])
.enable_ctrlc_handler()
.build()
}
#[tokio::main]
async fn main() -> Result<()> {

View File

@ -1,4 +1,5 @@
use crate::{errors::HandlerUsed, markups::deletion_markup, utils::delete_optional, PinnedFuture};
use crate::{errors::HandlerUsed, markups::deletion_markup, utils::delete_optional};
use futures::future::BoxFuture;
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
@ -19,7 +20,7 @@ where
&'a Message,
&'a DatabaseConnection,
&'a str,
) -> PinnedFuture<'a, crate::Result<Option<Message>>>,
) -> BoxFuture<'a, crate::Result<Option<Message>>>,
{
let mut handler = next.lock().await;
delete_optional(&bot, handler.previous.as_ref()).await;

View File

@ -1,4 +1,5 @@
use crate::MainDialogue;
use futures::future::BoxFuture;
use sea_orm::prelude::*;
use std::{future::Future, sync::Arc};
use teloxide::{adaptors::Throttle, prelude::*};
@ -11,7 +12,7 @@ type DynHanlder<T> = Box<
DatabaseConnection,
MainDialogue,
T,
) -> crate::PinnedFuture<'static, crate::Result<()>>
) -> BoxFuture<'static, crate::Result<()>>
+ Send,
>;