Prettify the examples

This commit is contained in:
Temirkhan Myrzamadi 2020-03-16 17:55:49 +06:00
parent 3c55a6b0d3
commit bc89fe7b01
2 changed files with 16 additions and 18 deletions

View file

@ -153,7 +153,10 @@ async fn favourite_music(cx: Cx<ReceiveFavouriteMusicState>) -> Res {
async fn handle_message(cx: Cx<Dialogue>) -> Res {
let DialogueDispatcherHandlerCx { bot, update, dialogue } = cx;
match dialogue.unwrap() {
// You need handle the error instead of panicking in real-world code, maybe
// send diagnostics to a development chat.
match dialogue.expect("Failed to get dialogue info from storage") {
Dialogue::Start => {
start(DialogueDispatcherHandlerCx::new(bot, update, ())).await
}
@ -186,10 +189,8 @@ async fn run() {
let bot = Bot::from_env();
Dispatcher::new(bot)
.messages_handler(DialogueDispatcher::new(|cx| {
async move {
handle_message(cx).await.expect("Something wrong with the bot!")
}
.messages_handler(DialogueDispatcher::new(|cx| async move {
handle_message(cx).await.expect("Something wrong with the bot!")
}))
.dispatch()
.await;

View file

@ -21,8 +21,8 @@ extern crate smart_default;
use teloxide::prelude::*;
use std::convert::Infallible;
use rand::{thread_rng, Rng};
use std::convert::Infallible;
// ============================================================================
// [A type-safe finite automaton]
@ -80,23 +80,20 @@ async fn receive_attempt(cx: Cx<u8>) -> Res {
async fn handle_message(
cx: DialogueDispatcherHandlerCx<Message, Dialogue, Infallible>,
) -> Res {
match cx {
DialogueDispatcherHandlerCx {
bot,
update,
dialogue: Ok(Dialogue::Start),
} => start(DialogueDispatcherHandlerCx::new(bot, update, ())).await,
DialogueDispatcherHandlerCx {
bot,
update,
dialogue: Ok(Dialogue::ReceiveAttempt(secret)),
} => {
let DialogueDispatcherHandlerCx { bot, update, dialogue } = cx;
// You need handle the error instead of panicking in real-world code, maybe
// send diagnostics to a development chat.
match dialogue.expect("Failed to get dialogue info from storage") {
Dialogue::Start => {
start(DialogueDispatcherHandlerCx::new(bot, update, ())).await
}
Dialogue::ReceiveAttempt(secret) => {
receive_attempt(DialogueDispatcherHandlerCx::new(
bot, update, secret,
))
.await
}
_ => panic!("Failed to get dialogue info from storage")
}
}