Add append_field

This commit is contained in:
Temirkhan Myrzamadi 2020-07-26 03:10:48 +06:00
parent d901529ce3
commit 094a95e8d8
8 changed files with 37 additions and 17 deletions

View file

@ -28,6 +28,8 @@ redis-storage = ["redis"]
cbor-serializer = ["serde_cbor"] cbor-serializer = ["serde_cbor"]
bincode-serializer = ["bincode"] bincode-serializer = ["bincode"]
frunk- = ["frunk"]
[dependencies] [dependencies]
serde_json = "1.0.55" serde_json = "1.0.55"
serde = { version = "1.0.114", features = ["derive"] } serde = { version = "1.0.114", features = ["derive"] }
@ -51,6 +53,8 @@ serde_with_macros = "1.1.0"
redis = { version = "0.16.0", optional = true } redis = { version = "0.16.0", optional = true }
serde_cbor = { version = "0.11.1", optional = true } serde_cbor = { version = "0.11.1", optional = true }
bincode = { version = "1.3.1", optional = true } bincode = { version = "1.3.1", optional = true }
#frunk = { git = "https://github.com/Hirrolot/frunk", branch = "append-to-hlist", optional = true }
frunk = { path = "../../Desktop/frunk", optional = true }
#teloxide-macros = "0.3.1" #teloxide-macros = "0.3.1"
teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" } teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" }

View file

@ -9,12 +9,17 @@ edition = "2018"
[dependencies] [dependencies]
log = "0.4.8" log = "0.4.8"
tokio = "0.2.9" tokio = "0.2.9"
frunk = "0.3.1"
frunk_core = "0.3.1"
pretty_env_logger = "0.4.0" pretty_env_logger = "0.4.0"
futures = "0.3.5" futures = "0.3.5"
smart-default = "0.6.0" smart-default = "0.6.0"
derive_more = "0.99.9" derive_more = "0.99.9"
teloxide = { path = "../../" } teloxide = { path = "../../", features = ["frunk"] }
teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" } #teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" }
teloxide-macros = { path = "../../../teloxide-macros" }
[profile.release] [profile.release]
lto = true lto = true

View file

@ -18,6 +18,8 @@
extern crate smart_default; extern crate smart_default;
#[macro_use] #[macro_use]
extern crate derive_more; extern crate derive_more;
#[macro_use]
extern crate frunk;
mod states; mod states;
mod transitions; mod transitions;

View file

@ -10,25 +10,18 @@ pub enum Dialogue {
ReceiveGandalfAlternativeName(ReceiveGandalfAlternativeNameState), ReceiveGandalfAlternativeName(ReceiveGandalfAlternativeNameState),
} }
#[derive(Default)] #[derive(Generic, Default)]
pub struct StartState; pub struct StartState;
pub struct ReceiveDaysOfWeekState { #[derive(Generic)]
rest: StartState, pub struct ReceiveDaysOfWeekState;
}
#[derive(Generic)]
pub struct Receive10x5AnswerState { pub struct Receive10x5AnswerState {
rest: ReceiveDaysOfWeekState,
days_of_week: u8, days_of_week: u8,
} }
pub struct ReceiveGandalfAlternativeNameState { pub struct ReceiveGandalfAlternativeNameState {
rest: Receive10x5AnswerState, days_of_week: u8,
_10x5_answer: u8, _10x5_answer: u8,
} }
up!(
StartState -> ReceiveDaysOfWeekState,
ReceiveDaysOfWeekState + [days_of_week: u8] -> Receive10x5AnswerState,
Receive10x5AnswerState + [_10x5_answer: u8] -> ReceiveGandalfAlternativeNameState,
);

View file

@ -12,7 +12,7 @@ pub type Out = TransitionOut<Dialogue>;
async fn start(state: StartState, cx: TransitionIn) -> Out { async fn start(state: StartState, cx: TransitionIn) -> Out {
cx.answer_str("Let's start our test! How many days per week are there?") cx.answer_str("Let's start our test! How many days per week are there?")
.await?; .await?;
next(state.up()) next(ReceiveDaysOfWeekState)
} }
#[teloxide(transition)] #[teloxide(transition)]
@ -23,7 +23,7 @@ async fn receive_days_of_week(
match cx.update.text().map(str::parse) { match cx.update.text().map(str::parse) {
Some(Ok(ans)) if ans == 7 => { Some(Ok(ans)) if ans == 7 => {
cx.answer_str("10*5 = ?").await?; cx.answer_str("10*5 = ?").await?;
next(state.up(ans)) next(append_field(state, ans))
} }
_ => { _ => {
cx.answer_str("Try again.").await?; cx.answer_str("Try again.").await?;
@ -40,7 +40,7 @@ async fn receive_10x5_answer(
match cx.update.text().map(str::parse) { match cx.update.text().map(str::parse) {
Some(Ok(ans)) if ans == 50 => { Some(Ok(ans)) if ans == 50 => {
cx.answer_str("What's an alternative name of Gandalf?").await?; cx.answer_str("What's an alternative name of Gandalf?").await?;
next(state.up(ans)) next(append_field(state, ans))
} }
_ => { _ => {
cx.answer_str("Try again.").await?; cx.answer_str("Try again.").await?;

12
src/append_field.rs Normal file
View file

@ -0,0 +1,12 @@
#[cfg(feature = "frunk")]
use frunk::{from_generic, generic::Generic, hlist::HAppender, into_generic};
#[cfg(feature = "frunk")]
pub fn append_field<T1, T2, T1Repr, F>(src: T1, field: F) -> T2
where
T1: Generic<Repr = T1Repr>,
T2: Generic<Repr = <T1Repr as HAppender<F>>::Output>,
T1Repr: HAppender<F>,
{
from_generic(into_generic(src).append(field))
}

View file

@ -24,6 +24,7 @@ pub use errors::{
mod errors; mod errors;
mod net; mod net;
mod append_field;
mod bot; mod bot;
pub mod dispatching; pub mod dispatching;
pub mod error_handlers; pub mod error_handlers;

View file

@ -14,6 +14,9 @@ pub use crate::{
up, Bot, RequestError, up, Bot, RequestError,
}; };
#[cfg(feature = "frunk")]
pub use crate::append_field::append_field;
pub use tokio::sync::mpsc::UnboundedReceiver; pub use tokio::sync::mpsc::UnboundedReceiver;
pub use futures::StreamExt; pub use futures::StreamExt;