Added a prelude to reduce the amount of imports

This commit is contained in:
StNicolay 2023-06-29 17:33:26 +03:00
parent 580641bcf4
commit c0fcb41575
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
32 changed files with 78 additions and 155 deletions

12
Cargo.lock generated
View File

@ -1596,9 +1596,9 @@ dependencies = [
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.28" version = "1.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488" checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@ -1891,9 +1891,9 @@ dependencies = [
[[package]] [[package]]
name = "rustls-pemfile" name = "rustls-pemfile"
version = "1.0.2" version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d194b56d58803a43635bdc398cd17e383d6f71f9182b9a192c127ca42494a59b" checksum = "2d3987094b1d07b653b7dfdc3f70ce9a1da9c51ac18c1b06b662e4f9a0e9f4b2"
dependencies = [ dependencies = [
"base64", "base64",
] ]
@ -2997,9 +2997,9 @@ dependencies = [
[[package]] [[package]]
name = "windows-targets" name = "windows-targets"
version = "0.48.0" version = "0.48.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm",
"windows_aarch64_msvc", "windows_aarch64_msvc",

View File

@ -1,5 +1,5 @@
use crate::markups::deletion_markup; use crate::prelude::*;
use teloxide::{adaptors::Throttle, dispatching::DpHandlerDescription, prelude::*}; use teloxide::{dispatching::DpHandlerDescription, dptree::Handler};
/// Deletes the message from the callback /// Deletes the message from the callback
async fn run(bot: Throttle<Bot>, q: CallbackQuery) -> crate::Result<()> { async fn run(bot: Throttle<Bot>, q: CallbackQuery) -> crate::Result<()> {

View File

@ -1,9 +1,4 @@
use crate::{ use crate::prelude::*;
errors::NoUserInfo, markups::deletion_markup, models::DecryptedAccount, Handler, MainDialogue,
State,
};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
/// Gets the name of the master password, encryptes the account and adds it to the DB /// Gets the name of the master password, encryptes the account and adds it to the DB

View File

@ -1,5 +1,4 @@
use crate::markups::deletion_markup; use crate::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
/// Handles /cancel command when there's no active state /// Handles /cancel command when there's no active state
pub async fn cancel(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> { pub async fn cancel(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> {

View File

@ -1,11 +1,4 @@
use crate::{ use crate::prelude::*;
errors::NoUserInfo,
markups::{self, deletion_markup},
Handler, MainDialogue, State,
};
use entity::prelude::*;
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
/// Gets the master password and deletes the account. /// Gets the master password and deletes the account.
/// Although it doesn't use the master password, we get it to be sure that it's the user who used that command /// Although it doesn't use the master password, we get it to be sure that it's the user who used that command
@ -53,7 +46,7 @@ pub async fn delete(
db: DatabaseConnection, db: DatabaseConnection,
) -> crate::Result<()> { ) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let markup = markups::account_markup(user_id, &db).await?; let markup = account_markup(user_id, &db).await?;
if markup.keyboard.is_empty() { if markup.keyboard.is_empty() {
bot.send_message(msg.chat.id, "No accounts found") bot.send_message(msg.chat.id, "No accounts found")
.reply_markup(deletion_markup()) .reply_markup(deletion_markup())

View File

@ -1,7 +1,4 @@
use crate::{errors::NoUserInfo, markups::deletion_markup, Handler, MainDialogue, State}; use crate::prelude::*;
use entity::prelude::*;
use sea_orm::DatabaseConnection;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::join; use tokio::join;
/// Gets the master password, deletes the accounts and the master password from DB. /// Gets the master password, deletes the accounts and the master password from DB.

View File

@ -1,15 +1,7 @@
use crate::{ use crate::prelude::*;
errors::NoUserInfo,
markups::deletion_markup,
models::{DecryptedAccount, User},
Handler, MainDialogue, State,
};
use entity::prelude::*;
use futures::TryStreamExt;
use parking_lot::Mutex; use parking_lot::Mutex;
use sea_orm::DatabaseConnection;
use std::sync::Arc; use std::sync::Arc;
use teloxide::{adaptors::Throttle, prelude::*, types::InputFile}; use teloxide::types::InputFile;
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
/// Decryptes the account on a worker thread and adds it to the accounts vector /// Decryptes the account on a worker thread and adds it to the accounts vector

View File

@ -1,8 +1,8 @@
use crate::markups::deletion_markup; use crate::prelude::*;
use arrayvec::ArrayString; use arrayvec::ArrayString;
use cryptography::passwords::generate_passwords; use cryptography::passwords::generate_passwords;
use std::fmt::Write; use std::fmt::Write;
use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode}; use teloxide::types::ParseMode;
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
const MESSAGE_HEADER: &str = "Passwords:"; const MESSAGE_HEADER: &str = "Passwords:";

View File

@ -1,12 +1,5 @@
use crate::{ use crate::prelude::*;
errors::NoUserInfo, use teloxide::types::ParseMode;
markups::{self, deletion_markup},
Handler, MainDialogue, State,
};
use cryptography::prelude::*;
use entity::prelude::*;
use sea_orm::DatabaseConnection;
use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode};
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
/// Gets the master password, decryptes the account and sends it to the user with copyable fields /// Gets the master password, decryptes the account and sends it to the user with copyable fields
@ -68,7 +61,7 @@ pub async fn get_account(
db: DatabaseConnection, db: DatabaseConnection,
) -> crate::Result<()> { ) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let markup = markups::account_markup(user_id, &db).await?; let markup = account_markup(user_id, &db).await?;
if markup.keyboard.is_empty() { if markup.keyboard.is_empty() {
bot.send_message(msg.chat.id, "No accounts found") bot.send_message(msg.chat.id, "No accounts found")
.reply_markup(deletion_markup()) .reply_markup(deletion_markup())

View File

@ -1,9 +1,7 @@
use crate::{errors::NoUserInfo, markups::deletion_markup}; use crate::prelude::*;
use entity::prelude::*; use futures::future;
use futures::{future, TryStreamExt};
use sea_orm::DatabaseConnection;
use std::fmt::Write; use std::fmt::Write;
use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode}; use teloxide::types::ParseMode;
/// Handles /get_accounts command by sending the list of copyable account names to the user /// Handles /get_accounts command by sending the list of copyable account names to the user
pub async fn get_accounts( pub async fn get_accounts(

View File

@ -1,5 +1,5 @@
use crate::{markups::deletion_markup, Command}; use crate::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*, utils::command::BotCommands}; use teloxide::utils::command::BotCommands;
/// Handles the help command by sending the passwords descryptions /// Handles the help command by sending the passwords descryptions
pub async fn help(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> { pub async fn help(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> {

View File

@ -1,15 +1,8 @@
use crate::{ use crate::prelude::*;
errors::NoUserInfo, use futures::stream;
markups::deletion_markup,
models::{DecryptedAccount, User},
Handler, MainDialogue, State,
};
use futures::{stream, StreamExt};
use itertools::Itertools; use itertools::Itertools;
use parking_lot::Mutex; use parking_lot::Mutex;
use sea_orm::prelude::*;
use std::sync::Arc; use std::sync::Arc;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
/// Ecryptes the account and adds it to the database /// Ecryptes the account and adds it to the database

View File

@ -25,6 +25,7 @@ pub use help::help;
pub use import::import; pub use import::import;
pub use set_master_pass::set_master_pass; pub use set_master_pass::set_master_pass;
pub use start::start; pub use start::start;
use teloxide::macros::BotCommands; use teloxide::macros::BotCommands;
#[derive(BotCommands, Clone, Copy)] #[derive(BotCommands, Clone, Copy)]

View File

@ -1,9 +1,5 @@
use crate::{errors::NoUserInfo, markups::deletion_markup, Handler, MainDialogue, State}; use crate::prelude::*;
use cryptography::prelude::*; use tokio::task::spawn_blocking;
use entity::prelude::*;
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::task;
/// Actually sets the master password /// Actually sets the master password
async fn get_master_pass( async fn get_master_pass(
@ -15,7 +11,7 @@ async fn get_master_pass(
) -> crate::Result<()> { ) -> crate::Result<()> {
dialogue.exit().await?; dialogue.exit().await?;
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let model = task::spawn_blocking(move || { let model = spawn_blocking(move || {
master_pass::ActiveModel::from_unencrypted(user_id, &master_password) master_pass::ActiveModel::from_unencrypted(user_id, &master_password)
}) })
.await?; .await?;

View File

@ -1,4 +1,4 @@
use teloxide::{adaptors::Throttle, prelude::*}; use crate::prelude::*;
/// Handles /start command by sending the greeting message /// Handles /start command by sending the greeting message
pub async fn start(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> { pub async fn start(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> {

View File

@ -1,5 +1,4 @@
use crate::markups::deletion_markup; use crate::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
/// Handles the messages which weren't matched by any commands or states /// Handles the messages which weren't matched by any commands or states
pub async fn default(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> { pub async fn default(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> {

View File

@ -5,22 +5,17 @@ mod errors;
mod markups; mod markups;
mod master_password_check; mod master_password_check;
mod models; mod models;
mod prelude;
mod state; mod state;
mod utils; mod utils;
use anyhow::{Error, Result}; use anyhow::{Error, Result};
use commands::Command;
use dotenv::dotenv; use dotenv::dotenv;
use migration::{Migrator, MigratorTrait}; use migration::{Migrator, MigratorTrait};
use sea_orm::{prelude::*, Database}; use prelude::*;
use state::{Handler, MainDialogue, State}; use sea_orm::Database;
use std::env; use std::env;
use teloxide::{ use teloxide::{adaptors::throttle::Limits, dispatching::dialogue::InMemStorage, filter_command};
adaptors::{throttle::Limits, Throttle},
dispatching::dialogue::InMemStorage,
filter_command,
prelude::*,
};
fn get_dispatcher( fn get_dispatcher(
token: String, token: String,

View File

@ -1,6 +1,4 @@
use entity::prelude::Account; use crate::prelude::*;
use futures::TryStreamExt;
use sea_orm::prelude::*;
use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, KeyboardMarkup}; use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, KeyboardMarkup};
/// Creates a markup of all user's account names /// Creates a markup of all user's account names

View File

@ -1,9 +1,6 @@
use crate::errors::NoUserInfo; use crate::prelude::*;
use crate::markups::deletion_markup;
use entity::prelude::*;
use sea_orm::DatabaseConnection;
use std::sync::Arc; use std::sync::Arc;
use teloxide::{adaptors::Throttle, dispatching::DpHandlerDescription, prelude::*}; use teloxide::{dispatching::DpHandlerDescription, dptree::Handler};
/// A wierd filter that checks for the existance of a master password. /// A wierd filter that checks for the existance of a master password.
/// ///

View File

@ -1,11 +1,8 @@
//! Models to export and import the accounts //! Models to export and import the accounts
use cryptography::prelude::*; use crate::prelude::*;
use entity::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::utils::validate_field;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct DecryptedAccount { pub struct DecryptedAccount {
pub name: String, pub name: String,

14
src/prelude.rs Normal file
View File

@ -0,0 +1,14 @@
pub(crate) use crate::{
commands::Command,
errors::*,
markups::*,
models::*,
state::State,
state::{Handler, MainDialogue, PackagedHandler},
utils::*,
};
pub(crate) use cryptography::prelude::*;
pub(crate) use entity::prelude::*;
pub(crate) use futures::{StreamExt, TryStreamExt};
pub(crate) use sea_orm::prelude::*;
pub(crate) use teloxide::{adaptors::Throttle, prelude::*};

View File

@ -1,7 +1,5 @@
use crate::{errors::HandlerUsed, markups::deletion_markup, utils::delete_optional}; use crate::prelude::*;
use futures::future::BoxFuture; use futures::future::BoxFuture;
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
/// A generic state handler. It checks for "/cancel" messages and runs the provided validation function /// A generic state handler. It checks for "/cancel" messages and runs the provided validation function
#[inline] #[inline]
@ -9,10 +7,10 @@ pub async fn generic<F>(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
db: DatabaseConnection, db: DatabaseConnection,
dialogue: crate::MainDialogue, dialogue: MainDialogue,
check: F, check: F,
no_text_message: impl Into<String>, no_text_message: impl Into<String>,
next: super::PackagedHandler<String>, next: PackagedHandler<String>,
) -> crate::Result<()> ) -> crate::Result<()>
where where
for<'a> F: FnOnce( for<'a> F: FnOnce(

View File

@ -1,12 +1,4 @@
use crate::{ use crate::prelude::*;
errors::{HandlerUsed, NoUserInfo},
markups::{account_markup, deletion_markup},
utils::delete_optional,
MainDialogue,
};
use entity::prelude::*;
use sea_orm::DatabaseConnection;
use teloxide::{adaptors::Throttle, prelude::*};
/// Checks that the account with that name exists /// Checks that the account with that name exists
#[inline] #[inline]
@ -33,7 +25,7 @@ pub async fn get_existing_name(
msg: Message, msg: Message,
db: DatabaseConnection, db: DatabaseConnection,
dialogue: MainDialogue, dialogue: MainDialogue,
next: super::PackagedHandler<String>, next: PackagedHandler<String>,
) -> crate::Result<()> { ) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let mut handler = next.lock().await; let mut handler = next.lock().await;

View File

@ -1,6 +1,4 @@
use crate::{utils::validate_field, MainDialogue}; use crate::prelude::*;
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
/// Function to handle GetLogin state /// Function to handle GetLogin state
pub async fn get_login( pub async fn get_login(
@ -8,7 +6,7 @@ pub async fn get_login(
msg: Message, msg: Message,
db: DatabaseConnection, db: DatabaseConnection,
dialogue: MainDialogue, dialogue: MainDialogue,
next: super::PackagedHandler<String>, next: PackagedHandler<String>,
) -> crate::Result<()> { ) -> crate::Result<()> {
super::generic::generic( super::generic::generic(
bot, bot,

View File

@ -1,9 +1,5 @@
use crate::{errors::NoUserInfo, MainDialogue}; use crate::prelude::*;
use cryptography::prelude::*;
use entity::prelude::*;
use log::error; use log::error;
use sea_orm::DatabaseConnection;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
/// Returns true if the provided master password is valid /// Returns true if the provided master password is valid
@ -48,7 +44,7 @@ pub async fn get_master_pass(
msg: Message, msg: Message,
db: DatabaseConnection, db: DatabaseConnection,
dialogue: MainDialogue, dialogue: MainDialogue,
next: super::PackagedHandler<String>, next: PackagedHandler<String>,
) -> crate::Result<()> { ) -> crate::Result<()> {
super::generic::generic( super::generic::generic(
bot, bot,

View File

@ -1,7 +1,5 @@
use crate::MainDialogue; use crate::prelude::*;
use cryptography::passwords::{check_master_pass, PasswordValidity}; use cryptography::passwords::{check_master_pass, PasswordValidity};
use sea_orm::DatabaseConnection;
use teloxide::{adaptors::Throttle, prelude::*};
#[inline] #[inline]
fn process_validity(validity: PasswordValidity) -> Result<(), String> { fn process_validity(validity: PasswordValidity) -> Result<(), String> {
@ -56,7 +54,7 @@ pub async fn get_new_master_pass(
msg: Message, msg: Message,
db: DatabaseConnection, db: DatabaseConnection,
dialogue: MainDialogue, dialogue: MainDialogue,
next: super::PackagedHandler<String>, next: PackagedHandler<String>,
) -> crate::Result<()> { ) -> crate::Result<()> {
super::generic::generic( super::generic::generic(
bot, bot,

View File

@ -1,7 +1,4 @@
use crate::{errors::NoUserInfo, utils::validate_field, MainDialogue}; use crate::prelude::*;
use entity::prelude::*;
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
/// Validates a new account /// Validates a new account
#[inline] #[inline]
@ -34,7 +31,7 @@ pub async fn get_new_name(
msg: Message, msg: Message,
db: DatabaseConnection, db: DatabaseConnection,
dialogue: MainDialogue, dialogue: MainDialogue,
next: super::PackagedHandler<String>, next: PackagedHandler<String>,
) -> crate::Result<()> { ) -> crate::Result<()> {
super::generic::generic( super::generic::generic(
bot, bot,

View File

@ -1,7 +1,4 @@
use sea_orm::prelude::*; use crate::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use crate::{utils::validate_field, MainDialogue};
/// Function to handle GetPassword state /// Function to handle GetPassword state
pub async fn get_password( pub async fn get_password(
@ -9,7 +6,7 @@ pub async fn get_password(
msg: Message, msg: Message,
db: DatabaseConnection, db: DatabaseConnection,
dialogue: MainDialogue, dialogue: MainDialogue,
next: super::PackagedHandler<String>, next: PackagedHandler<String>,
) -> crate::Result<()> { ) -> crate::Result<()> {
super::generic::generic( super::generic::generic(
bot, bot,

View File

@ -1,17 +1,9 @@
use crate::{ use crate::prelude::*;
errors::{HandlerUsed, NoUserInfo}, use futures::future::try_join;
markups::deletion_markup,
models::{DecryptedAccount, User},
utils::delete_optional,
MainDialogue,
};
use entity::prelude::*;
use futures::{future::try_join, TryStreamExt};
use itertools::Itertools; use itertools::Itertools;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use sea_orm::prelude::*;
use std::fmt::Write; use std::fmt::Write;
use teloxide::{adaptors::Throttle, net::Download, prelude::*, types::Document}; use teloxide::{net::Download, types::Document};
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
use trim_in_place::TrimInPlace; use trim_in_place::TrimInPlace;
@ -119,7 +111,7 @@ pub async fn get_user(
msg: Message, msg: Message,
db: DatabaseConnection, db: DatabaseConnection,
dialogue: MainDialogue, dialogue: MainDialogue,
next: super::PackagedHandler<User>, next: PackagedHandler<User>,
) -> crate::Result<()> { ) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let mut handler = next.lock().await; let mut handler = next.lock().await;

View File

@ -1,8 +1,6 @@
use crate::MainDialogue; use crate::prelude::*;
use futures::future::BoxFuture; use futures::future::BoxFuture;
use sea_orm::prelude::*;
use std::{future::Future, sync::Arc}; use std::{future::Future, sync::Arc};
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::sync::Mutex; use tokio::sync::Mutex;
type DynHanlder<T> = Box< type DynHanlder<T> = Box<

View File

@ -19,8 +19,8 @@ pub use get_password::get_password;
pub use get_user::get_user; pub use get_user::get_user;
pub use handler::{Handler, PackagedHandler}; pub use handler::{Handler, PackagedHandler};
use crate::models::User; use crate::prelude::*;
use teloxide::{dispatching::dialogue::InMemStorage, prelude::*}; use teloxide::dispatching::dialogue::InMemStorage;
#[derive(Default, Clone)] #[derive(Default, Clone)]
pub enum State { pub enum State {

View File

@ -1,4 +1,4 @@
use teloxide::{adaptors::Throttle, prelude::*}; use crate::prelude::*;
/// Deletes the message ignoring the errors /// Deletes the message ignoring the errors
#[inline] #[inline]