From b0940df9bdf642c5905cc68fc80e9959d0d3f451 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Sun, 24 May 2020 01:02:17 +0600 Subject: [PATCH] Avoid repetition in DialogueDispatcherHandlerCx --- .../dialogue/dialogue_dispatcher.rs | 8 +- .../dialogue_dispatcher_handler_cx.rs | 160 +----------------- 2 files changed, 8 insertions(+), 160 deletions(-) diff --git a/src/dispatching/dialogue/dialogue_dispatcher.rs b/src/dispatching/dialogue/dialogue_dispatcher.rs index 0181a564..c13c131c 100644 --- a/src/dispatching/dialogue/dialogue_dispatcher.rs +++ b/src/dispatching/dialogue/dialogue_dispatcher.rs @@ -99,11 +99,7 @@ where .map(Option::unwrap_or_default); match handler - .handle(DialogueDispatcherHandlerCx { - bot: cx.bot, - update: cx.update, - dialogue, - }) + .handle(DialogueDispatcherHandlerCx { cx, dialogue }) .await { DialogueStage::Next(new_dialogue) => { @@ -224,7 +220,7 @@ mod tests { |cx: DialogueDispatcherHandlerCx| async move { delay_for(Duration::from_millis(300)).await; - match cx.update { + match cx.cx.update { MyUpdate { chat_id: 1, unique_number } => { SEQ1.lock().await.push(unique_number); } diff --git a/src/dispatching/dialogue/dialogue_dispatcher_handler_cx.rs b/src/dispatching/dialogue/dialogue_dispatcher_handler_cx.rs index 7c76db74..145a6b68 100644 --- a/src/dispatching/dialogue/dialogue_dispatcher_handler_cx.rs +++ b/src/dispatching/dialogue/dialogue_dispatcher_handler_cx.rs @@ -1,15 +1,4 @@ -use crate::{ - dispatching::dialogue::GetChatId, - requests::{ - DeleteMessage, EditMessageCaption, EditMessageText, ForwardMessage, - PinChatMessage, SendAnimation, SendAudio, SendContact, SendDocument, - SendLocation, SendMediaGroup, SendMessage, SendPhoto, SendSticker, - SendVenue, SendVideo, SendVideoNote, SendVoice, - }, - types::{ChatId, ChatOrInlineMessage, InputFile, InputMedia, Message}, - Bot, -}; -use std::sync::Arc; +use crate::dispatching::{dialogue::GetChatId, DispatcherHandlerCx}; /// A context of a [`DialogueDispatcher`]'s message handler. /// @@ -19,15 +8,14 @@ use std::sync::Arc; /// [`DialogueDispatcher`]: crate::dispatching::dialogue::DialogueDispatcher #[derive(Debug)] pub struct DialogueDispatcherHandlerCx { - pub bot: Arc, - pub update: Upd, + pub cx: DispatcherHandlerCx, pub dialogue: Result, } impl DialogueDispatcherHandlerCx { /// Creates a new instance with the provided fields. - pub fn new(bot: Arc, update: Upd, dialogue: D) -> Self { - Self { bot, update, dialogue: Ok(dialogue) } + pub fn new(cx: DispatcherHandlerCx, dialogue: D) -> Self { + Self { cx, dialogue: Ok(dialogue) } } /// Creates a new instance by substituting a dialogue and preserving @@ -36,11 +24,7 @@ impl DialogueDispatcherHandlerCx { self, new_dialogue: Result, ) -> DialogueDispatcherHandlerCx { - DialogueDispatcherHandlerCx { - bot: self.bot, - update: self.update, - dialogue: new_dialogue, - } + DialogueDispatcherHandlerCx { cx: self.cx, dialogue: new_dialogue } } } @@ -49,138 +33,6 @@ where Upd: GetChatId, { fn chat_id(&self) -> i64 { - self.update.chat_id() - } -} - -impl DialogueDispatcherHandlerCx { - pub fn answer(&self, text: T) -> SendMessage - where - T: Into, - { - self.bot.send_message(self.chat_id(), text) - } - - pub fn reply_to(&self, text: T) -> SendMessage - where - T: Into, - { - self.bot - .send_message(self.chat_id(), text) - .reply_to_message_id(self.update.id) - } - - pub fn answer_photo(&self, photo: InputFile) -> SendPhoto { - self.bot.send_photo(self.update.chat.id, photo) - } - - pub fn answer_audio(&self, audio: InputFile) -> SendAudio { - self.bot.send_audio(self.update.chat.id, audio) - } - - pub fn answer_animation(&self, animation: InputFile) -> SendAnimation { - self.bot.send_animation(self.update.chat.id, animation) - } - - pub fn answer_document(&self, document: InputFile) -> SendDocument { - self.bot.send_document(self.update.chat.id, document) - } - - pub fn answer_video(&self, video: InputFile) -> SendVideo { - self.bot.send_video(self.update.chat.id, video) - } - - pub fn answer_voice(&self, voice: InputFile) -> SendVoice { - self.bot.send_voice(self.update.chat.id, voice) - } - - pub fn answer_media_group(&self, media_group: T) -> SendMediaGroup - where - T: Into>, - { - self.bot.send_media_group(self.update.chat.id, media_group) - } - - pub fn answer_location( - &self, - latitude: f32, - longitude: f32, - ) -> SendLocation { - self.bot.send_location(self.update.chat.id, latitude, longitude) - } - - pub fn answer_venue( - &self, - latitude: f32, - longitude: f32, - title: T, - address: U, - ) -> SendVenue - where - T: Into, - U: Into, - { - self.bot.send_venue( - self.update.chat.id, - latitude, - longitude, - title, - address, - ) - } - - pub fn answer_video_note(&self, video_note: InputFile) -> SendVideoNote { - self.bot.send_video_note(self.update.chat.id, video_note) - } - - pub fn answer_contact( - &self, - phone_number: T, - first_name: U, - ) -> SendContact - where - T: Into, - U: Into, - { - self.bot.send_contact(self.chat_id(), phone_number, first_name) - } - - pub fn answer_sticker(&self, sticker: InputFile) -> SendSticker { - self.bot.send_sticker(self.update.chat.id, sticker) - } - - pub fn forward_to(&self, chat_id: T) -> ForwardMessage - where - T: Into, - { - self.bot.forward_message(chat_id, self.update.chat.id, self.update.id) - } - - pub fn edit_message_text(&self, text: T) -> EditMessageText - where - T: Into, - { - self.bot.edit_message_text( - ChatOrInlineMessage::Chat { - chat_id: self.update.chat.id.into(), - message_id: self.update.id, - }, - text, - ) - } - - pub fn edit_message_caption(&self) -> EditMessageCaption { - self.bot.edit_message_caption(ChatOrInlineMessage::Chat { - chat_id: self.update.chat.id.into(), - message_id: self.update.id, - }) - } - - pub fn delete_message(&self) -> DeleteMessage { - self.bot.delete_message(self.update.chat.id, self.update.id) - } - - pub fn pin_message(&self) -> PinChatMessage { - self.bot.pin_chat_message(self.update.chat.id, self.update.id) + self.cx.update.chat_id() } }