Simplify .commands()

This commit is contained in:
Temirkhan Myrzamadi 2020-02-19 16:05:01 +06:00
parent 46b20f676e
commit 332da2c3dd
5 changed files with 14 additions and 22 deletions

View file

@ -146,10 +146,8 @@ async fn answer(
} }
async fn handle_command(rx: DispatcherHandlerRx<Message>) { async fn handle_command(rx: DispatcherHandlerRx<Message>) {
// Only iterate through text messages: // Only iterate through commands in a proper format:
rx.text_messages() rx.commands::<Command>()
// Only iterate through commands in a proper format:
.commands::<Command>()
// Execute all incoming commands concurrently: // Execute all incoming commands concurrently:
.for_each_concurrent(None, |(cx, command, _)| async move { .for_each_concurrent(None, |(cx, command, _)| async move {
answer(cx, command).await.log_on_error().await; answer(cx, command).await.log_on_error().await;
@ -182,7 +180,7 @@ TELOXIDE_TOKEN=<Your token here> cargo run
</div> </div>
See? The dispatcher gives us a stream of messages, so we can handle it as we want! Here we use our `.text_messages()`, `.commands()`, and [`.for_each_concurrent()`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.for_each_concurrent), but others are also available: See? The dispatcher gives us a stream of messages, so we can handle it as we want! Here we use our `.commands::<Command>()` and [`.for_each_concurrent()`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.for_each_concurrent), but others are also available:
- [`.flatten()`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.flatten) - [`.flatten()`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.flatten)
- [`.left_stream()`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.left_stream) - [`.left_stream()`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.left_stream)
- [`.scan()`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.scan) - [`.scan()`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.scan)

View file

@ -178,8 +178,6 @@ async fn action(
async fn handle_commands(rx: DispatcherHandlerRx<Message>) { async fn handle_commands(rx: DispatcherHandlerRx<Message>) {
// Only iterate through messages from groups: // Only iterate through messages from groups:
rx.filter(|cx| future::ready(cx.update.chat.is_group())) rx.filter(|cx| future::ready(cx.update.chat.is_group()))
// Only iterate through text messages:
.text_messages()
// Only iterate through commands in a proper format: // Only iterate through commands in a proper format:
.commands::<Command>() .commands::<Command>()
// Execute all incoming commands concurrently: // Execute all incoming commands concurrently:

View file

@ -31,10 +31,8 @@ async fn answer(
} }
async fn handle_command(rx: DispatcherHandlerRx<Message>) { async fn handle_command(rx: DispatcherHandlerRx<Message>) {
// Only iterate through text messages: // Only iterate through commands in a proper format:
rx.text_messages() rx.commands::<Command>()
// Only iterate through commands in a proper format:
.commands::<Command>()
// Execute all incoming commands concurrently: // Execute all incoming commands concurrently:
.for_each_concurrent(None, |(cx, command, _)| async move { .for_each_concurrent(None, |(cx, command, _)| async move {
answer(cx, command).await.log_on_error().await; answer(cx, command).await.log_on_error().await;

View file

@ -7,20 +7,20 @@ use futures::{stream::BoxStream, Stream, StreamExt};
/// ///
/// [`DispatcherHandlerRx`]: crate:dispatching::DispatcherHandlerRx /// [`DispatcherHandlerRx`]: crate:dispatching::DispatcherHandlerRx
pub trait DispatcherHandlerRxExt { pub trait DispatcherHandlerRxExt {
/// Extracts only text messages from this stream. /// Extracts only text messages from this stream of arbitrary messages.
fn text_messages( fn text_messages(
self, self,
) -> BoxStream<'static, (DispatcherHandlerCx<Message>, String)> ) -> BoxStream<'static, (DispatcherHandlerCx<Message>, String)>
where where
Self: Stream<Item = DispatcherHandlerCx<Message>>; Self: Stream<Item = DispatcherHandlerCx<Message>>;
/// Extracts only commands with their arguments from this stream of text /// Extracts only commands with their arguments from this stream of
/// messages. /// arbitrary messages.
fn commands<C>( fn commands<C>(
self, self,
) -> BoxStream<'static, (DispatcherHandlerCx<Message>, C, Vec<String>)> ) -> BoxStream<'static, (DispatcherHandlerCx<Message>, C, Vec<String>)>
where where
Self: Stream<Item = (DispatcherHandlerCx<Message>, String)>, Self: Stream<Item = DispatcherHandlerCx<Message>>,
C: BotCommand; C: BotCommand;
} }
@ -43,10 +43,10 @@ where
self, self,
) -> BoxStream<'static, (DispatcherHandlerCx<Message>, C, Vec<String>)> ) -> BoxStream<'static, (DispatcherHandlerCx<Message>, C, Vec<String>)>
where where
Self: Stream<Item = (DispatcherHandlerCx<Message>, String)>, Self: Stream<Item = DispatcherHandlerCx<Message>>,
C: BotCommand, C: BotCommand,
{ {
Box::pin(self.filter_map(|(cx, text)| async move { Box::pin(self.text_messages().filter_map(|(cx, text)| async move {
C::parse(&text).map(|(command, args)| { C::parse(&text).map(|(command, args)| {
( (
cx, cx,

View file

@ -132,10 +132,8 @@
//! } //! }
//! //!
//! async fn handle_command(rx: DispatcherHandlerRx<Message>) { //! async fn handle_command(rx: DispatcherHandlerRx<Message>) {
//! // Only iterate through text messages: //! // Only iterate through commands in a proper format:
//! rx.text_messages() //! rx.commands::<Command>()
//! // Only iterate through commands in a proper format:
//! .commands::<Command>()
//! // Execute all incoming commands concurrently: //! // Execute all incoming commands concurrently:
//! .for_each_concurrent(None, |(cx, command, _)| async move { //! .for_each_concurrent(None, |(cx, command, _)| async move {
//! answer(cx, command).await.log_on_error().await; //! answer(cx, command).await.log_on_error().await;
@ -173,7 +171,7 @@
//! //!
//! //!
//! See? The dispatcher gives us a stream of messages, so we can handle it as we //! See? The dispatcher gives us a stream of messages, so we can handle it as we
//! want! Here we use our `.text_messages()`, `.commands()`, and //! want! Here we use our `.commands::<Command>()` and
//! [`.for_each_concurrent()`], but others are also available: //! [`.for_each_concurrent()`], but others are also available:
//! - [`.flatten()`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.flatten) //! - [`.flatten()`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.flatten)
//! - [`.left_stream()`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.left_stream) //! - [`.left_stream()`](https://docs.rs/futures/0.3.4/futures/stream/trait.StreamExt.html#method.left_stream)