Code cleanup

This commit is contained in:
StNicolay 2024-05-11 17:25:15 +03:00
parent 913d90f077
commit aa3c2ae313
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
20 changed files with 45 additions and 43 deletions

View File

@ -11,11 +11,11 @@ crate::export_handlers!(
); );
use crate::{ use crate::{
entity::locale::LocaleType, errors::handle_error, errors::InvalidCommand, entity::locale::LocaleType,
errors::{handle_error, InvalidCommand},
locales::LocaleTypeExt, locales::LocaleTypeExt,
}; };
use base64::{engine::general_purpose::STANDARD_NO_PAD as B64_ENGINE, Engine as _}; use base64::{engine::general_purpose::STANDARD_NO_PAD as B64_ENGINE, Engine as _};
use std::str::FromStr;
use teloxide::types::CallbackQuery; use teloxide::types::CallbackQuery;
type NameHash = Vec<u8>; type NameHash = Vec<u8>;
@ -46,7 +46,7 @@ impl CallbackCommand {
} }
} }
impl FromStr for CallbackCommand { impl std::str::FromStr for CallbackCommand {
type Err = crate::errors::InvalidCommand; type Err = crate::errors::InvalidCommand;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {

View File

@ -1,6 +1,5 @@
use super::AlterableField::{self, Login, Name, Pass}; use super::AlterableField::{self, Login, Name, Pass};
use crate::{change_state, prelude::*}; use crate::{change_state, cryptography::account::Cipher, prelude::*};
use cryptography::account::Cipher;
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
#[inline] #[inline]

View File

@ -1,5 +1,6 @@
use crate::{locales::LocaleTypeExt, prelude::*}; use crate::{
use entity::locale::LocaleType; entity::locale::LocaleType, locales::LocaleTypeExt, markups::language_markup, prelude::*,
};
#[inline] #[inline]
pub async fn change_locale( pub async fn change_locale(

View File

@ -1,4 +1,4 @@
use crate::{change_state, prelude::*}; use crate::{change_state, markups::account_markup, prelude::*};
use teloxide::types::ParseMode; use teloxide::types::ParseMode;
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;

View File

@ -1,4 +1,4 @@
use crate::prelude::*; use crate::{markups::account_markup, prelude::*};
use teloxide::types::ParseMode; use teloxide::types::ParseMode;
#[inline] #[inline]

View File

@ -1,4 +1,4 @@
use crate::prelude::*; use crate::{markups::language_markup, prelude::*};
#[inline] #[inline]
pub async fn change_language( pub async fn change_language(

View File

@ -1,4 +1,4 @@
use crate::prelude::*; use crate::{models::User, prelude::*};
use parking_lot::Mutex; use parking_lot::Mutex;
use std::sync::Arc; use std::sync::Arc;
use teloxide::types::InputFile; use teloxide::types::InputFile;
@ -46,9 +46,11 @@ async fn get_master_pass(
.await?; .await?;
} }
accounts.sort_unstable_by(|this, other| this.name.cmp(&other.name)); let json = spawn_blocking(move || {
accounts.sort_unstable_by(|this, other| this.name.cmp(&other.name));
let json = spawn_blocking(move || serde_json::to_vec_pretty(&User { accounts })).await??; serde_json::to_vec_pretty(&User { accounts })
})
.await??;
let file = InputFile::memory(json).file_name("accounts.json"); let file = InputFile::memory(json).file_name("accounts.json");
bot.send_document(msg.chat.id, file) bot.send_document(msg.chat.id, file)

View File

@ -1,6 +1,5 @@
use crate::prelude::*; use crate::{cryptography::passwords::generate_passwords, prelude::*};
use arrayvec::ArrayString; use arrayvec::ArrayString;
use cryptography::passwords::generate_passwords;
use std::fmt::Write; use std::fmt::Write;
use teloxide::types::ParseMode; use teloxide::types::ParseMode;
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;

View File

@ -1,4 +1,4 @@
use crate::prelude::*; use crate::{models::User, prelude::*};
use futures::stream; use futures::stream;
use itertools::Itertools; use itertools::Itertools;
use parking_lot::Mutex; use parking_lot::Mutex;

View File

@ -1,6 +1,7 @@
use crate::{change_state, locales::LocaleTypeExt, prelude::*}; use crate::{
use cryptography::hashing::HashedBytes; change_state, cryptography::hashing::HashedBytes, entity::locale::LocaleType,
use entity::locale::LocaleType; locales::LocaleTypeExt, prelude::*,
};
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
#[inline] #[inline]

View File

@ -22,7 +22,6 @@ impl Cipher {
/// Encrypts the value with the current cipher. The 12 byte nonce is appended to the result /// Encrypts the value with the current cipher. The 12 byte nonce is appended to the result
#[inline] #[inline]
#[allow(clippy::missing_panics_doc)]
pub fn encrypt(&self, value: &mut Vec<u8>) { pub fn encrypt(&self, value: &mut Vec<u8>) {
let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng); let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
self.chacha.encrypt_in_place(&nonce, b"", value).unwrap(); self.chacha.encrypt_in_place(&nonce, b"", value).unwrap();

View File

@ -1,7 +1,10 @@
use crate::prelude::*; use crate::prelude::*;
/// Deletes the message ignoring the errors /// Deletes the message without returining errors
#[inline] #[inline]
pub async fn delete_message(bot: Throttle<Bot>, msg: Message) { pub async fn delete_message(bot: Throttle<Bot>, msg: Message) {
let _ = bot.delete_message(msg.chat.id, msg.id).await; let ids = MessageIds::from(&msg);
if let Err(err) = ids.delete(&bot).await {
handle_error(err);
}
} }

View File

@ -1,5 +1,4 @@
// This is fine, because all errors can only be caused by the database errors and the docs would get repetative very quickly // This is fine, because all errors can only be caused by the database errors and the docs would get repetative very quickly
#![allow(clippy::missing_errors_doc)]
pub mod account; pub mod account;
pub mod locale; pub mod locale;

View File

@ -29,6 +29,9 @@ pub fn init_logger() {
.init(); .init();
} }
/// Handles an error.
/// This function must be as cheap and generic as possible and must remain sync.
/// If expensive or long action is required, a task should be spawned but not joined.
#[inline] #[inline]
pub fn handle_error<T>(error: T) pub fn handle_error<T>(error: T)
where where

View File

@ -1,7 +1,5 @@
use crate::prelude::*; use crate::{entity::locale::LocaleType, prelude::*};
use entity::locale::LocaleType; use std::{future::Future, sync::OnceLock};
use std::future::Future;
use std::sync::OnceLock;
static LOCALES: OnceLock<LocaleStore> = OnceLock::new(); static LOCALES: OnceLock<LocaleStore> = OnceLock::new();

View File

@ -1,14 +1,11 @@
pub(crate) use crate::cryptography::{ pub use crate::{
self, account::Decrypted as DecryptedAccount, validate_field, cryptography::{account::Decrypted as DecryptedAccount, validate_field},
}; entity::{account::Account, master_pass::MasterPass, Pool},
pub(crate) use crate::entity::{self, account::Account, master_pass::MasterPass, Pool};
pub(crate) use crate::{
errors::{handle_error, NoUserInfo}, errors::{handle_error, NoUserInfo},
first_handler, handler, first_handler, handler,
locales::LocaleRef, locales::LocaleRef,
markups::*, markups::{deletion_markup, menu_markup},
models::*,
state::{Handler, MainDialogue, MessageIds, PackagedHandler, State}, state::{Handler, MainDialogue, MessageIds, PackagedHandler, State},
}; };
pub(crate) use futures::{StreamExt, TryStreamExt}; pub use futures::{StreamExt as _, TryStreamExt as _};
pub(crate) use teloxide::{adaptors::Throttle, prelude::*}; pub use teloxide::{adaptors::Throttle, prelude::*};

View File

@ -26,7 +26,7 @@ pub enum State {
GetNewMasterPass(PackagedHandler<String>), GetNewMasterPass(PackagedHandler<String>),
GetLogin(PackagedHandler<String>), GetLogin(PackagedHandler<String>),
GetPassword(PackagedHandler<String>), GetPassword(PackagedHandler<String>),
GetUser(PackagedHandler<User>), GetUser(PackagedHandler<crate::models::User>),
} }
pub type MainDialogue = Dialogue<State, InMemStorage<State>>; pub type MainDialogue = Dialogue<State, InMemStorage<State>>;

View File

@ -1,5 +1,4 @@
use crate::prelude::*; use crate::{cryptography::hashing::HashedBytes, prelude::*};
use cryptography::hashing::HashedBytes;
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

View File

@ -1,5 +1,7 @@
use crate::prelude::*; use crate::{
use cryptography::passwords::{check_master_pass, PasswordValidity}; cryptography::passwords::{check_master_pass, PasswordValidity},
prelude::*,
};
use std::fmt::Write as _; use std::fmt::Write as _;
#[inline] #[inline]

View File

@ -1,4 +1,4 @@
use crate::{errors::HandlerUsed, prelude::*}; use crate::{errors::HandlerUsed, models::User, prelude::*};
use futures::TryFutureExt; use futures::TryFutureExt;
use itertools::Itertools; use itertools::Itertools;
use std::{borrow::Cow, fmt::Write, path::Path}; use std::{borrow::Cow, fmt::Write, path::Path};