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
105
use std::fmt;
use serde::{de::DeserializeOwned, Serialize};
use serde_json::{from_str as from_json_str, value::RawValue as RawJsonValue};
use crate::serde::{CanBeEmpty, Raw};
use super::{
EphemeralRoomEventType, GlobalAccountDataEventType, MessageLikeEventType,
RoomAccountDataEventType, StateEventType, ToDeviceEventType,
};
pub trait EventContent: Sized + Serialize {
type EventType;
fn event_type(&self) -> Self::EventType;
}
impl<T> Raw<T>
where
T: EventContentFromType,
T::EventType: fmt::Display,
{
pub fn deserialize_with_type(&self, event_type: T::EventType) -> serde_json::Result<T> {
T::from_parts(&event_type.to_string(), self.json())
}
}
pub trait StaticEventContent: EventContent {
const TYPE: &'static str;
}
pub trait GlobalAccountDataEventContent:
EventContent<EventType = GlobalAccountDataEventType>
{
}
pub trait RoomAccountDataEventContent: EventContent<EventType = RoomAccountDataEventType> {}
pub trait EphemeralRoomEventContent: EventContent<EventType = EphemeralRoomEventType> {}
pub trait MessageLikeEventContent: EventContent<EventType = MessageLikeEventType> {}
pub trait RedactedMessageLikeEventContent: EventContent<EventType = MessageLikeEventType> {}
pub trait StateEventContent: EventContent<EventType = StateEventType> {
type StateKey: AsRef<str> + Clone + fmt::Debug + DeserializeOwned + Serialize;
}
pub trait StaticStateEventContent: StateEventContent {
type PossiblyRedacted: PossiblyRedactedStateEventContent;
type Unsigned: Clone + fmt::Debug + Default + CanBeEmpty + DeserializeOwned;
}
pub trait RedactedStateEventContent: EventContent<EventType = StateEventType> {
type StateKey: AsRef<str> + Clone + fmt::Debug + DeserializeOwned + Serialize;
}
pub trait PossiblyRedactedStateEventContent: EventContent<EventType = StateEventType> {
type StateKey: AsRef<str> + Clone + fmt::Debug + DeserializeOwned + Serialize;
}
pub trait ToDeviceEventContent: EventContent<EventType = ToDeviceEventType> {}
pub trait EventContentFromType: EventContent {
#[doc(hidden)]
fn from_parts(event_type: &str, content: &RawJsonValue) -> serde_json::Result<Self>;
}
impl<T> EventContentFromType for T
where
T: EventContent + DeserializeOwned,
{
fn from_parts(_event_type: &str, content: &RawJsonValue) -> serde_json::Result<Self> {
from_json_str(content.get())
}
}