twilight_cache_inmemory/model/
presence.rs

1use serde::Serialize;
2use twilight_model::{
3    gateway::presence::{Activity, ClientStatus, Presence, Status},
4    id::{
5        marker::{GuildMarker, UserMarker},
6        Id,
7    },
8};
9
10use crate::CacheablePresence;
11
12/// Represents a cached [`Presence`].
13///
14/// [`Presence`]: twilight_model::gateway::presence::Presence
15#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
16pub struct CachedPresence {
17    pub(crate) activities: Vec<Activity>,
18    pub(crate) client_status: ClientStatus,
19    pub(crate) guild_id: Id<GuildMarker>,
20    pub(crate) status: Status,
21    pub(crate) user_id: Id<UserMarker>,
22}
23
24impl CachedPresence {
25    /// Current activities.
26    #[allow(clippy::missing_const_for_fn)]
27    pub fn activities(&self) -> &[Activity] {
28        &self.activities
29    }
30
31    /// Platform-dependent status.
32    pub const fn client_status(&self) -> &ClientStatus {
33        &self.client_status
34    }
35
36    /// ID of the guild.
37    pub const fn guild_id(&self) -> Id<GuildMarker> {
38        self.guild_id
39    }
40
41    /// Status of the user.
42    pub const fn status(&self) -> Status {
43        self.status
44    }
45
46    /// ID of the user.
47    pub const fn user_id(&self) -> Id<UserMarker> {
48        self.user_id
49    }
50}
51
52impl From<Presence> for CachedPresence {
53    fn from(presence: Presence) -> Self {
54        let Presence {
55            activities,
56            client_status,
57            guild_id,
58            status,
59            user,
60        } = presence;
61
62        Self {
63            activities,
64            client_status,
65            guild_id,
66            status,
67            user_id: user.id(),
68        }
69    }
70}
71
72impl PartialEq<Presence> for CachedPresence {
73    fn eq(&self, other: &Presence) -> bool {
74        self.activities == other.activities
75            && self.client_status == other.client_status
76            && self.guild_id == other.guild_id
77            && self.status == other.status
78            && self.user_id == other.user.id()
79    }
80}
81
82impl CacheablePresence for CachedPresence {}
83
84#[cfg(test)]
85mod tests {
86    use super::CachedPresence;
87    use serde::Serialize;
88    use static_assertions::{assert_fields, assert_impl_all};
89    use std::fmt::Debug;
90    use twilight_model::gateway::presence::Presence;
91
92    assert_fields!(
93        CachedPresence: activities,
94        client_status,
95        guild_id,
96        status,
97        user_id
98    );
99    assert_impl_all!(
100        CachedPresence: Clone,
101        Debug,
102        Eq,
103        From<Presence>,
104        PartialEq,
105        PartialEq<Presence>,
106        Send,
107        Serialize,
108        Sync,
109    );
110}