Remove DialogueWithCx::unpack

This commit is contained in:
Temirkhan Myrzamadi 2020-07-26 13:46:43 +06:00
parent 19730fb923
commit 477d8a67d3
5 changed files with 26 additions and 33 deletions

View file

@ -305,12 +305,13 @@ async fn main() {
let bot = Bot::from_env();
Dispatcher::new(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!")
}))
.messages_handler(DialogueDispatcher::new(
|DialogueWithCx { cx, dialogue }: In| async move {
// No panic because of std::convert::Infallible.
let dialogue = dialogue.unwrap();
dialogue.react(cx).await.expect("Something wrong with the bot!")
},
))
.dispatch()
.await;
}

View file

@ -49,12 +49,13 @@ async fn run() {
let bot = Bot::from_env();
Dispatcher::new(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!")
}))
.messages_handler(DialogueDispatcher::new(
|DialogueWithCx { cx, dialogue }: In| async move {
// No panic because of std::convert::Infallible.
let dialogue = dialogue.unwrap();
dialogue.react(cx).await.expect("Something wrong with the bot!")
},
))
.dispatch()
.await;
}

View file

@ -35,9 +35,9 @@ async fn run() {
let bot = Bot::from_env();
Dispatcher::new(bot)
.messages_handler(DialogueDispatcher::with_storage(
|input: In| async move {
|DialogueWithCx { cx, dialogue }: In| async move {
// No panic because of std::convert::Infallible.
let (cx, dialogue) = input.unpack();
let dialogue = dialogue.unwrap();
dialogue
.react(cx)

View file

@ -13,19 +13,6 @@ pub struct DialogueWithCx<Upd, D, E> {
pub dialogue: Result<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,
{
(self.cx, self.dialogue.unwrap())
}
}
impl<Upd, D, E> DialogueWithCx<Upd, D, E> {
/// Creates a new instance with the provided fields.
pub fn new(cx: UpdateWithCx<Upd>, dialogue: D) -> Self {

View file

@ -83,12 +83,16 @@
//! let bot = Bot::from_env();
//!
//! Dispatcher::new(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!")
//! }))
//! .messages_handler(DialogueDispatcher::new(
//! |DialogueWithCx { cx, dialogue }: In| async move {
//! // No panic because of std::convert::Infallible.
//! let dialogue = dialogue.unwrap();
//! dialogue
//! .react(cx)
//! .await
//! .expect("Something wrong with the bot!")
//! },
//! ))
//! .dispatch()
//! .await;
//! }