mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-31 16:40:37 +01:00
Unify TransitionIn with TransitionOut
This commit is contained in:
parent
0b203bd513
commit
18b813c159
5 changed files with 19 additions and 13 deletions
|
@ -45,7 +45,7 @@ async fn run() {
|
|||
|
||||
Dispatcher::new(bot)
|
||||
.messages_handler(DialogueDispatcher::new(
|
||||
|input: TransitionIn<Dialogue, Infallible>| async move {
|
||||
|input: DialogueWithCx<Message, Dialogue, Infallible>| async move {
|
||||
// Unwrap without panic because of std::convert::Infallible.
|
||||
input
|
||||
.dialogue
|
||||
|
|
|
@ -4,17 +4,16 @@ use crate::states::{
|
|||
};
|
||||
use teloxide::prelude::*;
|
||||
|
||||
pub type Cx = UpdateWithCx<Message>;
|
||||
pub type Out = TransitionOut<Dialogue>;
|
||||
|
||||
pub async fn start(cx: Cx, state: StartState) -> Out {
|
||||
pub async fn start(cx: TransitionIn, state: StartState) -> Out {
|
||||
cx.answer_str("Let's start our test! How many days per week are there?")
|
||||
.await?;
|
||||
next(state.up())
|
||||
}
|
||||
|
||||
pub async fn receive_days_of_week(
|
||||
cx: Cx,
|
||||
cx: TransitionIn,
|
||||
state: ReceiveDaysOfWeekState,
|
||||
) -> Out {
|
||||
match cx.update.text().map(str::parse) {
|
||||
|
@ -29,7 +28,10 @@ pub async fn receive_days_of_week(
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn receive_10x5_answer(cx: Cx, state: Receive10x5AnswerState) -> Out {
|
||||
pub async fn receive_10x5_answer(
|
||||
cx: TransitionIn,
|
||||
state: Receive10x5AnswerState,
|
||||
) -> Out {
|
||||
match cx.update.text().map(str::parse) {
|
||||
Some(Ok(ans)) if ans == 50 => {
|
||||
cx.answer_str("What's an alternative name of Gandalf?").await?;
|
||||
|
@ -43,7 +45,7 @@ pub async fn receive_10x5_answer(cx: Cx, state: Receive10x5AnswerState) -> Out {
|
|||
}
|
||||
|
||||
pub async fn receive_gandalf_alternative_name(
|
||||
cx: Cx,
|
||||
cx: TransitionIn,
|
||||
state: ReceiveGandalfAlternativeNameState,
|
||||
) -> Out {
|
||||
match cx.update.text() {
|
||||
|
|
|
@ -25,7 +25,7 @@ enum Error {
|
|||
StorageError(#[from] StorageError),
|
||||
}
|
||||
|
||||
type In = TransitionIn<Dialogue, StorageError>;
|
||||
type In = DialogueWithCx<Message, Dialogue, StorageError>;
|
||||
|
||||
async fn handle_message(input: In) -> Out {
|
||||
let (cx, dialogue) = input.unpack();
|
||||
|
|
|
@ -2,10 +2,9 @@ use teloxide::prelude::*;
|
|||
|
||||
use super::states::*;
|
||||
|
||||
pub type Cx = UpdateWithCx<Message>;
|
||||
pub type Out = TransitionOut<Dialogue>;
|
||||
|
||||
async fn start(cx: Cx, state: StartState, text: &str) -> Out {
|
||||
async fn start(cx: TransitionIn, state: StartState, text: &str) -> Out {
|
||||
if let Ok(number) = text.parse() {
|
||||
cx.answer_str(format!(
|
||||
"Remembered number {}. Now use /get or /reset",
|
||||
|
@ -19,7 +18,11 @@ async fn start(cx: Cx, state: StartState, text: &str) -> Out {
|
|||
}
|
||||
}
|
||||
|
||||
async fn have_number(cx: Cx, state: HaveNumberState, text: &str) -> Out {
|
||||
async fn have_number(
|
||||
cx: TransitionIn,
|
||||
state: HaveNumberState,
|
||||
text: &str,
|
||||
) -> Out {
|
||||
let num = state.number;
|
||||
|
||||
if text.starts_with("/get") {
|
||||
|
@ -34,7 +37,7 @@ async fn have_number(cx: Cx, state: HaveNumberState, text: &str) -> Out {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn dispatch(cx: Cx, dialogue: Dialogue, text: &str) -> Out {
|
||||
pub async fn dispatch(cx: TransitionIn, dialogue: Dialogue, text: &str) -> Out {
|
||||
match dialogue {
|
||||
Dialogue::Start(state) => start(cx, state, text).await,
|
||||
Dialogue::HaveNumber(state) => have_number(cx, state, text).await,
|
||||
|
|
|
@ -61,6 +61,7 @@ pub use get_chat_id::GetChatId;
|
|||
#[cfg(feature = "redis-storage")]
|
||||
pub use storage::{RedisStorage, RedisStorageError};
|
||||
|
||||
use crate::dispatching::UpdateWithCx;
|
||||
pub use storage::{serializer, InMemStorage, Serializer, Storage};
|
||||
|
||||
/// Generates `.up(field)` methods for dialogue states.
|
||||
|
@ -112,8 +113,8 @@ macro_rules! up {
|
|||
};
|
||||
}
|
||||
|
||||
/// A type passed into a FSM transition function.
|
||||
pub type TransitionIn<State, E> = DialogueWithCx<Message, State, E>;
|
||||
/// An input passed into a FSM transition function.
|
||||
pub type TransitionIn = UpdateWithCx<Message>;
|
||||
|
||||
// A type returned from a FSM transition function.
|
||||
pub type TransitionOut<D> = ResponseResult<DialogueStage<D>>;
|
||||
|
|
Loading…
Reference in a new issue