mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Implement ErasedStorage
This commit is contained in:
parent
b078107694
commit
c6f0cd9404
4 changed files with 90 additions and 2 deletions
|
@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
## unreleased
|
||||
|
||||
### Added
|
||||
|
||||
- The `StorageExt` trait with a single function `erase` that returns `ErasedStorage`.
|
||||
- `ErasedStorage`, a storage with an erased error type.
|
||||
|
||||
## 0.7.1 - 2022-03-09
|
||||
|
||||
### Fixed
|
||||
|
|
|
@ -169,4 +169,4 @@ pub use storage::{RedisStorage, RedisStorageError};
|
|||
#[cfg(feature = "sqlite-storage")]
|
||||
pub use storage::{SqliteStorage, SqliteStorageError};
|
||||
|
||||
pub use storage::{serializer, InMemStorage, InMemStorageError, Serializer, Storage, TraceStorage};
|
||||
pub use storage::*;
|
||||
|
|
|
@ -73,3 +73,85 @@ pub trait Storage<D> {
|
|||
chat_id: i64,
|
||||
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>;
|
||||
}
|
||||
|
||||
/// A storage with an erased error type.
|
||||
pub type ErasedStorage<D> = Arc<dyn Storage<D, Error = Box<dyn std::error::Error>>>;
|
||||
|
||||
/// Extension methods for working with [`Storage`].
|
||||
pub trait StorageExt<D> {
|
||||
/// Returns [`ErasedStorage`].
|
||||
fn erase(self: Arc<Self>) -> ErasedStorage<D>;
|
||||
}
|
||||
|
||||
impl<S, D> StorageExt<D> for S
|
||||
where
|
||||
S: Storage<D> + Send + Sync + 'static,
|
||||
S::Error: std::error::Error + 'static,
|
||||
{
|
||||
fn erase(self: Arc<Self>) -> ErasedStorage<D> {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_erased() {
|
||||
let chat_id = 123;
|
||||
|
||||
let erased = InMemStorage::new().erase();
|
||||
Arc::clone(&erased).update_dialogue(chat_id, 1).await.unwrap();
|
||||
assert_eq!(Arc::clone(&erased).get_dialogue(chat_id).await.unwrap().unwrap(), 1);
|
||||
Arc::clone(&erased).remove_dialogue(chat_id).await.unwrap();
|
||||
assert_eq!(Arc::clone(&erased).get_dialogue(chat_id).await.unwrap(), None);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,7 +92,8 @@ pub use crate::dispatching::dialogue::{RedisStorage, RedisStorageError};
|
|||
pub use crate::dispatching::dialogue::{SqliteStorage, SqliteStorageError};
|
||||
|
||||
pub use crate::dispatching::dialogue::{
|
||||
serializer, InMemStorage, InMemStorageError, Serializer, Storage, TraceStorage,
|
||||
serializer, ErasedStorage, InMemStorage, InMemStorageError, Serializer, Storage, StorageExt,
|
||||
TraceStorage,
|
||||
};
|
||||
pub use get_chat_id::GetChatId;
|
||||
|
||||
|
|
Loading…
Reference in a new issue