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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::ops::Deref;
use matrix_sdk_base::deserialized_responses::EncryptionInfo;
use serde_json::value::RawValue as RawJsonValue;
use super::{EventHandlerData, EventHandlerHandle};
use crate::{room, Client};
pub trait EventHandlerContext: Sized {
#[doc(hidden)]
fn from_data(_: &EventHandlerData<'_>) -> Option<Self>;
}
impl EventHandlerContext for Client {
fn from_data(data: &EventHandlerData<'_>) -> Option<Self> {
Some(data.client.clone())
}
}
impl EventHandlerContext for EventHandlerHandle {
fn from_data(data: &EventHandlerData<'_>) -> Option<Self> {
Some(data.handle.clone())
}
}
impl EventHandlerContext for room::Room {
fn from_data(data: &EventHandlerData<'_>) -> Option<Self> {
data.room.clone()
}
}
#[derive(Clone, Debug)]
pub struct RawEvent(Box<RawJsonValue>);
impl Deref for RawEvent {
type Target = RawJsonValue;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl EventHandlerContext for RawEvent {
fn from_data(data: &EventHandlerData<'_>) -> Option<Self> {
Some(Self(data.raw.to_owned()))
}
}
impl EventHandlerContext for Option<EncryptionInfo> {
fn from_data(data: &EventHandlerData<'_>) -> Option<Self> {
Some(data.encryption_info.cloned())
}
}
#[derive(Debug)]
pub struct Ctx<T>(pub T);
impl<T: Clone + Send + Sync + 'static> EventHandlerContext for Ctx<T> {
fn from_data(data: &EventHandlerData<'_>) -> Option<Self> {
let map = data.client.inner.event_handlers.context.read().unwrap();
map.get::<T>().cloned().map(Ctx)
}
}
impl<T> Deref for Ctx<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}