From c5a969384a474b32460fd1cb592757e1826d7774 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Wed, 7 Jun 2023 04:13:47 +0200 Subject: [PATCH] Refactor --- src/bots/approved_bot/modules/download.rs | 75 ++++++++++++++----- .../approved_bot/services/book_cache/mod.rs | 10 +-- .../services/book_library/formaters.rs | 4 +- 3 files changed, 65 insertions(+), 24 deletions(-) diff --git a/src/bots/approved_bot/modules/download.rs b/src/bots/approved_bot/modules/download.rs index e6936f7..821e258 100644 --- a/src/bots/approved_bot/modules/download.rs +++ b/src/bots/approved_bot/modules/download.rs @@ -34,12 +34,12 @@ use crate::{ use super::utils::{filter_command, CommandParse}; #[derive(Clone)] -pub struct DownloadData { +pub struct DownloadDataCommand { pub format: String, pub id: u32, } -impl CommandParse for DownloadData { +impl CommandParse for DownloadDataCommand { fn parse(s: &str, bot_name: &str) -> Result { let re = Regex::new(r"^/d_(?P[a-zA-Z0-9]+)_(?P\d+)$").unwrap(); @@ -55,7 +55,7 @@ impl CommandParse for DownloadData { let file_format = &caps["file_format"]; let book_id: u32 = caps["book_id"].parse().unwrap(); - Ok(DownloadData { + Ok(DownloadDataCommand { format: file_format.to_string(), id: book_id, }) @@ -63,18 +63,18 @@ impl CommandParse for DownloadData { } #[derive(Clone)] -pub struct StartDownloadData { +pub struct StartDownloadCommand { pub id: u32, } -impl ToString for StartDownloadData { +impl ToString for StartDownloadCommand { fn to_string(&self) -> String { - let StartDownloadData { id } = self; + let StartDownloadCommand { id } = self; format!("/d_{id}") } } -impl CommandParse for StartDownloadData { +impl CommandParse for StartDownloadCommand { fn parse(s: &str, bot_name: &str) -> Result { let re = Regex::new(r"^/d_(?P\d+)$").unwrap(); @@ -89,7 +89,7 @@ impl CommandParse for StartDownloadData { let book_id: u32 = caps["book_id"].parse().unwrap(); - Ok(StartDownloadData { id: book_id }) + Ok(StartDownloadCommand { id: book_id }) } } @@ -127,6 +127,47 @@ impl FromStr for DownloadQueryData { } } +#[derive(Clone, EnumIter)] +pub enum DownloadArchiveCommands { + Sequence { id: u32, file_format: String }, + Author { id: u32, file_format: String }, + Translator { id: u32, file_format: String } +} + +impl ToString for DownloadArchiveCommands { + fn to_string(&self) -> String { + match self { + DownloadArchiveCommands::Sequence { id, file_format } => format!("da_s_{id}_{file_format}"), + DownloadArchiveCommands::Author { id, file_format } => format!("da_a_{id}_{file_format}"), + DownloadArchiveCommands::Translator { id, file_format } => format!("da_t_{id}_{file_format}"), + } + } +} + +impl FromStr for DownloadArchiveCommands { + type Err = strum::ParseError; + + fn from_str(s: &str) -> Result { + let re = Regex::new(r"^/da_(?P[s|a|t])_(?P\d+)_(?P\w+)$").unwrap(); + + let caps = re.captures(s); + let caps = match caps { + Some(v) => v, + None => return Err(strum::ParseError::VariantNotFound), + }; + + let obj_id: u32 = caps["id"].parse().unwrap(); + let file_type: String = caps["file_type"].to_string(); + + match &caps["type"] { + "s" => Ok(DownloadArchiveCommands::Sequence { id: obj_id, file_format: file_type }), + "a" => Ok(DownloadArchiveCommands::Author { id: obj_id, file_format: file_type }), + "t" => Ok(DownloadArchiveCommands::Translator { id: obj_id, file_format: file_type }), + _ => Err(strum::ParseError::VariantNotFound) + } + } +} + async fn _send_cached( message: &Message, bot: &CacheMe>, @@ -149,7 +190,7 @@ async fn _send_cached( async fn send_cached_message( message: Message, bot: CacheMe>, - download_data: DownloadData, + download_data: DownloadDataCommand, donation_notification_cache: Cache, need_delete_message: bool, ) -> BotHandlerInternal { @@ -204,7 +245,7 @@ async fn _send_downloaded_file( async fn send_with_download_from_channel( message: Message, bot: CacheMe>, - download_data: DownloadData, + download_data: DownloadDataCommand, donation_notification_cache: Cache, need_delete_message: bool, ) -> BotHandlerInternal { @@ -226,7 +267,7 @@ async fn download_handler( message: Message, bot: CacheMe>, cache: BotCache, - download_data: DownloadData, + download_data: DownloadDataCommand, donation_notification_cache: Cache, need_delete_message: bool, ) -> BotHandlerInternal { @@ -257,7 +298,7 @@ async fn download_handler( async fn get_download_keyboard_handler( message: Message, bot: CacheMe>, - download_data: StartDownloadData, + download_data: StartDownloadCommand, ) -> BotHandlerInternal { let book = match get_book(download_data.id).await { Ok(v) => v, @@ -304,12 +345,12 @@ pub fn get_download_hander() -> crate::bots::BotHandler { dptree::entry() .branch( Update::filter_message() - .chain(filter_command::()) + .chain(filter_command::()) .endpoint( |message: Message, bot: CacheMe>, cache: BotCache, - download_data: DownloadData, + download_data: DownloadDataCommand, app_state: AppState| async move { download_handler( message, @@ -325,11 +366,11 @@ pub fn get_download_hander() -> crate::bots::BotHandler { ) .branch( Update::filter_message() - .chain(filter_command::()) + .chain(filter_command::()) .endpoint( |message: Message, bot: CacheMe>, - download_data: StartDownloadData| async move { + download_data: StartDownloadCommand| async move { get_download_keyboard_handler(message, bot, download_data).await }, ), @@ -349,7 +390,7 @@ pub fn get_download_hander() -> crate::bots::BotHandler { cq.message.unwrap(), bot, cache, - DownloadData { + DownloadDataCommand { format: file_type, id: book_id, }, diff --git a/src/bots/approved_bot/services/book_cache/mod.rs b/src/bots/approved_bot/services/book_cache/mod.rs index c344af3..e8715d7 100644 --- a/src/bots/approved_bot/services/book_cache/mod.rs +++ b/src/bots/approved_bot/services/book_cache/mod.rs @@ -2,7 +2,7 @@ use base64::{engine::general_purpose, Engine}; use reqwest::StatusCode; use std::fmt; -use crate::{bots::approved_bot::modules::download::DownloadData, config}; +use crate::{bots::approved_bot::modules::download::DownloadDataCommand, config}; use self::types::{CachedMessage, DownloadFile}; @@ -22,9 +22,9 @@ impl fmt::Display for DownloadError { impl std::error::Error for DownloadError {} pub async fn get_cached_message( - download_data: &DownloadData, + download_data: &DownloadDataCommand, ) -> Result> { - let DownloadData { format, id } = download_data; + let DownloadDataCommand { format, id } = download_data; let client = reqwest::Client::new(); let response = client @@ -47,9 +47,9 @@ pub async fn get_cached_message( } pub async fn download_file( - download_data: &DownloadData, + download_data: &DownloadDataCommand, ) -> Result> { - let DownloadData { format, id } = download_data; + let DownloadDataCommand { format, id } = download_data; let response = reqwest::Client::new() .get(format!( diff --git a/src/bots/approved_bot/services/book_library/formaters.rs b/src/bots/approved_bot/services/book_library/formaters.rs index b0f6010..67a542c 100644 --- a/src/bots/approved_bot/services/book_library/formaters.rs +++ b/src/bots/approved_bot/services/book_library/formaters.rs @@ -1,6 +1,6 @@ use std::cmp::min; -use crate::bots::approved_bot::modules::download::StartDownloadData; +use crate::bots::approved_bot::modules::download::StartDownloadCommand; use super::types::{ Author, AuthorBook, Book, BookAuthor, BookGenre, SearchBook, Sequence, Translator, @@ -416,7 +416,7 @@ impl Format for Book { false => "".to_string(), }; - let download_command = (StartDownloadData { id: self.id }).to_string(); + let download_command = (StartDownloadCommand { id: self.id }).to_string(); let download_links = format!("Скачать:\n📥{download_command}"); let required_data_len: usize = format!("{book_title}{annotations}{download_links}").len();