Basic implementation of TraceStorage

This commit is contained in:
ivan770 2020-11-17 21:46:21 +02:00
parent 89ae390dfe
commit e9b7ad9161
No known key found for this signature in database
GPG key ID: D8C4BD5AE4D9CC4D
3 changed files with 60 additions and 1 deletions

View file

@ -170,4 +170,4 @@ pub use storage::{RedisStorage, RedisStorageError};
#[cfg(feature = "sqlite-storage")]
pub use storage::{SqliteStorage, SqliteStorageError};
pub use storage::{serializer, InMemStorage, Serializer, Storage};
pub use storage::{serializer, InMemStorage, Serializer, Storage, TraceStorage};

View file

@ -2,6 +2,8 @@ pub mod serializer;
mod in_mem_storage;
mod trace_storage;
#[cfg(feature = "redis-storage")]
mod redis_storage;
@ -11,6 +13,8 @@ mod sqlite_storage;
use futures::future::BoxFuture;
pub use in_mem_storage::InMemStorage;
pub use trace_storage::TraceStorage;
#[cfg(feature = "redis-storage")]
// FIXME(waffle): use `docsrs` here when issue with combine is resolved <https://github.com/teloxide/teloxide/pull/305#issuecomment-716172103>
#[cfg_attr(all(teloxide_docsrs, feature = "nightly"), doc(cfg(feature = "redis-storage")))]

View file

@ -0,0 +1,55 @@
use std::{fmt::Debug, sync::Arc};
use futures::future::BoxFuture;
use log::trace;
use super::Storage;
/// Storage wrapper for logging purposes
///
/// Reports about any dialogue update or removal action on `trace` level
pub struct TraceStorage<S> {
inner: Arc<S>,
}
impl<S> TraceStorage<S> {
#[must_use]
pub fn new(inner: Arc<S>) -> Arc<Self> {
Arc::new(Self { inner })
}
pub fn into_inner(self) -> Arc<S> {
self.inner
}
}
impl<S, D> Storage<D> for TraceStorage<S>
where
D: Debug,
S: Storage<D>,
{
type Error = <S as Storage<D>>::Error;
fn remove_dialogue(
self: Arc<Self>,
chat_id: i64,
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>
where
D: Send + 'static,
{
trace!("Removing dialogue with {}", chat_id);
<S as Storage<D>>::remove_dialogue(self.inner.clone(), chat_id)
}
fn update_dialogue(
self: Arc<Self>,
chat_id: i64,
dialogue: D,
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>
where
D: Send + 'static,
{
trace!("Updating dialogue with {}: {:#?}", chat_id, dialogue);
<S as Storage<D>>::update_dialogue(self.inner.clone(), chat_id, dialogue)
}
}