From 4977bb413c7de26e4a2161773570f30e01d5798e Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 30 Jan 2024 12:54:22 +0400 Subject: [PATCH] feat: Removed unnecessary async --- src/bot/command.rs | 1 - src/bot/file_actions/download.rs | 6 +++--- src/bot/file_actions/mod.rs | 2 +- src/bot/file_actions/send_file.rs | 4 ++-- src/bot/file_info/get_info.rs | 5 ++++- src/bot/file_info/mod.rs | 2 +- src/bot/mod.rs | 2 +- src/crypt/aes_key.rs | 2 +- src/crypt/base64_convert.rs | 4 ++-- src/crypt/decryption.rs | 2 +- src/crypt/encryption.rs | 2 +- src/db/checkers.rs | 2 +- src/db/insert_to_db.rs | 2 +- src/tools/generate_uuid.rs | 2 +- src/tools/short_url.rs | 10 +++++----- 15 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/bot/command.rs b/src/bot/command.rs index 6ea6a7d..429c677 100644 --- a/src/bot/command.rs +++ b/src/bot/command.rs @@ -5,7 +5,6 @@ use crate::types::state::{HandlerResult, MyDialogue, State}; use super::file_info::get_history::send_history; - #[derive(BotCommands, Clone)] #[command( rename_rule = "lowercase", diff --git a/src/bot/file_actions/download.rs b/src/bot/file_actions/download.rs index 8c733fa..30dd03d 100644 --- a/src/bot/file_actions/download.rs +++ b/src/bot/file_actions/download.rs @@ -45,14 +45,14 @@ pub async fn download_file( let mut dst = File::create(&file_path).await?; bot.download_file(&telegram_file.path, &mut dst).await?; - let aes_key = set_aes_key().await; - let encoded_key = convert_aes_to_base64(aes_key).await; + let aes_key = set_aes_key(); + let encoded_key = convert_aes_to_base64(aes_key); let mut open_file = File::open(&file_path).await?; let mut file_data = Vec::new(); open_file.read_to_end(&mut file_data).await?; - let encrypted_data = match encrypt_data(&file_data, aes_key).await { + let encrypted_data = match encrypt_data(&file_data, aes_key) { Ok(encrypted_data) => encrypted_data, Err(_err) => { bot.send_message(msg.chat.id, "Unable to crypt file. Try again") diff --git a/src/bot/file_actions/mod.rs b/src/bot/file_actions/mod.rs index 17ee444..b5fccf2 100644 --- a/src/bot/file_actions/mod.rs +++ b/src/bot/file_actions/mod.rs @@ -1,3 +1,3 @@ pub mod download; pub mod get_file; -pub mod send_file; \ No newline at end of file +pub mod send_file; diff --git a/src/bot/file_actions/send_file.rs b/src/bot/file_actions/send_file.rs index 2170b26..5c03378 100644 --- a/src/bot/file_actions/send_file.rs +++ b/src/bot/file_actions/send_file.rs @@ -29,7 +29,7 @@ pub async fn send_file( } }; if let Some(key) = aes_key { - let key_bytes = match convert_base64_to_aes(key).await { + let key_bytes = match convert_base64_to_aes(key) { Ok(key) => key, Err(_err) => { bot.send_message(msg.chat.id, "Invalid key. Try again") @@ -41,7 +41,7 @@ pub async fn send_file( let mut file_data = Vec::new(); file.read_to_end(&mut file_data).await?; - let data = decrypt_data(file_data, key_bytes).await.unwrap(); + let data = decrypt_data(file_data, key_bytes).unwrap(); bot.send_document( msg.chat.id, diff --git a/src/bot/file_info/get_info.rs b/src/bot/file_info/get_info.rs index 11d8256..ac9d20e 100644 --- a/src/bot/file_info/get_info.rs +++ b/src/bot/file_info/get_info.rs @@ -1,6 +1,9 @@ use teloxide::{requests::Requester, types::Message, Bot}; -use crate::{types::state::{HandlerResult, MyDialogue, State}, bot::file_actions::send_file::send_file}; +use crate::{ + bot::file_actions::send_file::send_file, + types::state::{HandlerResult, MyDialogue, State}, +}; pub async fn get_file_info(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult { if let Some(text) = msg.text() { diff --git a/src/bot/file_info/mod.rs b/src/bot/file_info/mod.rs index 53728ca..560bdc2 100644 --- a/src/bot/file_info/mod.rs +++ b/src/bot/file_info/mod.rs @@ -1,3 +1,3 @@ pub mod encryption_type; pub mod get_history; -pub mod get_info; \ No newline at end of file +pub mod get_info; diff --git a/src/bot/mod.rs b/src/bot/mod.rs index f4acbec..e273dc6 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -1,3 +1,3 @@ pub mod command; pub mod file_actions; -pub mod file_info; \ No newline at end of file +pub mod file_info; diff --git a/src/crypt/aes_key.rs b/src/crypt/aes_key.rs index a4f0191..32485b1 100644 --- a/src/crypt/aes_key.rs +++ b/src/crypt/aes_key.rs @@ -1,7 +1,7 @@ use rand::thread_rng; use rand_core::RngCore; -pub async fn set_aes_key() -> [u8; 32] { +pub fn set_aes_key() -> [u8; 32] { let mut gen_aes_key = [0u8; 32]; thread_rng().fill_bytes(&mut gen_aes_key); gen_aes_key diff --git a/src/crypt/base64_convert.rs b/src/crypt/base64_convert.rs index f6af6f4..a3b92e5 100644 --- a/src/crypt/base64_convert.rs +++ b/src/crypt/base64_convert.rs @@ -1,6 +1,6 @@ use base64::{engine::general_purpose, Engine}; -pub async fn convert_base64_to_aes(aes_key: String) -> Result<[u8; 32], tokio::io::Error> { +pub fn convert_base64_to_aes(aes_key: String) -> Result<[u8; 32], tokio::io::Error> { let key_vec = match general_purpose::URL_SAFE_NO_PAD.decode(aes_key) { Ok(key) => key, Err(_err) => { @@ -23,6 +23,6 @@ pub async fn convert_base64_to_aes(aes_key: String) -> Result<[u8; 32], tokio::i Ok(key_array) } -pub async fn convert_aes_to_base64(aes_bytes: [u8; 32]) -> String { +pub fn convert_aes_to_base64(aes_bytes: [u8; 32]) -> String { general_purpose::URL_SAFE_NO_PAD.encode(aes_bytes) } diff --git a/src/crypt/decryption.rs b/src/crypt/decryption.rs index c94d2ef..8d4ad87 100644 --- a/src/crypt/decryption.rs +++ b/src/crypt/decryption.rs @@ -4,7 +4,7 @@ use aes::{ }; use aes_gcm::KeyInit; -pub async fn decrypt_data(buf: Vec, aes_key: [u8; 32]) -> Result, tokio::io::Error> { +pub fn decrypt_data(buf: Vec, aes_key: [u8; 32]) -> Result, tokio::io::Error> { let cipher = Aes256::new(&GenericArray::from_slice(&aes_key)); let mut decrypted_data = buf.to_vec().to_owned(); diff --git a/src/crypt/encryption.rs b/src/crypt/encryption.rs index b7af2b7..9a519f2 100644 --- a/src/crypt/encryption.rs +++ b/src/crypt/encryption.rs @@ -4,7 +4,7 @@ use aes::{ }; use aes_gcm::KeyInit; -pub async fn encrypt_data(data: &[u8], aes_key: [u8; 32]) -> Result, tokio::io::Error> { +pub fn encrypt_data(data: &[u8], aes_key: [u8; 32]) -> Result, tokio::io::Error> { let cipher = Aes256::new(&GenericArray::from_slice(&aes_key)); let mut padded_data = data.to_vec(); let pad_len = 16 - (data.len() % 16); diff --git a/src/db/checkers.rs b/src/db/checkers.rs index ee42343..e1ad137 100644 --- a/src/db/checkers.rs +++ b/src/db/checkers.rs @@ -33,7 +33,7 @@ pub async fn find_dublicate(short_url: String) -> String { .await .unwrap() { - find_dublicate(generate_short_path_url().await).await; + find_dublicate(generate_short_path_url()).await; } short_url diff --git a/src/db/insert_to_db.rs b/src/db/insert_to_db.rs index 514dce9..22a9450 100644 --- a/src/db/insert_to_db.rs +++ b/src/db/insert_to_db.rs @@ -36,7 +36,7 @@ pub async fn insert_main_info( .await .unwrap() { - short_path_url = find_dublicate(generate_short_path_url().await).await; + short_path_url = find_dublicate(generate_short_path_url()).await; }; let document = doc! { diff --git a/src/tools/generate_uuid.rs b/src/tools/generate_uuid.rs index b6b92ec..f0bb0dc 100644 --- a/src/tools/generate_uuid.rs +++ b/src/tools/generate_uuid.rs @@ -1,3 +1,3 @@ -pub async fn generate_uuid_v4() -> String { +pub fn generate_uuid_v4() -> String { uuid::Uuid::new_v4().to_string() } diff --git a/src/tools/short_url.rs b/src/tools/short_url.rs index 6f25c25..84114a5 100644 --- a/src/tools/short_url.rs +++ b/src/tools/short_url.rs @@ -1,14 +1,14 @@ use rand::Rng; const CHARS: [char; 52] = [ - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', - 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', - 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - ]; + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', + 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', + 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', +]; const NUMBERS: [char; 10] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']; -pub async fn generate_short_path_url() -> String { +pub fn generate_short_path_url() -> String { let mut short_url: Vec = Vec::with_capacity(8); for _ in 0..8 {