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
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
pub struct AfkTimeout(u16);
impl AfkTimeout {
pub const ONE_MINUTE: Self = Self(60);
pub const FIVE_MINUTES: Self = Self(300);
pub const FIFTEEN_MINUTES: Self = Self(900);
pub const THIRTY_MINUTES: Self = Self(1800);
pub const ONE_HOUR: Self = Self(3600);
pub const fn get(self) -> u16 {
self.0
}
}
impl From<u16> for AfkTimeout {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<AfkTimeout> for Duration {
fn from(value: AfkTimeout) -> Self {
Self::from_secs(u64::from(value.get()))
}
}
impl PartialEq<u16> for AfkTimeout {
fn eq(&self, other: &u16) -> bool {
self.get() == *other
}
}
impl PartialEq<AfkTimeout> for u16 {
fn eq(&self, other: &AfkTimeout) -> bool {
*self == other.get()
}
}
#[cfg(test)]
mod tests {
use super::AfkTimeout;
use serde::{Deserialize, Serialize};
use serde_test::Token;
use static_assertions::assert_impl_all;
use std::{fmt::Debug, hash::Hash, time::Duration};
assert_impl_all!(
AfkTimeout: Clone,
Copy,
Debug,
Deserialize<'static>,
Eq,
Hash,
PartialEq,
Send,
Serialize,
Sync
);
const MAP: &[(AfkTimeout, u16)] = &[
(AfkTimeout::ONE_MINUTE, 60),
(AfkTimeout::FIVE_MINUTES, 300),
(AfkTimeout::FIFTEEN_MINUTES, 900),
(AfkTimeout::THIRTY_MINUTES, 1800),
(AfkTimeout::ONE_HOUR, 3600),
];
#[test]
fn serde() {
for (value, seconds) in MAP {
serde_test::assert_tokens(
value,
&[
Token::NewtypeStruct { name: "AfkTimeout" },
Token::U16(*seconds),
],
);
assert_eq!(*value, AfkTimeout::from(*seconds));
assert_eq!(*seconds, value.get());
}
}
#[test]
fn eq() {
assert_eq!(300, AfkTimeout::FIVE_MINUTES);
assert_eq!(AfkTimeout::FIVE_MINUTES, 300);
}
#[test]
fn std_time_duration() {
for (kind, _) in MAP {
let std_duration = Duration::from(*kind);
assert_eq!(u64::from(kind.get()), std_duration.as_secs());
}
}
}