diff --git a/Cargo.toml b/Cargo.toml index 079b5d0c..9613733b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,8 @@ redis-storage = ["redis"] cbor-serializer = ["serde_cbor"] bincode-serializer = ["bincode"] +frunk- = ["frunk"] + [dependencies] serde_json = "1.0.55" serde = { version = "1.0.114", features = ["derive"] } @@ -51,6 +53,8 @@ serde_with_macros = "1.1.0" redis = { version = "0.16.0", optional = true } serde_cbor = { version = "0.11.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 = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" } diff --git a/examples/dialogue_bot/Cargo.toml b/examples/dialogue_bot/Cargo.toml index eac96fa6..9e3eac9f 100644 --- a/examples/dialogue_bot/Cargo.toml +++ b/examples/dialogue_bot/Cargo.toml @@ -9,12 +9,17 @@ edition = "2018" [dependencies] log = "0.4.8" tokio = "0.2.9" + +frunk = "0.3.1" +frunk_core = "0.3.1" + pretty_env_logger = "0.4.0" futures = "0.3.5" smart-default = "0.6.0" derive_more = "0.99.9" -teloxide = { path = "../../" } -teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" } +teloxide = { path = "../../", features = ["frunk"] } +#teloxide-macros = { git = "http://github.com/teloxide/teloxide-macros", branch = "master" } +teloxide-macros = { path = "../../../teloxide-macros" } [profile.release] lto = true \ No newline at end of file diff --git a/examples/dialogue_bot/src/main.rs b/examples/dialogue_bot/src/main.rs index accc2343..e946948c 100644 --- a/examples/dialogue_bot/src/main.rs +++ b/examples/dialogue_bot/src/main.rs @@ -18,6 +18,8 @@ extern crate smart_default; #[macro_use] extern crate derive_more; +#[macro_use] +extern crate frunk; mod states; mod transitions; diff --git a/examples/dialogue_bot/src/states.rs b/examples/dialogue_bot/src/states.rs index c0b44ce7..a2465dcb 100644 --- a/examples/dialogue_bot/src/states.rs +++ b/examples/dialogue_bot/src/states.rs @@ -10,25 +10,18 @@ pub enum Dialogue { ReceiveGandalfAlternativeName(ReceiveGandalfAlternativeNameState), } -#[derive(Default)] +#[derive(Generic, Default)] pub struct StartState; -pub struct ReceiveDaysOfWeekState { - rest: StartState, -} +#[derive(Generic)] +pub struct ReceiveDaysOfWeekState; +#[derive(Generic)] pub struct Receive10x5AnswerState { - rest: ReceiveDaysOfWeekState, days_of_week: u8, } pub struct ReceiveGandalfAlternativeNameState { - rest: Receive10x5AnswerState, + days_of_week: u8, _10x5_answer: u8, } - -up!( - StartState -> ReceiveDaysOfWeekState, - ReceiveDaysOfWeekState + [days_of_week: u8] -> Receive10x5AnswerState, - Receive10x5AnswerState + [_10x5_answer: u8] -> ReceiveGandalfAlternativeNameState, -); diff --git a/examples/dialogue_bot/src/transitions.rs b/examples/dialogue_bot/src/transitions.rs index cb6aa709..933107c1 100644 --- a/examples/dialogue_bot/src/transitions.rs +++ b/examples/dialogue_bot/src/transitions.rs @@ -12,7 +12,7 @@ pub type Out = TransitionOut; async fn start(state: StartState, cx: TransitionIn) -> Out { cx.answer_str("Let's start our test! How many days per week are there?") .await?; - next(state.up()) + next(ReceiveDaysOfWeekState) } #[teloxide(transition)] @@ -23,7 +23,7 @@ async fn receive_days_of_week( match cx.update.text().map(str::parse) { Some(Ok(ans)) if ans == 7 => { cx.answer_str("10*5 = ?").await?; - next(state.up(ans)) + next(append_field(state, ans)) } _ => { cx.answer_str("Try again.").await?; @@ -40,7 +40,7 @@ async fn receive_10x5_answer( match cx.update.text().map(str::parse) { Some(Ok(ans)) if ans == 50 => { 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?; diff --git a/src/append_field.rs b/src/append_field.rs new file mode 100644 index 00000000..96aa71c5 --- /dev/null +++ b/src/append_field.rs @@ -0,0 +1,12 @@ +#[cfg(feature = "frunk")] +use frunk::{from_generic, generic::Generic, hlist::HAppender, into_generic}; + +#[cfg(feature = "frunk")] +pub fn append_field(src: T1, field: F) -> T2 +where + T1: Generic, + T2: Generic>::Output>, + T1Repr: HAppender, +{ + from_generic(into_generic(src).append(field)) +} diff --git a/src/lib.rs b/src/lib.rs index 9f7f7e4f..e23ffc9b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,7 @@ pub use errors::{ mod errors; mod net; +mod append_field; mod bot; pub mod dispatching; pub mod error_handlers; diff --git a/src/prelude.rs b/src/prelude.rs index 8c68fe48..81183e1a 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -14,6 +14,9 @@ pub use crate::{ up, Bot, RequestError, }; +#[cfg(feature = "frunk")] +pub use crate::append_field::append_field; + pub use tokio::sync::mpsc::UnboundedReceiver; pub use futures::StreamExt;