From 6dee897bb611fef710ee65cd387ff5e1a7a669b9 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Sat, 2 Sep 2023 14:52:15 +0200 Subject: [PATCH] Refactor --- .../modules/download/callback_data.rs | 111 ++++++++++ .../approved_bot/modules/download/commads.rs | 77 +++++++ .../modules/{download.rs => download/mod.rs} | 192 +----------------- .../approved_bot/services/book_cache/mod.rs | 2 +- .../services/book_library/formaters.rs | 2 +- 5 files changed, 199 insertions(+), 185 deletions(-) create mode 100644 src/bots/approved_bot/modules/download/callback_data.rs create mode 100644 src/bots/approved_bot/modules/download/commads.rs rename src/bots/approved_bot/modules/{download.rs => download/mod.rs} (72%) diff --git a/src/bots/approved_bot/modules/download/callback_data.rs b/src/bots/approved_bot/modules/download/callback_data.rs new file mode 100644 index 0000000..1013b84 --- /dev/null +++ b/src/bots/approved_bot/modules/download/callback_data.rs @@ -0,0 +1,111 @@ +use std::str::FromStr; + +use regex::Regex; +use strum_macros::EnumIter; + + +#[derive(Clone, EnumIter)] +pub enum DownloadQueryData { + DownloadData { book_id: u32, file_type: String }, +} + +impl ToString for DownloadQueryData { + fn to_string(&self) -> String { + match self { + DownloadQueryData::DownloadData { book_id, file_type } => { + format!("d_{book_id}_{file_type}") + } + } + } +} + +impl FromStr for DownloadQueryData { + type Err = strum::ParseError; + + fn from_str(s: &str) -> Result { + let re = Regex::new(r"^d_(?P\d+)_(?P\w+)$").unwrap(); + + let caps = re.captures(s); + let caps = match caps { + Some(v) => v, + None => return Err(strum::ParseError::VariantNotFound), + }; + + let book_id: u32 = caps["book_id"].parse().unwrap(); + let file_type: String = caps["file_type"].to_string(); + + Ok(DownloadQueryData::DownloadData { book_id, file_type }) + } +} + +#[derive(Clone, EnumIter)] +pub enum DownloadArchiveQueryData { + Sequence { id: u32, file_type: String }, + Author { id: u32, file_type: String }, + Translator { id: u32, file_type: String } +} + +impl ToString for DownloadArchiveQueryData { + fn to_string(&self) -> String { + match self { + DownloadArchiveQueryData::Sequence { id, file_type } => format!("da_s_{id}_{file_type}"), + DownloadArchiveQueryData::Author { id, file_type } => format!("da_a_{id}_{file_type}"), + DownloadArchiveQueryData::Translator { id, file_type } => format!("da_t_{id}_{file_type}"), + } + } +} + +impl FromStr for DownloadArchiveQueryData { + 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 id: u32 = caps["id"].parse().unwrap(); + let file_type: String = caps["file_type"].to_string(); + + Ok( + match caps["obj_type"].to_string().as_str() { + "s" => DownloadArchiveQueryData::Sequence { id, file_type }, + "a" => DownloadArchiveQueryData::Author { id, file_type }, + "t" => DownloadArchiveQueryData::Translator { id, file_type }, + _ => return Err(strum::ParseError::VariantNotFound) + } + ) + } +} + +#[derive(Clone)] +pub struct CheckArchiveStatus { + pub task_id: String +} + +impl ToString for CheckArchiveStatus { + fn to_string(&self) -> String { + format!("check_da_{}", self.task_id) + } +} + +impl FromStr for CheckArchiveStatus { + type Err = strum::ParseError; + + fn from_str(s: &str) -> Result { + let re = Regex::new(r"^check_da_(?P\w+)$").unwrap(); + + let caps = re.captures(s); + let caps = match caps { + Some(v) => v, + None => return Err(strum::ParseError::VariantNotFound), + }; + + let task_id: String = caps["task_id"].parse().unwrap(); + + Ok(CheckArchiveStatus { task_id }) + } +} diff --git a/src/bots/approved_bot/modules/download/commads.rs b/src/bots/approved_bot/modules/download/commads.rs new file mode 100644 index 0000000..0d8b37e --- /dev/null +++ b/src/bots/approved_bot/modules/download/commads.rs @@ -0,0 +1,77 @@ +use regex::Regex; +use strum_macros::EnumIter; + +use crate::bots::approved_bot::modules::utils::CommandParse; + + +#[derive(Clone)] +pub struct StartDownloadCommand { + pub id: u32, +} + +impl ToString for StartDownloadCommand { + fn to_string(&self) -> String { + let StartDownloadCommand { id } = self; + format!("/d_{id}") + } +} + +impl CommandParse for StartDownloadCommand { + fn parse(s: &str, bot_name: &str) -> Result { + let re = Regex::new(r"^/d_(?P\d+)$").unwrap(); + + let full_bot_name = format!("@{bot_name}"); + let after_replace = s.replace(&full_bot_name, ""); + + let caps = re.captures(&after_replace); + let caps = match caps { + Some(v) => v, + None => return Err(strum::ParseError::VariantNotFound), + }; + + let book_id: u32 = caps["book_id"].parse().unwrap(); + + Ok(StartDownloadCommand { id: book_id }) + } +} + +#[derive(Clone, EnumIter)] +pub enum DownloadArchiveCommand { + Sequence { id: u32}, + Author { id: u32 }, + Translator { id: u32 } +} + +impl ToString for DownloadArchiveCommand { + fn to_string(&self) -> String { + match self { + DownloadArchiveCommand::Sequence { id } => format!("/da_s_{id}"), + DownloadArchiveCommand::Author { id } => format!("/da_a_{id}"), + DownloadArchiveCommand::Translator { id } => format!("/da_t_{id}"), + } + } +} + +impl CommandParse for DownloadArchiveCommand { + fn parse(s: &str, bot_name: &str) -> Result { + let re = Regex::new(r"^/da_(?P[s|a|t])_(?P\d+)$").unwrap(); + + let full_bot_name = format!("@{bot_name}"); + let after_replace = s.replace(&full_bot_name, ""); + + let caps = re.captures(&after_replace); + let caps = match caps { + Some(v) => v, + None => return Err(strum::ParseError::VariantNotFound), + }; + + let obj_id: u32 = caps["id"].parse().unwrap(); + + match &caps["type"] { + "s" => Ok(DownloadArchiveCommand::Sequence { id: obj_id }), + "a" => Ok(DownloadArchiveCommand::Author { id: obj_id }), + "t" => Ok(DownloadArchiveCommand::Translator { id: obj_id }), + _ => Err(strum::ParseError::VariantNotFound) + } + } +} diff --git a/src/bots/approved_bot/modules/download.rs b/src/bots/approved_bot/modules/download/mod.rs similarity index 72% rename from src/bots/approved_bot/modules/download.rs rename to src/bots/approved_bot/modules/download/mod.rs index 7928bb1..dd14a2e 100644 --- a/src/bots/approved_bot/modules/download.rs +++ b/src/bots/approved_bot/modules/download/mod.rs @@ -1,9 +1,12 @@ -use std::{str::FromStr, time::Duration}; +pub mod commads; +pub mod callback_data; + +use std::time::Duration; use chrono::Utc; use futures::TryStreamExt; -use regex::Regex; -use strum_macros::EnumIter; + + use teloxide::{ adaptors::{CacheMe, Throttle}, dispatching::UpdateFilterExt, @@ -28,193 +31,16 @@ use crate::{ batch_downloader::{create_task, get_task, TaskStatus} }, - tools::filter_callback_query, + tools::filter_callback_query, modules::download::callback_data::DownloadArchiveQueryData, }, BotHandlerInternal, }, bots_manager::BotCache, }; -use super::utils::{filter_command, CommandParse}; +use self::{callback_data::{CheckArchiveStatus, DownloadQueryData}, commads::{StartDownloadCommand, DownloadArchiveCommand}}; -#[derive(Clone)] -pub struct StartDownloadCommand { - pub id: u32, -} - -impl ToString for StartDownloadCommand { - fn to_string(&self) -> String { - let StartDownloadCommand { id } = self; - format!("/d_{id}") - } -} - -impl CommandParse for StartDownloadCommand { - fn parse(s: &str, bot_name: &str) -> Result { - let re = Regex::new(r"^/d_(?P\d+)$").unwrap(); - - let full_bot_name = format!("@{bot_name}"); - let after_replace = s.replace(&full_bot_name, ""); - - let caps = re.captures(&after_replace); - let caps = match caps { - Some(v) => v, - None => return Err(strum::ParseError::VariantNotFound), - }; - - let book_id: u32 = caps["book_id"].parse().unwrap(); - - Ok(StartDownloadCommand { id: book_id }) - } -} - -#[derive(Clone, EnumIter)] -pub enum DownloadQueryData { - DownloadData { book_id: u32, file_type: String }, -} - -impl ToString for DownloadQueryData { - fn to_string(&self) -> String { - match self { - DownloadQueryData::DownloadData { book_id, file_type } => { - format!("d_{book_id}_{file_type}") - } - } - } -} - -impl FromStr for DownloadQueryData { - type Err = strum::ParseError; - - fn from_str(s: &str) -> Result { - let re = Regex::new(r"^d_(?P\d+)_(?P\w+)$").unwrap(); - - let caps = re.captures(s); - let caps = match caps { - Some(v) => v, - None => return Err(strum::ParseError::VariantNotFound), - }; - - let book_id: u32 = caps["book_id"].parse().unwrap(); - let file_type: String = caps["file_type"].to_string(); - - Ok(DownloadQueryData::DownloadData { book_id, file_type }) - } -} - -#[derive(Clone, EnumIter)] -pub enum DownloadArchiveCommand { - Sequence { id: u32}, - Author { id: u32 }, - Translator { id: u32 } -} - -impl ToString for DownloadArchiveCommand { - fn to_string(&self) -> String { - match self { - DownloadArchiveCommand::Sequence { id } => format!("/da_s_{id}"), - DownloadArchiveCommand::Author { id } => format!("/da_a_{id}"), - DownloadArchiveCommand::Translator { id } => format!("/da_t_{id}"), - } - } -} - -impl CommandParse for DownloadArchiveCommand { - fn parse(s: &str, bot_name: &str) -> Result { - let re = Regex::new(r"^/da_(?P[s|a|t])_(?P\d+)$").unwrap(); - - let full_bot_name = format!("@{bot_name}"); - let after_replace = s.replace(&full_bot_name, ""); - - let caps = re.captures(&after_replace); - let caps = match caps { - Some(v) => v, - None => return Err(strum::ParseError::VariantNotFound), - }; - - let obj_id: u32 = caps["id"].parse().unwrap(); - - match &caps["type"] { - "s" => Ok(DownloadArchiveCommand::Sequence { id: obj_id }), - "a" => Ok(DownloadArchiveCommand::Author { id: obj_id }), - "t" => Ok(DownloadArchiveCommand::Translator { id: obj_id }), - _ => Err(strum::ParseError::VariantNotFound) - } - } -} - -#[derive(Clone, EnumIter)] -pub enum DownloadArchiveQueryData { - Sequence { id: u32, file_type: String }, - Author { id: u32, file_type: String }, - Translator { id: u32, file_type: String } -} - -impl ToString for DownloadArchiveQueryData { - fn to_string(&self) -> String { - match self { - DownloadArchiveQueryData::Sequence { id, file_type } => format!("da_s_{id}_{file_type}"), - DownloadArchiveQueryData::Author { id, file_type } => format!("da_a_{id}_{file_type}"), - DownloadArchiveQueryData::Translator { id, file_type } => format!("da_t_{id}_{file_type}"), - } - } -} - -impl FromStr for DownloadArchiveQueryData { - 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 id: u32 = caps["id"].parse().unwrap(); - let file_type: String = caps["file_type"].to_string(); - - Ok( - match caps["obj_type"].to_string().as_str() { - "s" => DownloadArchiveQueryData::Sequence { id, file_type }, - "a" => DownloadArchiveQueryData::Author { id, file_type }, - "t" => DownloadArchiveQueryData::Translator { id, file_type }, - _ => return Err(strum::ParseError::VariantNotFound) - } - ) - } -} - - -#[derive(Clone)] -pub struct CheckArchiveStatus { - pub task_id: String -} - -impl ToString for CheckArchiveStatus { - fn to_string(&self) -> String { - format!("check_da_{}", self.task_id) - } -} - -impl FromStr for CheckArchiveStatus { - type Err = strum::ParseError; - - fn from_str(s: &str) -> Result { - let re = Regex::new(r"^check_da_(?P\w+)$").unwrap(); - - let caps = re.captures(s); - let caps = match caps { - Some(v) => v, - None => return Err(strum::ParseError::VariantNotFound), - }; - - let task_id: String = caps["task_id"].parse().unwrap(); - - Ok(CheckArchiveStatus { task_id }) - } -} +use super::utils::filter_command; fn get_check_keyboard(task_id: String) -> InlineKeyboardMarkup { diff --git a/src/bots/approved_bot/services/book_cache/mod.rs b/src/bots/approved_bot/services/book_cache/mod.rs index 99de4bf..53d35f0 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::{config, bots::approved_bot::modules::download::DownloadQueryData}; +use crate::{config, bots::approved_bot::modules::download::callback_data::DownloadQueryData}; use self::types::{CachedMessage, DownloadFile, DownloadLink}; diff --git a/src/bots/approved_bot/services/book_library/formaters.rs b/src/bots/approved_bot/services/book_library/formaters.rs index 1a78541..cb782ba 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::{StartDownloadCommand, DownloadArchiveCommand}; +use crate::bots::approved_bot::modules::download::commads::{StartDownloadCommand, DownloadArchiveCommand}; use super::types::{ Author, AuthorBook, Book, BookAuthor, BookGenre, SearchBook, Sequence, Translator,