Add the state! macro

This commit is contained in:
Temirkhan Myrzamadi 2020-02-11 04:04:57 +06:00
parent afb6677a1e
commit 0ae2d975df
4 changed files with 22 additions and 5 deletions

View file

@ -87,7 +87,7 @@ async fn start(mut ctx: Ctx) -> Res {
ctx.answer("Let's start! First, what's your full name?")
.send()
.await?;
ctx.dialogue.state = State::FullName;
state!(ctx, State::FullName);
next(ctx.dialogue)
}
@ -96,7 +96,7 @@ async fn full_name(mut ctx: Ctx) -> Res {
.send()
.await?;
ctx.dialogue.data.full_name = Some(ctx.update.text().unwrap().to_owned());
ctx.dialogue.state = State::Age;
state!(ctx, State::Age);
next(ctx.dialogue)
}
@ -105,7 +105,7 @@ async fn age(mut ctx: Ctx) -> Res {
Ok(ok) => {
send_favourite_music_types(&ctx).await?;
ctx.dialogue.data.age = Some(ok);
ctx.dialogue.state = State::FavouriteMusic;
state!(ctx, State::FavouriteMusic);
}
Err(_) => ctx
.answer("Oh, please, enter a number!")

View file

@ -20,6 +20,21 @@ pub struct DialogueHandlerCtx<Upd, State, T> {
pub dialogue: Dialogue<State, T>,
}
/// Sets a new state.
///
/// Use it like this: `state!(ctx, State::RequestAge)`, where `ctx` is
/// [`DialogueHandlerCtx<Upd, State, T>`] and `State::RequestAge` is of type
/// `State`.
///
/// [`DialogueHandlerCtx<Upd, State, T>`]:
/// crate::dispatching::dialogue::DialogueHandlerCtx
#[macro_export]
macro_rules! state {
($ctx:ident, $state:expr) => {
$ctx.dialogue.state = $state;
};
}
impl<Upd, State, T> GetChatId for DialogueHandlerCtx<Upd, State, T>
where
Upd: GetChatId,

View file

@ -9,6 +9,7 @@ pub use crate::{
Dispatcher, DispatcherHandlerCtx,
},
requests::{Request, ResponseResult},
state,
types::Message,
Bot, RequestError,
};

View file

@ -39,8 +39,9 @@
//! assert_eq!(args, vec!["3", "hours"]);
//! ```
//!
//! [`parse_command`]: crate::utils::parse_command
//! [`parse_command_with_prefix`]: crate::utils::parse_command_with_prefix
//! [`parse_command`]: crate::utils::command::parse_command
//! [`parse_command_with_prefix`]:
//! crate::utils::command::parse_command_with_prefix
pub use teloxide_macros::BotCommand;