mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-20 13:59:00 +01:00
Add DialogueWithCx::unpack
This commit is contained in:
parent
f3fb72d527
commit
f95826e9f8
2 changed files with 16 additions and 10 deletions
|
@ -2,21 +2,18 @@ use teloxide::prelude::*;
|
||||||
|
|
||||||
use super::{favourite_music::FavouriteMusic, states::*};
|
use super::{favourite_music::FavouriteMusic, states::*};
|
||||||
|
|
||||||
pub type Cx<State> =
|
pub type Cx<State> = DialogueWithCx<Message, State, std::convert::Infallible>;
|
||||||
DialogueWithCx<Message, State, std::convert::Infallible>;
|
|
||||||
pub type Res = ResponseResult<DialogueStage<Wrapper>>;
|
pub type Res = ResponseResult<DialogueStage<Wrapper>>;
|
||||||
|
|
||||||
pub async fn start(cx: Cx<StartState>) -> Res {
|
pub async fn start(cx: Cx<StartState>) -> Res {
|
||||||
let DialogueWithCx { cx, dialogue } = cx;
|
let (cx, dialogue) = cx.unpack();
|
||||||
let dialogue = dialogue.unwrap();
|
|
||||||
|
|
||||||
cx.answer("Let's start! First, what's your full name?").send().await?;
|
cx.answer("Let's start! First, what's your full name?").send().await?;
|
||||||
next(dialogue.up())
|
next(dialogue.up())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn receive_full_name(cx: Cx<ReceiveFullNameState>) -> Res {
|
pub async fn receive_full_name(cx: Cx<ReceiveFullNameState>) -> Res {
|
||||||
let DialogueWithCx { cx, dialogue } = cx;
|
let (cx, dialogue) = cx.unpack();
|
||||||
let dialogue = dialogue.unwrap();
|
|
||||||
|
|
||||||
match cx.update.text_owned() {
|
match cx.update.text_owned() {
|
||||||
Some(full_name) => {
|
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 {
|
pub async fn receive_age(cx: Cx<ReceiveAgeState>) -> Res {
|
||||||
let DialogueWithCx { cx, dialogue } = cx;
|
let (cx, dialogue) = cx.unpack();
|
||||||
let dialogue = dialogue.unwrap();
|
|
||||||
|
|
||||||
match cx.update.text().unwrap().parse() {
|
match cx.update.text().unwrap().parse() {
|
||||||
Ok(age) => {
|
Ok(age) => {
|
||||||
|
@ -52,8 +48,7 @@ pub async fn receive_age(cx: Cx<ReceiveAgeState>) -> Res {
|
||||||
pub async fn receive_favourite_music(
|
pub async fn receive_favourite_music(
|
||||||
cx: Cx<ReceiveFavouriteMusicState>,
|
cx: Cx<ReceiveFavouriteMusicState>,
|
||||||
) -> Res {
|
) -> Res {
|
||||||
let DialogueWithCx { cx, dialogue } = cx;
|
let (cx, dialogue) = cx.unpack();
|
||||||
let dialogue = dialogue.unwrap();
|
|
||||||
|
|
||||||
match cx.update.text().unwrap().parse() {
|
match cx.update.text().unwrap().parse() {
|
||||||
Ok(favourite_music) => {
|
Ok(favourite_music) => {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::dispatching::{dialogue::GetChatId, UpdateWithCx};
|
use crate::dispatching::{dialogue::GetChatId, UpdateWithCx};
|
||||||
|
use std::fmt::Debug;
|
||||||
|
|
||||||
/// A context of a [`DialogueDispatcher`]'s message handler.
|
/// A context of a [`DialogueDispatcher`]'s message handler.
|
||||||
///
|
///
|
||||||
|
@ -12,6 +13,16 @@ pub struct DialogueWithCx<Upd, D, E> {
|
||||||
pub dialogue: Result<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> {
|
impl<Upd, D, E> DialogueWithCx<Upd, D, E> {
|
||||||
/// Creates a new instance with the provided fields.
|
/// Creates a new instance with the provided fields.
|
||||||
pub fn new(cx: UpdateWithCx<Upd>, dialogue: D) -> Self {
|
pub fn new(cx: UpdateWithCx<Upd>, dialogue: D) -> Self {
|
||||||
|
|
Loading…
Add table
Reference in a new issue