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
use serde::Serialize;
use twilight_model::{
gateway::presence::{Activity, ClientStatus, Presence, Status},
id::{
marker::{GuildMarker, UserMarker},
Id,
},
};
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct CachedPresence {
pub(crate) activities: Vec<Activity>,
pub(crate) client_status: ClientStatus,
pub(crate) guild_id: Id<GuildMarker>,
pub(crate) status: Status,
pub(crate) user_id: Id<UserMarker>,
}
impl CachedPresence {
pub fn activities(&self) -> &[Activity] {
&self.activities
}
pub const fn client_status(&self) -> &ClientStatus {
&self.client_status
}
pub const fn guild_id(&self) -> Id<GuildMarker> {
self.guild_id
}
pub const fn status(&self) -> Status {
self.status
}
pub const fn user_id(&self) -> Id<UserMarker> {
self.user_id
}
#[allow(clippy::missing_const_for_fn)]
pub(crate) fn from_model(presence: Presence) -> Self {
let Presence {
activities,
client_status,
guild_id,
status,
user,
} = presence;
Self {
activities,
client_status,
guild_id,
status,
user_id: user.id(),
}
}
}
impl PartialEq<Presence> for CachedPresence {
fn eq(&self, other: &Presence) -> bool {
self.activities == other.activities
&& self.client_status == other.client_status
&& self.guild_id == other.guild_id
&& self.status == other.status
&& self.user_id == other.user.id()
}
}
impl From<Presence> for CachedPresence {
fn from(presence: Presence) -> Self {
Self::from_model(presence)
}
}
#[cfg(test)]
mod tests {
use super::CachedPresence;
use serde::Serialize;
use static_assertions::{assert_fields, assert_impl_all};
use std::fmt::Debug;
use twilight_model::gateway::presence::Presence;
assert_fields!(
CachedPresence: activities,
client_status,
guild_id,
status,
user_id
);
assert_impl_all!(
CachedPresence: Clone,
Debug,
Eq,
From<Presence>,
PartialEq,
PartialEq<Presence>,
Send,
Serialize,
Sync,
);
}