mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Merge branch 'dev' into merge-redis-sqlite-examples
This commit is contained in:
commit
873ed87930
5 changed files with 83 additions and 3 deletions
|
@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
## unreleased
|
||||
|
||||
### Added
|
||||
|
||||
- The `Storage::erase` default function 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::*;
|
||||
|
|
|
@ -72,4 +72,79 @@ pub trait Storage<D> {
|
|||
self: Arc<Self>,
|
||||
chat_id: i64,
|
||||
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>;
|
||||
|
||||
/// Erases [`Self::Error`] to [`std::error::Error`].
|
||||
fn erase(self: Arc<Self>) -> ErasedStorage<D>
|
||||
where
|
||||
Self: Sized + Send + Sync + 'static,
|
||||
Self::Error: std::error::Error + '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>>>;
|
||||
|
||||
#[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,7 @@ 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, TraceStorage,
|
||||
};
|
||||
pub use get_chat_id::GetChatId;
|
||||
|
||||
|
|
|
@ -337,7 +337,7 @@ where
|
|||
let command = splited.next()?;
|
||||
let bot = splited.next();
|
||||
match bot {
|
||||
Some(name) if name == bot_name.as_ref() => {}
|
||||
Some(name) if name.eq_ignore_ascii_case(bot_name.as_ref()) => {}
|
||||
None => {}
|
||||
_ => return None,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue