Rewrite examples/db_remember.rs to use ErasedStorage

This commit is contained in:
Hirrolot 2022-03-21 20:06:32 +06:00
parent 873ed87930
commit cf8c1b78b7
7 changed files with 84 additions and 84 deletions

View file

@ -8,8 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- The `Storage::erase` default function that returns `ErasedStorage`.
- The `Storage::erase` default function that returns `Arc<ErasedStorage>`.
- `ErasedStorage`, a storage with an erased error type.
- Allow the storage generic `S` be `?Sized` in `Dialogue`.
## 0.7.1 - 2022-03-09

View file

@ -1,12 +1,23 @@
use teloxide::{macros::DialogueState, prelude2::*, types::Me, utils::command::BotCommand};
// Set the `DB_REMEMBER_REDIS` environmental variable if you want to use Redis.
// Otherwise, the default is Sqlite.
// include!("redis_config.in");
include!("sqlite_config.in");
use teloxide::{
dispatching2::dialogue::{
serializer::{Bincode, Json},
ErasedStorage, RedisStorage, SqliteStorage, Storage,
},
macros::DialogueState,
prelude2::*,
types::Me,
utils::command::BotCommand,
};
type MyDialogue = Dialogue<State, MyStorage>;
type MyDialogue = Dialogue<State, ErasedStorage<State>>;
type MyStorage = std::sync::Arc<ErasedStorage<State>>;
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
#[derive(DialogueState, Clone, serde::Serialize, serde::Deserialize)]
#[handler_out(anyhow::Result<()>)]
#[handler_out(HandlerResult)]
pub enum State {
#[handler(handle_start)]
Start,
@ -34,9 +45,14 @@ pub enum Command {
async fn main() {
let bot = Bot::from_env().auto_send();
let storage = open_storage().await;
let storage: MyStorage = if std::env::var("DB_REMEMBER_REDIS").is_ok() {
RedisStorage::open("redis://127.0.0.1:6379", Bincode).await.unwrap().erase()
} else {
SqliteStorage::open("db.sqlite", Json).await.unwrap().erase()
};
let handler = Update::filter_message()
.enter_dialogue::<Message, MyStorage, State>()
.enter_dialogue::<Message, ErasedStorage<State>, State>()
.dispatch_by::<State>();
Dispatcher::builder(bot, handler)
@ -47,11 +63,7 @@ async fn main() {
.await;
}
async fn handle_start(
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
) -> anyhow::Result<()> {
async fn handle_start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
match msg.text().unwrap().parse() {
Ok(number) => {
dialogue.update(State::GotNumber(number)).await?;
@ -75,7 +87,7 @@ async fn handle_got_number(
dialogue: MyDialogue,
num: i32,
me: Me,
) -> anyhow::Result<()> {
) -> HandlerResult {
let ans = msg.text().unwrap();
let bot_name = me.user.username.unwrap();

View file

@ -1,7 +0,0 @@
use teloxide::dispatching2::dialogue::{RedisStorage, serializer::Bincode};
type MyStorage = RedisStorage<Bincode>;
async fn open_storage() -> std::sync::Arc<MyStorage> {
RedisStorage::open("redis://127.0.0.1:6379", Bincode).await.unwrap()
}

View file

@ -1,7 +0,0 @@
use teloxide::dispatching2::dialogue::{SqliteStorage, serializer::Json};
type MyStorage = SqliteStorage<Json>;
async fn open_storage() -> std::sync::Arc<MyStorage> {
SqliteStorage::open("db.sqlite", Json).await.unwrap()
}

View file

@ -74,64 +74,59 @@ pub trait Storage<D> {
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>;
/// Erases [`Self::Error`] to [`std::error::Error`].
fn erase(self: Arc<Self>) -> ErasedStorage<D>
#[must_use]
fn erase(self: Arc<Self>) -> Arc<ErasedStorage<D>>
where
Self: Sized + Send + Sync + 'static,
Self::Error: std::error::Error + 'static,
Self::Error: std::error::Error + Send + Sync + 'static,
{
struct Eraser<S>(Arc<S>);
impl<D, S> Storage<D> for Eraser<S>
where
S: Storage<D> + Send + Sync + 'static,
S::Error: std::error::Error + 'static,
{
type Error = Box<dyn std::error::Error>;
fn remove_dialogue(
self: Arc<Self>,
chat_id: i64,
) -> BoxFuture<'static, Result<(), Self::Error>>
where
D: Send + 'static,
{
Box::pin(async move {
Arc::clone(&self.0).remove_dialogue(chat_id).await.map_err(|e| e.into())
})
}
fn update_dialogue(
self: Arc<Self>,
chat_id: i64,
dialogue: D,
) -> BoxFuture<'static, Result<(), Self::Error>>
where
D: Send + 'static,
{
Box::pin(async move {
Arc::clone(&self.0)
.update_dialogue(chat_id, dialogue)
.await
.map_err(|e| e.into())
})
}
fn get_dialogue(
self: Arc<Self>,
chat_id: i64,
) -> BoxFuture<'static, Result<Option<D>, Self::Error>> {
Box::pin(async move {
Arc::clone(&self.0).get_dialogue(chat_id).await.map_err(|e| e.into())
})
}
}
Arc::new(Eraser(self))
}
}
/// A storage with an erased error type.
pub type ErasedStorage<D> = Arc<dyn Storage<D, Error = Box<dyn std::error::Error>>>;
struct Eraser<S>(Arc<S>);
impl<D, S> Storage<D> for Eraser<S>
where
S: Storage<D> + Send + Sync + 'static,
S::Error: std::error::Error + Send + Sync + 'static,
{
type Error = Box<dyn std::error::Error + Send + Sync>;
fn remove_dialogue(self: Arc<Self>, chat_id: i64) -> BoxFuture<'static, Result<(), Self::Error>>
where
D: Send + 'static,
{
Box::pin(
async move { Arc::clone(&self.0).remove_dialogue(chat_id).await.map_err(|e| e.into()) },
)
}
fn update_dialogue(
self: Arc<Self>,
chat_id: i64,
dialogue: D,
) -> BoxFuture<'static, Result<(), Self::Error>>
where
D: Send + 'static,
{
Box::pin(async move {
Arc::clone(&self.0).update_dialogue(chat_id, dialogue).await.map_err(|e| e.into())
})
}
fn get_dialogue(
self: Arc<Self>,
chat_id: i64,
) -> BoxFuture<'static, Result<Option<D>, Self::Error>> {
Box::pin(
async move { Arc::clone(&self.0).get_dialogue(chat_id).await.map_err(|e| e.into()) },
)
}
}
pub type ErasedStorage<D> =
dyn Storage<D, Error = Box<dyn std::error::Error + Send + Sync>> + Send + Sync;
#[cfg(test)]
mod tests {

View file

@ -102,7 +102,10 @@ mod get_chat_id;
/// A handle for controlling dialogue state.
#[derive(Debug)]
pub struct Dialogue<D, S> {
pub struct Dialogue<D, S>
where
S: ?Sized,
{
storage: Arc<S>,
chat_id: i64,
_phantom: PhantomData<D>,
@ -110,7 +113,10 @@ pub struct Dialogue<D, S> {
// `#[derive]` requires generics to implement `Clone`, but `S` is wrapped around
// `Arc`, and `D` is wrapped around PhantomData.
impl<D, S> Clone for Dialogue<D, S> {
impl<D, S> Clone for Dialogue<D, S>
where
S: ?Sized,
{
fn clone(&self) -> Self {
Dialogue { storage: self.storage.clone(), chat_id: self.chat_id, _phantom: PhantomData }
}
@ -119,7 +125,7 @@ impl<D, S> Clone for Dialogue<D, S> {
impl<D, S> Dialogue<D, S>
where
D: Send + 'static,
S: Storage<D>,
S: Storage<D> + ?Sized,
{
/// Constructs a new dialogue with `storage` (where dialogues are stored)
/// and `chat_id` of a current dialogue.

View file

@ -45,7 +45,7 @@ pub trait HandlerExt<Output> {
#[must_use]
fn enter_dialogue<Upd, S, D>(self) -> Self
where
S: Storage<D> + Send + Sync + 'static,
S: Storage<D> + ?Sized + Send + Sync + 'static,
<S as Storage<D>>::Error: Debug + Send,
D: Default + Send + Sync + 'static,
Upd: GetChatId + Clone + Send + Sync + 'static;
@ -72,7 +72,7 @@ where
fn enter_dialogue<Upd, S, D>(self) -> Self
where
S: Storage<D> + Send + Sync + 'static,
S: Storage<D> + ?Sized + Send + Sync + 'static,
<S as Storage<D>>::Error: Debug + Send,
D: Default + Send + Sync + 'static,
Upd: GetChatId + Clone + Send + Sync + 'static,