From 18b813c159823d4c2584afb156b7afe341fb6989 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Fri, 24 Jul 2020 19:26:49 +0600 Subject: [PATCH] Unify TransitionIn with TransitionOut --- examples/dialogue_bot/src/main.rs | 2 +- examples/dialogue_bot/src/transitions.rs | 12 +++++++----- examples/redis_remember_bot/src/main.rs | 2 +- examples/redis_remember_bot/src/transitions.rs | 11 +++++++---- src/dispatching/dialogue/mod.rs | 5 +++-- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/examples/dialogue_bot/src/main.rs b/examples/dialogue_bot/src/main.rs index 8c7d80d5..6fb15ace 100644 --- a/examples/dialogue_bot/src/main.rs +++ b/examples/dialogue_bot/src/main.rs @@ -45,7 +45,7 @@ async fn run() { Dispatcher::new(bot) .messages_handler(DialogueDispatcher::new( - |input: TransitionIn| async move { + |input: DialogueWithCx| async move { // Unwrap without panic because of std::convert::Infallible. input .dialogue diff --git a/examples/dialogue_bot/src/transitions.rs b/examples/dialogue_bot/src/transitions.rs index d98d8aed..a3265652 100644 --- a/examples/dialogue_bot/src/transitions.rs +++ b/examples/dialogue_bot/src/transitions.rs @@ -4,17 +4,16 @@ use crate::states::{ }; use teloxide::prelude::*; -pub type Cx = UpdateWithCx; pub type Out = TransitionOut; -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() { diff --git a/examples/redis_remember_bot/src/main.rs b/examples/redis_remember_bot/src/main.rs index bffaa8aa..2b86b295 100644 --- a/examples/redis_remember_bot/src/main.rs +++ b/examples/redis_remember_bot/src/main.rs @@ -25,7 +25,7 @@ enum Error { StorageError(#[from] StorageError), } -type In = TransitionIn; +type In = DialogueWithCx; async fn handle_message(input: In) -> Out { let (cx, dialogue) = input.unpack(); diff --git a/examples/redis_remember_bot/src/transitions.rs b/examples/redis_remember_bot/src/transitions.rs index 594471e1..c294e9e4 100644 --- a/examples/redis_remember_bot/src/transitions.rs +++ b/examples/redis_remember_bot/src/transitions.rs @@ -2,10 +2,9 @@ use teloxide::prelude::*; use super::states::*; -pub type Cx = UpdateWithCx; pub type Out = TransitionOut; -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, diff --git a/src/dispatching/dialogue/mod.rs b/src/dispatching/dialogue/mod.rs index 36b700b4..2687d489 100644 --- a/src/dispatching/dialogue/mod.rs +++ b/src/dispatching/dialogue/mod.rs @@ -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 = DialogueWithCx; +/// An input passed into a FSM transition function. +pub type TransitionIn = UpdateWithCx; // A type returned from a FSM transition function. pub type TransitionOut = ResponseResult>;