Add DialogueWithCx::unpack

This commit is contained in:
Temirkhan Myrzamadi 2020-05-26 15:05:22 +06:00
parent f3fb72d527
commit f95826e9f8
2 changed files with 16 additions and 10 deletions

View file

@ -2,21 +2,18 @@ use teloxide::prelude::*;
use super::{favourite_music::FavouriteMusic, states::*};
pub type Cx<State> =
DialogueWithCx<Message, State, std::convert::Infallible>;
pub type Cx<State> = DialogueWithCx<Message, State, std::convert::Infallible>;
pub type Res = ResponseResult<DialogueStage<Wrapper>>;
pub async fn start(cx: Cx<StartState>) -> 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<ReceiveFullNameState>) -> 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<ReceiveFullNameState>) -> Res {
}
pub async fn receive_age(cx: Cx<ReceiveAgeState>) -> 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<ReceiveAgeState>) -> Res {
pub async fn receive_favourite_music(
cx: Cx<ReceiveFavouriteMusicState>,
) -> Res {
let DialogueWithCx { cx, dialogue } = cx;
let dialogue = dialogue.unwrap();
let (cx, dialogue) = cx.unpack();
match cx.update.text().unwrap().parse() {
Ok(favourite_music) => {

View file

@ -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<Upd, D, E> {
pub dialogue: Result<D, E>,
}
impl<Upd, D, E> DialogueWithCx<Upd, D, E> {
/// Returns the inner `UpdateWithCx<Upd>` and an unwrapped dialogue.
pub fn unpack(self) -> (UpdateWithCx<Upd>, D)
where
E: Debug,
{
(self.cx, self.dialogue.unwrap())
}
}
impl<Upd, D, E> DialogueWithCx<Upd, D, E> {
/// Creates a new instance with the provided fields.
pub fn new(cx: UpdateWithCx<Upd>, dialogue: D) -> Self {