From 0ca115976bb4a41f4839df5da29ecf54582bcc58 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Sat, 2 Sep 2023 14:33:26 +0200 Subject: [PATCH] Refactor --- Cargo.lock | 20 ++-- .../modules/book/callback_data.rs | 61 +++++++++++ .../approved_bot/modules/book/commands.rs | 36 +++++++ .../modules/{book.rs => book/mod.rs} | 100 ++---------------- src/bots/mod.rs | 11 +- 5 files changed, 122 insertions(+), 106 deletions(-) create mode 100644 src/bots/approved_bot/modules/book/callback_data.rs create mode 100644 src/bots/approved_bot/modules/book/commands.rs rename src/bots/approved_bot/modules/{book.rs => book/mod.rs} (71%) diff --git a/Cargo.lock b/Cargo.lock index db0ba51..b50acac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -215,7 +215,7 @@ dependencies = [ "tokio-stream", "tokio-util", "tower", - "tower-http 0.4.3", + "tower-http 0.4.4", "tracing", "tracing-subscriber", "url", @@ -287,9 +287,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.27" +version = "0.4.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f56b4c72906975ca04becb8a30e102dfecddd0c06181e3e95ddc444be28881f8" +checksum = "95ed24df0632f708f5f6d8082675bef2596f7084dee3dd55f632290bf35bfe0f" dependencies = [ "android-tzdata", "iana-time-zone", @@ -1005,9 +1005,9 @@ checksum = "ed1202b2a6f884ae56f04cff409ab315c5ce26b5e58d7412e484f01fd52f52ef" [[package]] name = "memchr" -version = "2.6.1" +version = "2.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f478948fd84d9f8e86967bf432640e46adfb5a4bd4f14ef7e864ab38220534ae" +checksum = "5486aed0026218e61b8a01d5fbd5a0a134649abb71a0e53b7bc088529dced86e" [[package]] name = "memoffset" @@ -1555,9 +1555,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.10" +version = "0.38.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed6248e1caa625eb708e266e06159f135e8c26f2bb7ceb72dc4b2766d0340964" +checksum = "c0c3dde1fc030af041adc40e79c0e7fbcf431dd24870053d187d7c66e4b87453" dependencies = [ "bitflags 2.4.0", "errno", @@ -2062,7 +2062,7 @@ dependencies = [ "cfg-if", "fastrand 2.0.0", "redox_syscall", - "rustix 0.38.10", + "rustix 0.38.11", "windows-sys", ] @@ -2263,9 +2263,9 @@ dependencies = [ [[package]] name = "tower-http" -version = "0.4.3" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55ae70283aba8d2a8b411c695c437fe25b8b5e44e23e780662002fc72fb47a82" +checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" dependencies = [ "bitflags 2.4.0", "bytes", diff --git a/src/bots/approved_bot/modules/book/callback_data.rs b/src/bots/approved_bot/modules/book/callback_data.rs new file mode 100644 index 0000000..66fde2b --- /dev/null +++ b/src/bots/approved_bot/modules/book/callback_data.rs @@ -0,0 +1,61 @@ +#[derive(Clone)] +pub enum BookCallbackData { + Author { id: u32, page: u32 }, + Translator { id: u32, page: u32 }, + Sequence { id: u32, page: u32 }, +} + +impl FromStr for BookCallbackData { + type Err = strum::ParseError; + + fn from_str(s: &str) -> Result { + let re = Regex::new(r"^b(?Pa|t|s)_(?P\d+)_(?P\d+)$").unwrap(); + + let caps = re.captures(s); + let caps = match caps { + Some(v) => v, + None => return Err(strum::ParseError::VariantNotFound), + }; + + let annotation_type = &caps["an_type"]; + let id = caps["id"].parse::().unwrap(); + let page = caps["page"].parse::().unwrap(); + + match annotation_type { + "a" => Ok(BookCallbackData::Author { id, page }), + "t" => Ok(BookCallbackData::Translator { id, page }), + "s" => Ok(BookCallbackData::Sequence { id, page }), + _ => Err(strum::ParseError::VariantNotFound), + } + } +} + +impl ToString for BookCallbackData { + fn to_string(&self) -> String { + match self { + BookCallbackData::Author { id, page } => format!("ba_{id}_{page}"), + BookCallbackData::Translator { id, page } => format!("bt_{id}_{page}"), + BookCallbackData::Sequence { id, page } => format!("bs_{id}_{page}"), + } + } +} + +impl GetPaginationCallbackData for BookCallbackData { + fn get_pagination_callback_data(&self, target_page: u32) -> String { + match self { + BookCallbackData::Author { id, .. } => BookCallbackData::Author { + id: *id, + page: target_page, + }, + BookCallbackData::Translator { id, .. } => BookCallbackData::Translator { + id: *id, + page: target_page, + }, + BookCallbackData::Sequence { id, .. } => BookCallbackData::Sequence { + id: *id, + page: target_page, + }, + } + .to_string() + } +} diff --git a/src/bots/approved_bot/modules/book/commands.rs b/src/bots/approved_bot/modules/book/commands.rs new file mode 100644 index 0000000..9cf3561 --- /dev/null +++ b/src/bots/approved_bot/modules/book/commands.rs @@ -0,0 +1,36 @@ +use regex::Regex; + +use crate::bots::approved_bot::modules::utils::CommandParse; + + +#[derive(Clone)] +pub enum BookCommand { + Author { id: u32 }, + Translator { id: u32 }, + Sequence { id: u32 }, +} + +impl CommandParse for BookCommand { + fn parse(s: &str, bot_name: &str) -> Result { + let re = Regex::new(r"^/(?Pa|t|s)_(?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 annotation_type = &caps["an_type"]; + let id: u32 = caps["id"].parse().unwrap(); + + match annotation_type { + "a" => Ok(BookCommand::Author { id }), + "t" => Ok(BookCommand::Translator { id }), + "s" => Ok(BookCommand::Sequence { id }), + _ => Err(strum::ParseError::VariantNotFound), + } + } +} diff --git a/src/bots/approved_bot/modules/book.rs b/src/bots/approved_bot/modules/book/mod.rs similarity index 71% rename from src/bots/approved_bot/modules/book.rs rename to src/bots/approved_bot/modules/book/mod.rs index 82d96e3..198b663 100644 --- a/src/bots/approved_bot/modules/book.rs +++ b/src/bots/approved_bot/modules/book/mod.rs @@ -1,3 +1,6 @@ +pub mod commands; +pub mod callback_data; + use core::fmt::Debug; use std::str::FromStr; @@ -19,103 +22,12 @@ use crate::bots::approved_bot::{ tools::filter_callback_query, }; +use self::commands::BookCommand; + use super::utils::{ - filter_command, generic_get_pagination_keyboard, CommandParse, GetPaginationCallbackData, + filter_command, generic_get_pagination_keyboard, GetPaginationCallbackData, }; -#[derive(Clone)] -pub enum BookCommand { - Author { id: u32 }, - Translator { id: u32 }, - Sequence { id: u32 }, -} - -impl CommandParse for BookCommand { - fn parse(s: &str, bot_name: &str) -> Result { - let re = Regex::new(r"^/(?Pa|t|s)_(?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 annotation_type = &caps["an_type"]; - let id: u32 = caps["id"].parse().unwrap(); - - match annotation_type { - "a" => Ok(BookCommand::Author { id }), - "t" => Ok(BookCommand::Translator { id }), - "s" => Ok(BookCommand::Sequence { id }), - _ => Err(strum::ParseError::VariantNotFound), - } - } -} - -#[derive(Clone)] -pub enum BookCallbackData { - Author { id: u32, page: u32 }, - Translator { id: u32, page: u32 }, - Sequence { id: u32, page: u32 }, -} - -impl FromStr for BookCallbackData { - type Err = strum::ParseError; - - fn from_str(s: &str) -> Result { - let re = Regex::new(r"^b(?Pa|t|s)_(?P\d+)_(?P\d+)$").unwrap(); - - let caps = re.captures(s); - let caps = match caps { - Some(v) => v, - None => return Err(strum::ParseError::VariantNotFound), - }; - - let annotation_type = &caps["an_type"]; - let id = caps["id"].parse::().unwrap(); - let page = caps["page"].parse::().unwrap(); - - match annotation_type { - "a" => Ok(BookCallbackData::Author { id, page }), - "t" => Ok(BookCallbackData::Translator { id, page }), - "s" => Ok(BookCallbackData::Sequence { id, page }), - _ => Err(strum::ParseError::VariantNotFound), - } - } -} - -impl ToString for BookCallbackData { - fn to_string(&self) -> String { - match self { - BookCallbackData::Author { id, page } => format!("ba_{id}_{page}"), - BookCallbackData::Translator { id, page } => format!("bt_{id}_{page}"), - BookCallbackData::Sequence { id, page } => format!("bs_{id}_{page}"), - } - } -} - -impl GetPaginationCallbackData for BookCallbackData { - fn get_pagination_callback_data(&self, target_page: u32) -> String { - match self { - BookCallbackData::Author { id, .. } => BookCallbackData::Author { - id: *id, - page: target_page, - }, - BookCallbackData::Translator { id, .. } => BookCallbackData::Translator { - id: *id, - page: target_page, - }, - BookCallbackData::Sequence { id, .. } => BookCallbackData::Sequence { - id: *id, - page: target_page, - }, - } - .to_string() - } -} async fn send_book_handler( message: Message, diff --git a/src/bots/mod.rs b/src/bots/mod.rs index f8086bc..2d07aa3 100644 --- a/src/bots/mod.rs +++ b/src/bots/mod.rs @@ -28,8 +28,15 @@ fn ignore_channel_messages() -> crate::bots::BotHandler { } fn ignore_chat_member_update() -> crate::bots::BotHandler { - Update::filter_chat_member() - .endpoint(|| async { Ok(()) }) + dptree::entry() + .branch( + Update::filter_chat_member() + .endpoint(|| async { Ok(()) }) + ) + .branch( + Update::filter_my_chat_member() + .endpoint(|| async { Ok(()) }) + ) } pub fn get_bot_handler() -> (BotHandler, BotCommands) {