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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use serde::{Deserialize, Serialize};
pub type ChannelId = Snowflake;
pub type MessageId = Snowflake;
#[derive(Deserialize)]
pub(crate) struct CloseFrame<'frame> {
pub(crate) code: Option<i32>,
pub(crate) message: Option<&'frame str>,
}
#[derive(Deserialize, Debug)]
#[cfg_attr(test, derive(Serialize))]
pub struct ErrorPayload {
code: Option<u32>,
message: Option<String>,
}
#[derive(Deserialize)]
pub(crate) struct ErrorPayloadStack<'stack> {
pub(crate) code: Option<u32>,
pub(crate) message: Option<std::borrow::Cow<'stack, str>>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
#[serde(rename_all = "snake_case")]
pub enum Environment {
Production,
Other(String),
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(test, derive(Serialize))]
pub struct DiscordConfig {
pub cdn_host: String,
pub environment: Environment,
pub api_endpoint: String,
}
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq)]
pub struct Snowflake(pub u64);
impl Snowflake {
pub fn timestamp(self) -> chrono::DateTime<chrono::Utc> {
let millis = self.0.overflowing_shr(22).0 + 1420070400000;
let ts_seconds = millis / 1000;
let ts_nanos = (millis % 1000) as u32 * 1000000;
use chrono::TimeZone;
chrono::Utc.timestamp(ts_seconds as i64, ts_nanos)
}
}
use std::fmt;
impl fmt::Display for Snowflake {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for Snowflake {
type Err = std::num::ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.parse()?))
}
}
impl Serialize for Snowflake {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.0.to_string())
}
}
impl<'de> Deserialize<'de> for Snowflake {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::de;
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = Snowflake;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a u64 integer either as a number or a string")
}
fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
where
E: de::Error,
{
self.visit_i64(value as i64)
}
fn visit_i16<E>(self, value: i16) -> Result<Self::Value, E>
where
E: de::Error,
{
self.visit_i64(value as i64)
}
fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
where
E: de::Error,
{
self.visit_i64(value as i64)
}
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
self.visit_u64(std::convert::TryInto::try_into(value).map_err(de::Error::custom)?)
}
fn visit_u8<E>(self, value: u8) -> Result<Self::Value, E>
where
E: de::Error,
{
self.visit_u64(value as u64)
}
fn visit_u16<E>(self, value: u16) -> Result<Self::Value, E>
where
E: de::Error,
{
self.visit_u64(value as u64)
}
fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E>
where
E: de::Error,
{
self.visit_u64(value as u64)
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Snowflake(value))
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Snowflake(value.parse().map_err(|e| {
de::Error::custom(format!("failed to parse u64: {}", e))
})?))
}
}
deserializer.deserialize_any(Visitor)
}
}