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::{
entity::locale::LocaleType, errors::handle_error, errors::InvalidCommand,
entity::locale::LocaleType,
errors::{handle_error, InvalidCommand},
locales::LocaleTypeExt,
};
use base64::{engine::general_purpose::STANDARD_NO_PAD as B64_ENGINE, Engine as _};
use std::str::FromStr;
use teloxide::types::CallbackQuery;
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;
fn from_str(s: &str) -> Result<Self, Self::Err> {

View File

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

View File

@ -1,5 +1,6 @@
use crate::{locales::LocaleTypeExt, prelude::*};
use entity::locale::LocaleType;
use crate::{
entity::locale::LocaleType, locales::LocaleTypeExt, markups::language_markup, prelude::*,
};
#[inline]
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 tokio::task::spawn_blocking;

View File

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

View File

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

View File

@ -1,4 +1,4 @@
use crate::prelude::*;
use crate::{models::User, prelude::*};
use parking_lot::Mutex;
use std::sync::Arc;
use teloxide::types::InputFile;
@ -46,9 +46,11 @@ async fn get_master_pass(
.await?;
}
accounts.sort_unstable_by(|this, other| this.name.cmp(&other.name));
let json = spawn_blocking(move || serde_json::to_vec_pretty(&User { accounts })).await??;
let json = spawn_blocking(move || {
accounts.sort_unstable_by(|this, other| this.name.cmp(&other.name));
serde_json::to_vec_pretty(&User { accounts })
})
.await??;
let file = InputFile::memory(json).file_name("accounts.json");
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 cryptography::passwords::generate_passwords;
use std::fmt::Write;
use teloxide::types::ParseMode;
use tokio::task::spawn_blocking;

View File

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

View File

@ -1,6 +1,7 @@
use crate::{change_state, locales::LocaleTypeExt, prelude::*};
use cryptography::hashing::HashedBytes;
use entity::locale::LocaleType;
use crate::{
change_state, cryptography::hashing::HashedBytes, entity::locale::LocaleType,
locales::LocaleTypeExt, prelude::*,
};
use tokio::task::spawn_blocking;
#[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
#[inline]
#[allow(clippy::missing_panics_doc)]
pub fn encrypt(&self, value: &mut Vec<u8>) {
let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
self.chacha.encrypt_in_place(&nonce, b"", value).unwrap();

View File

@ -1,7 +1,10 @@
use crate::prelude::*;
/// Deletes the message ignoring the errors
/// Deletes the message without returining errors
#[inline]
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
#![allow(clippy::missing_errors_doc)]
pub mod account;
pub mod locale;

View File

@ -29,6 +29,9 @@ pub fn init_logger() {
.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]
pub fn handle_error<T>(error: T)
where

View File

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

View File

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

View File

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

View File

@ -1,5 +1,4 @@
use crate::prelude::*;
use cryptography::hashing::HashedBytes;
use crate::{cryptography::hashing::HashedBytes, prelude::*};
use tokio::task::spawn_blocking;
/// Returns true if the provided master password is valid

View File

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

View File

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