Deprecate teloxide::handler! in favour of dptree::case!

This commit is contained in:
Hirrolot 2022-04-27 15:16:34 +06:00
parent 51d00ec351
commit deb992c787
7 changed files with 25 additions and 46 deletions

View file

@ -16,6 +16,10 @@ 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
### Deprecated
- `teloxide::handler!` in favour of `dptree::case!`.
## 0.8.2 - 2022-04-26
### Fixed

View file

@ -62,7 +62,7 @@ teloxide-macros = { version = "0.6.1", optional = true }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
dptree = "0.2.0"
dptree = "0.2.1"
tokio = { version = "1.8", features = ["fs"] }
tokio-util = "0.6"

View file

@ -210,7 +210,6 @@ impl Default for State {
}
}
#[tokio::main]
async fn main() {
pretty_env_logger::init();
log::info!("Starting dialogue_bot...");
@ -221,12 +220,11 @@ async fn main() {
bot,
Update::filter_message()
.enter_dialogue::<Message, InMemStorage<State>, State>()
.branch(teloxide::handler![State::Start].endpoint(start))
.branch(teloxide::handler![State::ReceiveFullName].endpoint(receive_full_name))
.branch(teloxide::handler![State::ReceiveAge { full_name }].endpoint(receive_age))
.branch(dptree::case![State::Start].endpoint(start))
.branch(dptree::case![State::ReceiveFullName].endpoint(receive_full_name))
.branch(dptree::case![State::ReceiveAge { full_name }].endpoint(receive_age))
.branch(
teloxide::handler![State::ReceiveLocation { full_name, age }]
.endpoint(receive_location),
dptree::case![State::ReceiveLocation { full_name, age }].endpoint(receive_location),
),
)
.dependencies(dptree::deps![InMemStorage::<State>::new()])

View file

@ -50,9 +50,9 @@ async fn main() {
let handler = Update::filter_message()
.enter_dialogue::<Message, ErasedStorage<State>, State>()
.branch(teloxide::handler![State::Start].endpoint(start))
.branch(dptree::case![State::Start].endpoint(start))
.branch(
teloxide::handler![State::GotNumber(n)]
dptree::case![State::GotNumber(n)]
.branch(dptree::entry().filter_command::<Command>().endpoint(got_number))
.branch(dptree::endpoint(invalid_command)),
);

View file

@ -43,12 +43,11 @@ async fn main() {
bot,
Update::filter_message()
.enter_dialogue::<Message, InMemStorage<State>, State>()
.branch(teloxide::handler![State::Start].endpoint(start))
.branch(teloxide::handler![State::ReceiveFullName].endpoint(receive_full_name))
.branch(teloxide::handler![State::ReceiveAge { full_name }].endpoint(receive_age))
.branch(dptree::case![State::Start].endpoint(start))
.branch(dptree::case![State::ReceiveFullName].endpoint(receive_full_name))
.branch(dptree::case![State::ReceiveAge { full_name }].endpoint(receive_age))
.branch(
teloxide::handler![State::ReceiveLocation { full_name, age }]
.endpoint(receive_location),
dptree::case![State::ReceiveLocation { full_name, age }].endpoint(receive_location),
),
)
.dependencies(dptree::deps![InMemStorage::<State>::new()])

View file

@ -67,19 +67,19 @@ async fn main() {
fn schema() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
let command_handler = teloxide::filter_command::<Command, _>()
.branch(
teloxide::handler![State::Start]
.branch(teloxide::handler![Command::Help].endpoint(help))
.branch(teloxide::handler![Command::Start].endpoint(start)),
dptree::case![State::Start]
.branch(dptree::case![Command::Help].endpoint(help))
.branch(dptree::case![Command::Start].endpoint(start)),
)
.branch(teloxide::handler![Command::Cancel].endpoint(cancel));
.branch(dptree::case![Command::Cancel].endpoint(cancel));
let message_handler = Update::filter_message()
.branch(command_handler)
.branch(teloxide::handler![State::ReceiveFullName].endpoint(receive_full_name))
.branch(dptree::case![State::ReceiveFullName].endpoint(receive_full_name))
.branch(dptree::endpoint(invalid_state));
let callback_query_handler = Update::filter_callback_query().chain(
teloxide::handler![State::ReceiveProductChoice { full_name }]
dptree::case![State::ReceiveProductChoice { full_name }]
.endpoint(receive_product_selection),
);

View file

@ -252,38 +252,16 @@ where
/// - 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 {
($($variant:ident)::+) => {
$crate::dptree::filter(|state| matches!(state, $($variant)::+))
};
($($variant:ident)::+ ($param:ident)) => {
$crate::dptree::filter_map(|state| match state {
$($variant)::+($param) => Some($param),
_ => None,
})
};
($($variant:ident)::+ ($($param:ident),+ $(,)?)) => {
$crate::dptree::filter_map(|state| match state {
$($variant)::+($($param),+) => Some(($($param),+ ,)),
_ => None,
})
};
($($variant:ident)::+ {$param:ident}) => {
$crate::dptree::filter_map(|state| match state {
$($variant)::+{$param} => Some($param),
_ => None,
})
};
($($variant:ident)::+ {$($param:ident),+ $(,)?}) => {
$crate::dptree::filter_map(|state| match state {
$($variant)::+ { $($param),+ } => Some(($($param),+ ,)),
_ => None,
})
($($x:tt)*) => {
$crate::dptree::case![$($x)*]
};
}
#[cfg(test)]
#[allow(deprecated)]
mod tests {
use std::ops::ControlFlow;