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>) {
// Only iterate through text messages:
rx.text_messages()
// Only iterate through commands in a proper format:
.commands::<Command>()
// Only iterate through commands in a proper format:
rx.commands::<Command>()
// Execute all incoming commands concurrently:
.for_each_concurrent(None, |(cx, command, _)| async move {
answer(cx, command).await.log_on_error().await;
@ -182,7 +180,7 @@ TELOXIDE_TOKEN=<Your token here> cargo run
</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)
- [`.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)

View file

@ -178,8 +178,6 @@ async fn action(
async fn handle_commands(rx: DispatcherHandlerRx<Message>) {
// Only iterate through messages from groups:
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:
.commands::<Command>()
// Execute all incoming commands concurrently:

View file

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

View file

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

View file

@ -132,10 +132,8 @@
//! }
//!
//! async fn handle_command(rx: DispatcherHandlerRx<Message>) {
//! // Only iterate through text messages:
//! rx.text_messages()
//! // Only iterate through commands in a proper format:
//! .commands::<Command>()
//! // Only iterate through commands in a proper format:
//! rx.commands::<Command>()
//! // Execute all incoming commands concurrently:
//! .for_each_concurrent(None, |(cx, command, _)| async move {
//! 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
//! 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:
//! - [`.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)