-
Notifications
You must be signed in to change notification settings - Fork 203
/
trace_storage.rs
73 lines (64 loc) 路 1.91 KB
/
trace_storage.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::{
fmt::Debug,
marker::{Send, Sync},
sync::Arc,
};
use futures::future::BoxFuture;
use teloxide_core::types::ChatId;
use crate::dispatching::dialogue::Storage;
/// A dialogue storage wrapper which logs all actions performed on an underlying
/// storage.
///
/// Reports about any dialogue action via [`log::Level::Trace`].
pub struct TraceStorage<S> {
inner: Arc<S>,
}
impl<S> TraceStorage<S> {
#[must_use = "This function is pure, that is does nothing unless its output is used"]
pub fn new(inner: Arc<S>) -> Arc<Self> {
Arc::new(Self { inner })
}
#[must_use = "This function is pure, that is does nothing unless its output is used"]
pub fn into_inner(self) -> Arc<S> {
self.inner
}
}
impl<S, D> Storage<D> for TraceStorage<S>
where
D: Debug,
S: Storage<D> + Send + Sync + 'static,
{
type Error = <S as Storage<D>>::Error;
fn remove_dialogue(
self: Arc<Self>,
chat_id: ChatId,
) -> BoxFuture<'static, Result<(), Self::Error>>
where
D: Send + 'static,
{
log::trace!("Removing dialogue #{}", chat_id);
<S as Storage<D>>::remove_dialogue(self.inner.clone(), chat_id)
}
fn update_dialogue(
self: Arc<Self>,
chat_id: ChatId,
dialogue: D,
) -> BoxFuture<'static, Result<(), Self::Error>>
where
D: Send + 'static,
{
Box::pin(async move {
let to = format!("{:#?}", dialogue);
<S as Storage<D>>::update_dialogue(self.inner.clone(), chat_id, dialogue).await?;
log::trace!("Updated a dialogue #{}: {:#?}", chat_id, to);
Ok(())
})
}
fn get_dialogue(
self: Arc<Self>,
chat_id: ChatId,
) -> BoxFuture<'static, Result<Option<D>, Self::Error>> {
log::trace!("Requested a dialogue #{}", chat_id);
<S as Storage<D>>::get_dialogue(self.inner.clone(), chat_id)
}
}