diff --git a/CHANGELOG.md b/CHANGELOG.md index 64fa0e81..b42a9c83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Do not return a dialogue from `Storage::{remove_dialogue, update_dialogue}`. + - Require `D: ToOwned` in `dialogues_repl` and `InMemStorage`. ### Fixed diff --git a/examples/dialogue_bot/src/dialogue/mod.rs b/examples/dialogue_bot/src/dialogue/mod.rs index 4bae759a..43ad8db3 100644 --- a/examples/dialogue_bot/src/dialogue/mod.rs +++ b/examples/dialogue_bot/src/dialogue/mod.rs @@ -6,7 +6,7 @@ use crate::dialogue::states::{ use derive_more::From; use teloxide::macros::Transition; -#[derive(Transition, From)] +#[derive(Transition, Clone, From)] pub enum Dialogue { Start(StartState), ReceiveFullName(ReceiveFullNameState), diff --git a/examples/dialogue_bot/src/dialogue/states/receive_age.rs b/examples/dialogue_bot/src/dialogue/states/receive_age.rs index 36c72a23..099b3407 100644 --- a/examples/dialogue_bot/src/dialogue/states/receive_age.rs +++ b/examples/dialogue_bot/src/dialogue/states/receive_age.rs @@ -1,7 +1,7 @@ use crate::dialogue::{states::receive_location::ReceiveLocationState, Dialogue}; use teloxide::prelude::*; -#[derive(Generic)] +#[derive(Clone, Generic)] pub struct ReceiveAgeState { pub full_name: String, } diff --git a/examples/dialogue_bot/src/dialogue/states/receive_full_name.rs b/examples/dialogue_bot/src/dialogue/states/receive_full_name.rs index 21d3fef2..2ea60a1c 100644 --- a/examples/dialogue_bot/src/dialogue/states/receive_full_name.rs +++ b/examples/dialogue_bot/src/dialogue/states/receive_full_name.rs @@ -1,7 +1,7 @@ use crate::dialogue::{states::receive_age::ReceiveAgeState, Dialogue}; use teloxide::prelude::*; -#[derive(Generic)] +#[derive(Clone, Generic)] pub struct ReceiveFullNameState; #[teloxide(subtransition)] diff --git a/examples/dialogue_bot/src/dialogue/states/receive_location.rs b/examples/dialogue_bot/src/dialogue/states/receive_location.rs index aaa1af2d..3c1f6407 100644 --- a/examples/dialogue_bot/src/dialogue/states/receive_location.rs +++ b/examples/dialogue_bot/src/dialogue/states/receive_location.rs @@ -1,7 +1,7 @@ use crate::dialogue::Dialogue; use teloxide::prelude::*; -#[derive(Generic)] +#[derive(Clone, Generic)] pub struct ReceiveLocationState { pub full_name: String, pub age: u8, diff --git a/examples/dialogue_bot/src/dialogue/states/start.rs b/examples/dialogue_bot/src/dialogue/states/start.rs index a4f3c192..f3f12e0c 100644 --- a/examples/dialogue_bot/src/dialogue/states/start.rs +++ b/examples/dialogue_bot/src/dialogue/states/start.rs @@ -1,6 +1,7 @@ use crate::dialogue::{states::ReceiveFullNameState, Dialogue}; use teloxide::prelude::*; +#[derive(Clone)] pub struct StartState; #[teloxide(subtransition)] diff --git a/src/dispatching/dialogue/mod.rs b/src/dispatching/dialogue/mod.rs index 303d161c..ee182dce 100644 --- a/src/dispatching/dialogue/mod.rs +++ b/src/dispatching/dialogue/mod.rs @@ -35,8 +35,11 @@ //! //! use teloxide::{dispatching::dialogue::Transition, prelude::*, teloxide, RequestError}; //! +//! #[derive(Clone)] //! struct _1State; +//! #[derive(Clone)] //! struct _2State; +//! #[derive(Clone)] //! struct _3State; //! //! type Out = TransitionOut; @@ -56,7 +59,7 @@ //! todo!() //! } //! -//! #[derive(Transition)] +//! #[derive(Clone, Transition)] //! enum D { //! _1(_1State), //! _2(_2State), diff --git a/tests/redis.rs b/tests/redis.rs index 2b88ad8e..9777597e 100644 --- a/tests/redis.rs +++ b/tests/redis.rs @@ -1,6 +1,5 @@ use std::{ fmt::{Debug, Display}, - future::Future, sync::Arc, }; use teloxide::dispatching::dialogue::{RedisStorage, Serializer, Storage}; @@ -40,32 +39,35 @@ async fn test_redis_cbor() { type Dialogue = String; +macro_rules! test_dialogues { + ($storage:expr, $_0:expr, $_1:expr, $_2:expr) => { + assert_eq!(Arc::clone(&$storage).get_dialogue(1).await.unwrap(), $_0); + assert_eq!(Arc::clone(&$storage).get_dialogue(11).await.unwrap(), $_1); + assert_eq!(Arc::clone(&$storage).get_dialogue(256).await.unwrap(), $_2); + }; +} + async fn test_redis(storage: Arc>) where S: Send + Sync + Serializer + 'static, >::Error: Debug + Display, { - check_dialogue(None, Arc::clone(&storage).update_dialogue(1, "ABC".to_owned())).await; - check_dialogue(None, Arc::clone(&storage).update_dialogue(11, "DEF".to_owned())).await; - check_dialogue(None, Arc::clone(&storage).update_dialogue(256, "GHI".to_owned())).await; + test_dialogues!(storage, None, None, None); - // 1 - ABC, 11 - DEF, 256 - GHI + Arc::clone(&storage).update_dialogue(1, "ABC".to_owned()).await.unwrap(); + Arc::clone(&storage).update_dialogue(11, "DEF".to_owned()).await.unwrap(); + Arc::clone(&storage).update_dialogue(256, "GHI".to_owned()).await.unwrap(); - check_dialogue("ABC", Arc::clone(&storage).update_dialogue(1, "JKL".to_owned())).await; - check_dialogue("GHI", Arc::clone(&storage).update_dialogue(256, "MNO".to_owned())).await; + test_dialogues!( + storage, + Some("ABC".to_owned()), + Some("DEF".to_owned()), + Some("GHI".to_owned()) + ); - // 1 - GKL, 11 - DEF, 256 - MNO + Arc::clone(&storage).remove_dialogue(1).await.unwrap(); + Arc::clone(&storage).remove_dialogue(11).await.unwrap(); + Arc::clone(&storage).remove_dialogue(256).await.unwrap(); - check_dialogue("JKL", Arc::clone(&storage).remove_dialogue(1)).await; - check_dialogue("DEF", Arc::clone(&storage).remove_dialogue(11)).await; - check_dialogue("MNO", Arc::clone(&storage).remove_dialogue(256)).await; -} - -async fn check_dialogue( - expected: impl Into>, - actual: impl Future, E>>, -) where - E: Debug, -{ - assert_eq!(expected.into().map(ToOwned::to_owned), actual.await.unwrap()) + test_dialogues!(storage, None, None, None); } diff --git a/tests/sqlite.rs b/tests/sqlite.rs index bf08142d..de37de6c 100644 --- a/tests/sqlite.rs +++ b/tests/sqlite.rs @@ -1,6 +1,5 @@ use std::{ fmt::{Debug, Display}, - future::Future, sync::Arc, }; use teloxide::dispatching::dialogue::{Serializer, SqliteStorage, Storage}; @@ -36,32 +35,35 @@ async fn test_sqlite_cbor() { type Dialogue = String; +macro_rules! test_dialogues { + ($storage:expr, $_0:expr, $_1:expr, $_2:expr) => { + assert_eq!(Arc::clone(&$storage).get_dialogue(1).await.unwrap(), $_0); + assert_eq!(Arc::clone(&$storage).get_dialogue(11).await.unwrap(), $_1); + assert_eq!(Arc::clone(&$storage).get_dialogue(256).await.unwrap(), $_2); + }; +} + async fn test_sqlite(storage: Arc>) where S: Send + Sync + Serializer + 'static, >::Error: Debug + Display, { - check_dialogue(None, Arc::clone(&storage).update_dialogue(1, "ABC".to_owned())).await; - check_dialogue(None, Arc::clone(&storage).update_dialogue(11, "DEF".to_owned())).await; - check_dialogue(None, Arc::clone(&storage).update_dialogue(256, "GHI".to_owned())).await; + test_dialogues!(storage, None, None, None); - // 1 - ABC, 11 - DEF, 256 - GHI + Arc::clone(&storage).update_dialogue(1, "ABC".to_owned()).await.unwrap(); + Arc::clone(&storage).update_dialogue(11, "DEF".to_owned()).await.unwrap(); + Arc::clone(&storage).update_dialogue(256, "GHI".to_owned()).await.unwrap(); - check_dialogue("ABC", Arc::clone(&storage).update_dialogue(1, "JKL".to_owned())).await; - check_dialogue("GHI", Arc::clone(&storage).update_dialogue(256, "MNO".to_owned())).await; + test_dialogues!( + storage, + Some("ABC".to_owned()), + Some("DEF".to_owned()), + Some("GHI".to_owned()) + ); - // 1 - GKL, 11 - DEF, 256 - MNO + Arc::clone(&storage).remove_dialogue(1).await.unwrap(); + Arc::clone(&storage).remove_dialogue(11).await.unwrap(); + Arc::clone(&storage).remove_dialogue(256).await.unwrap(); - check_dialogue("JKL", Arc::clone(&storage).remove_dialogue(1)).await; - check_dialogue("DEF", Arc::clone(&storage).remove_dialogue(11)).await; - check_dialogue("MNO", Arc::clone(&storage).remove_dialogue(256)).await; -} - -async fn check_dialogue( - expected: impl Into>, - actual: impl Future, E>>, -) where - E: Debug, -{ - assert_eq!(expected.into().map(ToOwned::to_owned), actual.await.unwrap()) + test_dialogues!(storage, None, None, None); }