From f95826e9f8b0ef02a718f72b4375000dc9cbc337 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Tue, 26 May 2020 15:05:22 +0600 Subject: [PATCH] Add DialogueWithCx::unpack --- examples/dialogue_bot/src/transitions.rs | 15 +++++---------- src/dispatching/dialogue/dialogue_with_cx.rs | 11 +++++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/examples/dialogue_bot/src/transitions.rs b/examples/dialogue_bot/src/transitions.rs index 3e5243a2..e718d260 100644 --- a/examples/dialogue_bot/src/transitions.rs +++ b/examples/dialogue_bot/src/transitions.rs @@ -2,21 +2,18 @@ use teloxide::prelude::*; use super::{favourite_music::FavouriteMusic, states::*}; -pub type Cx = - DialogueWithCx; +pub type Cx = DialogueWithCx; pub type Res = ResponseResult>; pub async fn start(cx: Cx) -> Res { - let DialogueWithCx { cx, dialogue } = cx; - let dialogue = dialogue.unwrap(); + let (cx, dialogue) = cx.unpack(); cx.answer("Let's start! First, what's your full name?").send().await?; next(dialogue.up()) } pub async fn receive_full_name(cx: Cx) -> Res { - let DialogueWithCx { cx, dialogue } = cx; - let dialogue = dialogue.unwrap(); + let (cx, dialogue) = cx.unpack(); match cx.update.text_owned() { Some(full_name) => { @@ -31,8 +28,7 @@ pub async fn receive_full_name(cx: Cx) -> Res { } pub async fn receive_age(cx: Cx) -> Res { - let DialogueWithCx { cx, dialogue } = cx; - let dialogue = dialogue.unwrap(); + let (cx, dialogue) = cx.unpack(); match cx.update.text().unwrap().parse() { Ok(age) => { @@ -52,8 +48,7 @@ pub async fn receive_age(cx: Cx) -> Res { pub async fn receive_favourite_music( cx: Cx, ) -> Res { - let DialogueWithCx { cx, dialogue } = cx; - let dialogue = dialogue.unwrap(); + let (cx, dialogue) = cx.unpack(); match cx.update.text().unwrap().parse() { Ok(favourite_music) => { diff --git a/src/dispatching/dialogue/dialogue_with_cx.rs b/src/dispatching/dialogue/dialogue_with_cx.rs index f722a62a..5ff09e88 100644 --- a/src/dispatching/dialogue/dialogue_with_cx.rs +++ b/src/dispatching/dialogue/dialogue_with_cx.rs @@ -1,4 +1,5 @@ use crate::dispatching::{dialogue::GetChatId, UpdateWithCx}; +use std::fmt::Debug; /// A context of a [`DialogueDispatcher`]'s message handler. /// @@ -12,6 +13,16 @@ pub struct DialogueWithCx { pub dialogue: Result, } +impl DialogueWithCx { + /// Returns the inner `UpdateWithCx` and an unwrapped dialogue. + pub fn unpack(self) -> (UpdateWithCx, D) + where + E: Debug, + { + (self.cx, self.dialogue.unwrap()) + } +} + impl DialogueWithCx { /// Creates a new instance with the provided fields. pub fn new(cx: UpdateWithCx, dialogue: D) -> Self {