twilight_model/guild/
afk_timeout.rs

1use serde::{Deserialize, Serialize};
2use std::time::Duration;
3
4/// Duration of a user being AFK before being timed out from a voice channel.
5///
6/// This value is configured [for guilds][`Guild::afk_timeout`].
7///
8/// # Examples
9///
10/// ```
11/// use twilight_model::guild::AfkTimeout;
12///
13/// assert_eq!(300, AfkTimeout::FIVE_MINUTES);
14/// ```
15///
16/// [`Guild::afk_timeout`]: super::Guild::afk_timeout
17#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
18#[non_exhaustive]
19pub struct AfkTimeout(u16);
20
21impl AfkTimeout {
22    /// AFK timeout of one minute.
23    pub const ONE_MINUTE: Self = Self(60);
24
25    /// AFK timeout of five minutes.
26    pub const FIVE_MINUTES: Self = Self(300);
27
28    /// AFK timeout of fifteen minutes.
29    pub const FIFTEEN_MINUTES: Self = Self(900);
30
31    /// AFK timeout of thirty minutes.
32    pub const THIRTY_MINUTES: Self = Self(1800);
33
34    /// AFK timeout of one hour.
35    pub const ONE_HOUR: Self = Self(3600);
36
37    /// Retrieve the duration of the AFK timeout in seconds.
38    ///
39    /// # Examples
40    ///
41    /// ```
42    /// use twilight_model::guild::AfkTimeout;
43    ///
44    /// assert_eq!(60, AfkTimeout::ONE_MINUTE.get());
45    /// ```
46    pub const fn get(self) -> u16 {
47        self.0
48    }
49}
50
51impl From<u16> for AfkTimeout {
52    fn from(value: u16) -> Self {
53        Self(value)
54    }
55}
56
57impl From<AfkTimeout> for Duration {
58    fn from(value: AfkTimeout) -> Self {
59        Self::from_secs(u64::from(value.get()))
60    }
61}
62
63impl PartialEq<u16> for AfkTimeout {
64    fn eq(&self, other: &u16) -> bool {
65        self.get() == *other
66    }
67}
68
69impl PartialEq<AfkTimeout> for u16 {
70    fn eq(&self, other: &AfkTimeout) -> bool {
71        *self == other.get()
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::AfkTimeout;
78    use serde::{Deserialize, Serialize};
79    use serde_test::Token;
80    use static_assertions::assert_impl_all;
81    use std::{fmt::Debug, hash::Hash, time::Duration};
82
83    assert_impl_all!(
84        AfkTimeout: Clone,
85        Copy,
86        Debug,
87        Deserialize<'static>,
88        Eq,
89        Hash,
90        PartialEq,
91        Send,
92        Serialize,
93        Sync
94    );
95
96    const MAP: &[(AfkTimeout, u16)] = &[
97        (AfkTimeout::ONE_MINUTE, 60),
98        (AfkTimeout::FIVE_MINUTES, 300),
99        (AfkTimeout::FIFTEEN_MINUTES, 900),
100        (AfkTimeout::THIRTY_MINUTES, 1800),
101        (AfkTimeout::ONE_HOUR, 3600),
102    ];
103
104    #[test]
105    fn serde() {
106        for (value, seconds) in MAP {
107            serde_test::assert_tokens(
108                value,
109                &[
110                    Token::NewtypeStruct { name: "AfkTimeout" },
111                    Token::U16(*seconds),
112                ],
113            );
114            assert_eq!(*value, AfkTimeout::from(*seconds));
115            assert_eq!(*seconds, value.get());
116        }
117    }
118
119    /// Test two-way equality implementation.
120    #[test]
121    fn eq() {
122        assert_eq!(300, AfkTimeout::FIVE_MINUTES);
123        assert_eq!(AfkTimeout::FIVE_MINUTES, 300);
124    }
125
126    /// Test conversion to [`std::time::Duration`].
127    #[test]
128    fn std_time_duration() {
129        for (kind, _) in MAP {
130            let std_duration = Duration::from(*kind);
131            assert_eq!(u64::from(kind.get()), std_duration.as_secs());
132        }
133    }
134}