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

View File

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

View File

@ -1,9 +1,4 @@
use crate::{
errors::NoUserInfo, markups::deletion_markup, models::DecryptedAccount, Handler, MainDialogue,
State,
};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use crate::prelude::*;
use tokio::task::spawn_blocking;
/// 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 teloxide::{adaptors::Throttle, prelude::*};
use crate::prelude::*;
/// Handles /cancel command when there's no active state
pub async fn cancel(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> {

View File

@ -1,11 +1,4 @@
use crate::{
errors::NoUserInfo,
markups::{self, deletion_markup},
Handler, MainDialogue, State,
};
use entity::prelude::*;
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use crate::prelude::*;
/// 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
@ -53,7 +46,7 @@ pub async fn delete(
db: DatabaseConnection,
) -> crate::Result<()> {
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() {
bot.send_message(msg.chat.id, "No accounts found")
.reply_markup(deletion_markup())

View File

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

View File

@ -1,15 +1,7 @@
use crate::{
errors::NoUserInfo,
markups::deletion_markup,
models::{DecryptedAccount, User},
Handler, MainDialogue, State,
};
use entity::prelude::*;
use futures::TryStreamExt;
use crate::prelude::*;
use parking_lot::Mutex;
use sea_orm::DatabaseConnection;
use std::sync::Arc;
use teloxide::{adaptors::Throttle, prelude::*, types::InputFile};
use teloxide::types::InputFile;
use tokio::task::spawn_blocking;
/// 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 cryptography::passwords::generate_passwords;
use std::fmt::Write;
use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode};
use teloxide::types::ParseMode;
use tokio::task::spawn_blocking;
const MESSAGE_HEADER: &str = "Passwords:";

View File

@ -1,12 +1,5 @@
use crate::{
errors::NoUserInfo,
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 crate::prelude::*;
use teloxide::types::ParseMode;
use tokio::task::spawn_blocking;
/// 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,
) -> crate::Result<()> {
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() {
bot.send_message(msg.chat.id, "No accounts found")
.reply_markup(deletion_markup())

View File

@ -1,9 +1,7 @@
use crate::{errors::NoUserInfo, markups::deletion_markup};
use entity::prelude::*;
use futures::{future, TryStreamExt};
use sea_orm::DatabaseConnection;
use crate::prelude::*;
use futures::future;
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
pub async fn get_accounts(

View File

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

View File

@ -1,15 +1,8 @@
use crate::{
errors::NoUserInfo,
markups::deletion_markup,
models::{DecryptedAccount, User},
Handler, MainDialogue, State,
};
use futures::{stream, StreamExt};
use crate::prelude::*;
use futures::stream;
use itertools::Itertools;
use parking_lot::Mutex;
use sea_orm::prelude::*;
use std::sync::Arc;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::task::spawn_blocking;
/// 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 set_master_pass::set_master_pass;
pub use start::start;
use teloxide::macros::BotCommands;
#[derive(BotCommands, Clone, Copy)]

View File

@ -1,9 +1,5 @@
use crate::{errors::NoUserInfo, markups::deletion_markup, Handler, MainDialogue, State};
use cryptography::prelude::*;
use entity::prelude::*;
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::task;
use crate::prelude::*;
use tokio::task::spawn_blocking;
/// Actually sets the master password
async fn get_master_pass(
@ -15,7 +11,7 @@ async fn get_master_pass(
) -> crate::Result<()> {
dialogue.exit().await?;
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)
})
.await?;

View File

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

View File

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

View File

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

View File

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

View File

@ -1,9 +1,6 @@
use crate::errors::NoUserInfo;
use crate::markups::deletion_markup;
use entity::prelude::*;
use sea_orm::DatabaseConnection;
use crate::prelude::*;
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.
///

View File

@ -1,11 +1,8 @@
//! Models to export and import the accounts
use cryptography::prelude::*;
use entity::prelude::*;
use crate::prelude::*;
use serde::{Deserialize, Serialize};
use crate::utils::validate_field;
#[derive(Serialize, Deserialize)]
pub struct DecryptedAccount {
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 sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
/// A generic state handler. It checks for "/cancel" messages and runs the provided validation function
#[inline]
@ -9,10 +7,10 @@ pub async fn generic<F>(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: crate::MainDialogue,
dialogue: MainDialogue,
check: F,
no_text_message: impl Into<String>,
next: super::PackagedHandler<String>,
next: PackagedHandler<String>,
) -> crate::Result<()>
where
for<'a> F: FnOnce(

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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