Small refinements

This commit is contained in:
Temirkhan Myrzamadi 2020-07-26 13:18:29 +06:00
parent b255dfaf61
commit 71f38bcb92
6 changed files with 34 additions and 35 deletions

View file

@ -53,8 +53,7 @@ serde_with_macros = "1.1.0"
redis = { version = "0.16.0", optional = true }
serde_cbor = { version = "0.11.1", optional = true }
bincode = { version = "1.3.1", optional = true }
#frunk = { git = "https://github.com/Hirrolot/frunk", branch = "append-to-hlist", optional = true }
frunk = { path = "../../Desktop/frunk", optional = true }
frunk = { git = "https://github.com/Hirrolot/frunk", branch = "append-to-hlist", optional = true }
#teloxide-macros = "0.3.1"
teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" }

View file

@ -295,6 +295,8 @@ Finally, the `main` function looks like this:
```rust
// Imports are omitted...
type In = DialogueWithCx<Message, Dialogue, Infallible>;
#[tokio::main]
async fn main() {
teloxide::enable_logging!();
@ -303,17 +305,12 @@ async fn main() {
let bot = Bot::from_env();
Dispatcher::new(bot)
.messages_handler(DialogueDispatcher::new(
|input: DialogueWithCx<Message, Dialogue, Infallible>| async move {
// Unwrap without panic because of std::convert::Infallible.
input
.dialogue
.unwrap()
.react(input.cx)
.await
.expect("Something wrong with the bot!")
},
))
.messages_handler(DialogueDispatcher::new(|input: In| async move {
// No panic because of std::convert::Infallible.
let (cx, dialogue) = input.unpack();
dialogue.react(cx).await.expect("Something wrong with the bot!")
}))
.dispatch()
.await;
}

View file

@ -10,16 +10,15 @@ edition = "2018"
log = "0.4.8"
tokio = "0.2.9"
frunk = { path = "../../../../Desktop/frunk" }
frunk_core = { path = "../../../../Desktop/frunk/core" }
frunk = { git = "https://github.com/Hirrolot/frunk", branch = "append-to-hlist" }
frunk-core = { git = "https://github.com/Hirrolot/frunk/tree/master/core", branch = "append-to-hlist" }
pretty_env_logger = "0.4.0"
futures = "0.3.5"
smart-default = "0.6.0"
derive_more = "0.99.9"
teloxide = { path = "../../", features = ["frunk"] }
#teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" }
teloxide-macros = { path = "../../../teloxide-macros" }
teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" }
[profile.release]
lto = true

View file

@ -2,13 +2,16 @@
//
// # Example
// ```
// - Let's start our test! How many days per week are there?
// - 7
// - 10*5 = ?
// - 50
// - What's an alternative name of Gandalf?
// - Mithrandir
// - Congratulations! You've successfully passed the test!
// - Hey
// - Let's start! What's your full name?
// - Gandalf the Grey
// - How old are you?
// - 223
// - What's your location?
// - Middle-earth
// - Full name: Gandalf the Grey
// Age: 223
// Location: Middle-earth
// ```
#![allow(clippy::trivial_regex)]
@ -32,6 +35,8 @@ use states::*;
use std::convert::Infallible;
use teloxide::prelude::*;
type In = DialogueWithCx<Message, Dialogue, Infallible>;
#[tokio::main]
async fn main() {
run().await;
@ -44,17 +49,12 @@ async fn run() {
let bot = Bot::from_env();
Dispatcher::new(bot)
.messages_handler(DialogueDispatcher::new(
|input: DialogueWithCx<Message, Dialogue, Infallible>| async move {
// Unwrap without panic because of std::convert::Infallible.
input
.dialogue
.unwrap()
.react(input.cx)
.await
.expect("Something wrong with the bot!")
},
))
.messages_handler(DialogueDispatcher::new(|input: In| async move {
// No panic because of std::convert::Infallible.
let (cx, dialogue) = input.unpack();
dialogue.react(cx).await.expect("Something wrong with the bot!")
}))
.dispatch()
.await;
}

View file

@ -36,6 +36,7 @@ async fn run() {
Dispatcher::new(bot)
.messages_handler(DialogueDispatcher::with_storage(
|input: In| async move {
// No panic because of std::convert::Infallible.
let (cx, dialogue) = input.unpack();
dialogue

View file

@ -15,6 +15,9 @@ pub struct DialogueWithCx<Upd, D, E> {
impl<Upd, D, E> DialogueWithCx<Upd, D, E> {
/// Returns the inner `UpdateWithCx<Upd>` and an unwrapped dialogue.
///
/// # Panics
/// If `self.dialogue` is `Err`.
pub fn unpack(self) -> (UpdateWithCx<Upd>, D)
where
E: Debug,