Re-export dptree::case! instead of deprecation

This commit is contained in:
Hirrolot 2022-04-27 15:31:17 +06:00
parent deb992c787
commit 5a680b6001
3 changed files with 2 additions and 148 deletions

View file

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- The `dispatching::filter_command` function (also accessible as `teloxide::filter_command`) as a shortcut for `dptree::entry().filter_command()`. - The `dispatching::filter_command` function (also accessible as `teloxide::filter_command`) as a shortcut for `dptree::entry().filter_command()`.
- Re-export `dptree::case!` as `teloxide::handler!` (the former is preferred for new code).
### Changed ### Changed
@ -16,10 +17,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[Bot API 6.0]: https://core.telegram.org/bots/api#april-16-2022 [Bot API 6.0]: https://core.telegram.org/bots/api#april-16-2022
### Deprecated
- `teloxide::handler!` in favour of `dptree::case!`.
## 0.8.2 - 2022-04-26 ## 0.8.2 - 2022-04-26
### Fixed ### Fixed

View file

@ -218,146 +218,3 @@ where
} }
})) }))
} }
/// Filters an enumeration, passing its payload forwards.
///
/// This macro expands to a [`dptree::Handler`] that acts on your enumeration
/// type: if the enumeration is of a certain variant, the execution continues;
/// otherwise, `dptree` will try the next branch. This is very useful for
/// dialogue FSM transitions and Telegram command filtering; for a complete
/// example, please see [`examples/purchase.rs`].
///
/// Variants can take the following forms:
///
/// - `State::MyVariant` for empty variants;
/// - `State::MyVariant(param1, ..., paramN)` for function-like variants;
/// - `State::MyVariant { param1, ..., paramN }` for `struct`-like variants.
///
/// In the first case, this macro results in a simple [`dptree::filter`]; in the
/// second and third cases, this macro results in [`dptree::filter_map`] that
/// passes the payload of `MyVariant` to the next handler if the match occurs.
/// (This next handler can be an endpoint or a more complex one.) The payload
/// format depend on the form of `MyVariant`:
///
/// - For `State::MyVariant(param)` and `State::MyVariant { param }`, the
/// payload is `param`.
/// - For `State::MyVariant(param,)` and `State::MyVariant { param, }`, the
/// payload is `(param,)`.
/// - For `State::MyVariant(param1, ..., paramN)` and `State::MyVariant {
/// param1, ..., paramN }`, the payload is `(param1, ..., paramN)` (where
/// `N`>1).
///
/// ## Dependency requirements
///
/// - Your dialogue state enumeration `State`.
///
/// [`examples/purchase.rs`]: https://github.com/teloxide/teloxide/blob/master/examples/purchase.rs
#[deprecated(note = "Use dptree::case! instead")]
#[macro_export]
macro_rules! handler {
($($x:tt)*) => {
$crate::dptree::case![$($x)*]
};
}
#[cfg(test)]
#[allow(deprecated)]
mod tests {
use std::ops::ControlFlow;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum State {
A,
B(i32),
C(i32, &'static str),
D { foo: i32 },
E { foo: i32, bar: &'static str },
Other,
}
#[tokio::test]
async fn handler_empty_variant() {
let input = State::A;
let h: dptree::Handler<_, _> = handler![State::A].endpoint(|| async move { 123 });
assert_eq!(h.dispatch(dptree::deps![input]).await, ControlFlow::Break(123));
assert!(matches!(h.dispatch(dptree::deps![State::Other]).await, ControlFlow::Continue(_)));
}
#[tokio::test]
async fn handler_single_fn_variant() {
let input = State::B(42);
let h: dptree::Handler<_, _> = handler![State::B(x)].endpoint(|x: i32| async move {
assert_eq!(x, 42);
123
});
assert_eq!(h.dispatch(dptree::deps![input]).await, ControlFlow::Break(123));
assert!(matches!(h.dispatch(dptree::deps![State::Other]).await, ControlFlow::Continue(_)));
}
#[tokio::test]
async fn handler_single_fn_variant_trailing_comma() {
let input = State::B(42);
let h: dptree::Handler<_, _> = handler![State::B(x,)].endpoint(|(x,): (i32,)| async move {
assert_eq!(x, 42);
123
});
assert_eq!(h.dispatch(dptree::deps![input]).await, ControlFlow::Break(123));
assert!(matches!(h.dispatch(dptree::deps![State::Other]).await, ControlFlow::Continue(_)));
}
#[tokio::test]
async fn handler_fn_variant() {
let input = State::C(42, "abc");
let h: dptree::Handler<_, _> =
handler![State::C(x, y)].endpoint(|(x, str): (i32, &'static str)| async move {
assert_eq!(x, 42);
assert_eq!(str, "abc");
123
});
assert_eq!(h.dispatch(dptree::deps![input]).await, ControlFlow::Break(123));
assert!(matches!(h.dispatch(dptree::deps![State::Other]).await, ControlFlow::Continue(_)));
}
#[tokio::test]
async fn handler_single_struct_variant() {
let input = State::D { foo: 42 };
let h: dptree::Handler<_, _> = handler![State::D { foo }].endpoint(|x: i32| async move {
assert_eq!(x, 42);
123
});
assert_eq!(h.dispatch(dptree::deps![input]).await, ControlFlow::Break(123));
assert!(matches!(h.dispatch(dptree::deps![State::Other]).await, ControlFlow::Continue(_)));
}
#[tokio::test]
async fn handler_single_struct_variant_trailing_comma() {
let input = State::D { foo: 42 };
#[rustfmt::skip] // rustfmt removes the trailing comma from `State::D { foo, }`, but it plays a vital role in this test.
let h: dptree::Handler<_, _> = handler![State::D { foo, }].endpoint(|(x,): (i32,)| async move {
assert_eq!(x, 42);
123
});
assert_eq!(h.dispatch(dptree::deps![input]).await, ControlFlow::Break(123));
assert!(matches!(h.dispatch(dptree::deps![State::Other]).await, ControlFlow::Continue(_)));
}
#[tokio::test]
async fn handler_struct_variant() {
let input = State::E { foo: 42, bar: "abc" };
let h: dptree::Handler<_, _> =
handler![State::E { foo, bar }].endpoint(|(x, str): (i32, &'static str)| async move {
assert_eq!(x, 42);
assert_eq!(str, "abc");
123
});
assert_eq!(h.dispatch(dptree::deps![input]).await, ControlFlow::Break(123));
assert!(matches!(h.dispatch(dptree::deps![State::Other]).await, ControlFlow::Continue(_)));
}
}

View file

@ -80,7 +80,7 @@ pub use teloxide_core::*;
pub use teloxide_macros as macros; pub use teloxide_macros as macros;
pub use dispatching::filter_command; pub use dispatching::filter_command;
pub use dptree; pub use dptree::{self, case as handler};
#[cfg(all(feature = "nightly", doctest))] #[cfg(all(feature = "nightly", doctest))]
#[cfg_attr(feature = "nightly", cfg_attr(feature = "nightly", doc = include_str!("../README.md")))] #[cfg_attr(feature = "nightly", cfg_attr(feature = "nightly", doc = include_str!("../README.md")))]