acter_core/
referencing.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::borrow::Cow;

use matrix_sdk::ruma::{EventId, OwnedEventId, OwnedRoomId, RoomId};
use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};

#[derive(
    Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Display, EnumString, Serialize, Deserialize,
)]
#[strum(serialize_all = "snake_case")]
#[repr(u8)]
pub enum SectionIndex {
    #[strum(serialize = "news", serialize = "boosts")]
    Boosts = 0,
    Calendar,
    Pins,
    Stories,
    Tasks,
}

#[derive(
    Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Display, EnumString, Serialize, Deserialize,
)]
#[strum(serialize_all = "snake_case")]
#[repr(u8)]
pub enum ObjectListIndex {
    Attachments,
    Comments,
    Reactions,
    ReadReceipt,
    Rsvp,
    Tasks,
}

#[derive(
    Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Display, EnumString, Serialize, Deserialize,
)]
#[strum(serialize_all = "snake_case")]
#[repr(u8)]
pub enum SpecialListsIndex {
    MyOpenTasks,
    MyDoneTasks,
    #[cfg(any(test, feature = "testing"))]
    Test1,
    #[cfg(any(test, feature = "testing"))]
    Test2,
    #[cfg(any(test, feature = "testing"))]
    Test3,
}

// We organize our Index by typed keys
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Serialize, Deserialize)]
pub enum IndexKey {
    RoomHistory(OwnedRoomId),
    RoomModels(OwnedRoomId),
    ObjectHistory(OwnedEventId),
    Section(SectionIndex),
    RoomSection(OwnedRoomId, SectionIndex),
    ObjectList(OwnedEventId, ObjectListIndex),
    Special(SpecialListsIndex),
    Redacted,
}

#[derive(
    Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Display, EnumString, Serialize, Deserialize,
)]
#[strum(serialize_all = "snake_case")]
pub enum ModelParam {
    CommentsStats,
    AttachmentsStats,
    ReactionStats,
    RsvpStats,
    #[strum(to_string = "read_receipts")]
    ReadReceiptsStats,
}

#[derive(
    Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Display, EnumString, Serialize, Deserialize,
)]
#[strum(serialize_all = "snake_case")]
pub enum RoomParam {
    LatestMessage,
}

#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Serialize, Deserialize)]
pub enum ExecuteReference {
    Index(IndexKey),
    Model(OwnedEventId),
    Room(OwnedRoomId),
    RoomAccountData(OwnedRoomId, Cow<'static, str>),
    ModelParam(OwnedEventId, ModelParam),
    RoomParam(OwnedRoomId, RoomParam),
    AccountData(Cow<'static, str>),
    ModelType(Cow<'static, str>),
}

impl ExecuteReference {
    pub fn as_storage_key(&self) -> String {
        match self {
            ExecuteReference::Model(owned_event_id) => format!("acter::{owned_event_id}"),
            ExecuteReference::ModelParam(owned_event_id, model_param) => {
                format!("{owned_event_id}::{model_param}")
            }
            ExecuteReference::RoomParam(owned_room_id, room_param) => {
                format!("{owned_room_id}::{room_param}")
            }
            // not actually supported
            ExecuteReference::ModelType(model_type) => model_type.to_string(),
            ExecuteReference::Index(_index_key) => todo!(),
            ExecuteReference::Room(_owned_room_id) => todo!(),
            ExecuteReference::RoomAccountData(_owned_room_id, _cow) => todo!(),
            ExecuteReference::AccountData(_cow) => todo!(),
        }
    }
}

impl From<&'static str> for ExecuteReference {
    fn from(value: &'static str) -> Self {
        ExecuteReference::ModelType(Cow::Borrowed(value))
    }
}

impl From<&EventId> for ExecuteReference {
    fn from(value: &EventId) -> Self {
        ExecuteReference::Model(value.to_owned())
    }
}

impl From<OwnedEventId> for ExecuteReference {
    fn from(value: OwnedEventId) -> Self {
        ExecuteReference::Model(value)
    }
}

impl From<&RoomId> for ExecuteReference {
    fn from(value: &RoomId) -> Self {
        ExecuteReference::Room(value.to_owned())
    }
}

impl From<OwnedRoomId> for ExecuteReference {
    fn from(value: OwnedRoomId) -> Self {
        ExecuteReference::Room(value)
    }
}

impl From<IndexKey> for ExecuteReference {
    fn from(value: IndexKey) -> Self {
        ExecuteReference::Index(value)
    }
}

impl From<SectionIndex> for ExecuteReference {
    fn from(value: SectionIndex) -> Self {
        ExecuteReference::Index(IndexKey::Section(value))
    }
}