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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use crate::gateway::{User, PartialUser, PartialGuild};
use crate::Snowflake;
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub enum Channel {
    Category(Category),
    Text(TextChannel),
    Voice(VoiceChannel),
    News(NewsChannel),
    Store(StoreChannel),
    Direct(DirectChannel),
    Group(GroupChannel),
}

#[derive(Debug, Clone)]
pub enum GuildChannel {
    Category(Category),
    Text(TextChannel),
    Voice(VoiceChannel),
    News(NewsChannel),
    Store(StoreChannel),
}

#[derive(Debug, Clone)]
pub enum PrivateChannel {
    Direct(DirectChannel),
    Group(GroupChannel),
}

#[object(server)]
pub struct Category {
    pub id: Snowflake,
    pub guild_id: Option<Snowflake>,
    pub position: i32,
    pub permission_overwrites: Vec<Overwrite>,
    pub name: String,
}

#[object(server)]
pub struct TextChannel {
    pub id: Snowflake,
    pub guild_id: Option<Snowflake>,
    #[nullable]
    pub parent_id: Option<Snowflake>,
    pub position: i32,
    pub permission_overwrites: Vec<Overwrite>,
    pub name: String,
    #[nullable]
    pub topic: Option<String>,
    pub nsfw: Option<bool>,
    #[nullable]
    pub last_message_id: Option<Snowflake>,
    pub rate_limit_per_user: i32,
    pub last_pin_timestamp: Option<String>,
}

#[object(server)]
pub struct VoiceChannel {
    pub id: Snowflake,
    pub guild_id: Option<Snowflake>,
    #[nullable]
    pub parent_id: Option<Snowflake>,
    pub position: i32,
    pub permission_overwrites: Option<Vec<Overwrite>>,
    pub name: String,
    pub bitrate: i32,
    pub user_limit: i32,
}

#[object(server)]
pub struct NewsChannel {
    pub id: Snowflake,
    pub guild_id: Option<Snowflake>,
    #[nullable]
    pub parent_id: Option<Snowflake>,
    pub position: i32,
    pub permission_overwrites: Vec<Overwrite>,
    pub name: String,
    #[nullable]
    pub topic: Option<String>,
    #[nullable]
    pub last_message_id: Option<Snowflake>,
    pub last_pin_timestamp: Option<String>,
}

#[object(server)]
pub struct StoreChannel {
    pub id: Snowflake,
    pub guild_id: Option<Snowflake>,
    #[nullable]
    pub parent_id: Option<Snowflake>,
    pub position: i32,
    pub permission_overwrites: Vec<Overwrite>,
    pub name: String,
}

#[object(server)]
pub struct DirectChannel {
    pub id: Snowflake,
    pub name: Option<String>,
    #[option_nullable]
    pub last_message_id: Option<Option<Snowflake>>,
    pub last_pin_timestamp: Option<String>,
}

#[object(server)]
pub struct GroupChannel {
    pub id: Snowflake,
    pub name: String,
    #[option_nullable]
    pub icon: Option<Option<String>>,
    #[serde(deserialize_with = "automate::encode::json::as_hashmap")]
    pub recipients: HashMap<Snowflake, User>,
    pub owner_id: Snowflake,
    pub application_id: Option<Snowflake>,
    #[option_nullable]
    pub last_message_id: Option<Option<Snowflake>>,
    pub last_pin_timestamp: Option<String>,
}

#[object(server)]
pub struct Invite {
    pub code: String,
    pub guild: Option<PartialGuild>,
    pub channel: InviteChannel,
    pub inviter: Option<User>,
    pub target_user: Option<PartialUser>,
    pub target_user_type: Option<i32>,
    pub approximate_presence_count: Option<i32>,
    pub approximate_member_count: Option<i32>,

    //following variables are extra information
    pub uses: Option<i32>,
    pub max_uses: Option<i32>,
    pub max_age: Option<i32>,
    pub temporary: Option<bool>,
    pub created_at: Option<String>,
}

#[object(server)]
pub struct InviteChannel {
    pub id: Snowflake,
    pub name: String,
    #[serde(rename = "type")]
    pub _type: ChannelType
}

#[object(server)]
pub struct PartialInvite {
    pub code: Option<String>,
    pub guild: Option<PartialGuild>,
    pub channel: Option<Channel>,
    pub inviter: Option<User>,
    pub target_user: Option<PartialUser>,
    pub target_user_type: Option<i32>,
    pub approximate_presence_count: Option<i32>,
    pub approximate_member_count: Option<i32>,

    //following variables are extra information
    pub uses: Option<i32>,
    pub max_uses: Option<i32>,
    pub max_age: Option<i32>,
    pub temporary: Option<bool>,
    pub created_at: Option<String>,
}

#[convert(u8)]
pub enum ChannelType {
    GuildText = 0,
    DM = 1,
    GuildVoice = 2,
    GroupDM = 3,
    GuildCategory = 4,
    GuildNews = 5,
    GuildStore = 6,
}

#[object(both)]
pub struct Overwrite {
    pub id: Snowflake,
    #[serde(rename = "type")]
    pub _type: OverwriteType,
    pub allow: u32,
    pub deny: u32,
}

#[stringify(snake_case)]
pub enum OverwriteType {
    Role,
    Member,
}

#[object(both)]
pub struct ChannelMention {
    pub id: Snowflake,
    pub guild_id: Snowflake,
    #[serde(rename = "type")]
    pub _type: ChannelType,
    pub name: String,
}

#[object(server)]
pub struct Webhook {
    pub id: Snowflake,
    #[serde(rename = "type")]
    pub _type: WebhookType,
    pub guild_id: Option<Snowflake>,
    pub channel_id: Snowflake,
    pub user: Option<User>,
    #[option_nullable]
    pub name: Option<Option<String>>,
    #[option_nullable]
    pub avatar: Option<Option<String>>,
    pub token: Option<String>,
}

#[convert(u8)]
pub enum WebhookType {
    Incoming = 1,
    ChannelFollower = 2,
}

mod channels {
    use crate::{Snowflake, Identifiable, Error};
    use super::{Channel, GuildChannel, PrivateChannel};

    impl Identifiable for Channel {
        fn id(&self) -> Snowflake {
            match self {
                Channel::Category(c) => c.id,
                Channel::Text(c) => c.id,
                Channel::Voice(c) => c.id,
                Channel::News(c) => c.id,
                Channel::Store(c) => c.id,
                Channel::Direct(c) => c.id,
                Channel::Group(c) => c.id,
            }
        }
    }

    impl Identifiable for GuildChannel {
        fn id(&self) -> Snowflake {
            match self {
                GuildChannel::Category(c) => c.id,
                GuildChannel::Text(c) => c.id,
                GuildChannel::Voice(c) => c.id,
                GuildChannel::News(c) => c.id,
                GuildChannel::Store(c) => c.id,
            }
        }
    }

    impl Identifiable for PrivateChannel {
        fn id(&self) -> Snowflake {
            match self {
                PrivateChannel::Direct(c) => c.id,
                PrivateChannel::Group(c) => c.id,
            }
        }
    }

    impl Channel {
        pub fn from_guild(channel: &GuildChannel) -> Channel {
            match channel {
                GuildChannel::Category(c) => Channel::Category(Clone::clone(c)),
                GuildChannel::Text(c) => Channel::Text(Clone::clone(c)),
                GuildChannel::Voice(c) => Channel::Voice(Clone::clone(c)),
                GuildChannel::News(c) => Channel::News(Clone::clone(c)),
                GuildChannel::Store(c) => Channel::Store(Clone::clone(c)),
            }
        }

        pub fn from_private(channel: &PrivateChannel) -> Channel {
            match channel {
                PrivateChannel::Direct(c) => Channel::Direct(Clone::clone(c)),
                PrivateChannel::Group(c) => Channel::Group(Clone::clone(c)),
            }
        }
    }

    impl GuildChannel {
        pub fn from_channel(channel: &Channel) -> Result<Self, Error> {
            match channel {
                Channel::Category(c) => Ok(GuildChannel::Category(Clone::clone(c))),
                Channel::Text(c) => Ok(GuildChannel::Text(Clone::clone(c))),
                Channel::Voice(c) => Ok(GuildChannel::Voice(Clone::clone(c))),
                Channel::News(c) => Ok(GuildChannel::News(Clone::clone(c))),
                Channel::Store(c) => Ok(GuildChannel::Store(Clone::clone(c))),
                _ => Error::err("Can't convert private channel to guild channel")
            }
        }
    }

    impl PrivateChannel {
        pub fn from_channel(channel: &Channel) -> Result<Self, Error> {
            match channel {
                Channel::Direct(c) => Ok(PrivateChannel::Direct(Clone::clone(c))),
                Channel::Group(c) => Ok(PrivateChannel::Group(Clone::clone(c))),
                _ => Error::err("Can't convert guild channel to private channel")
            }
        }
    }
}

mod deserialize {
    use serde::{Deserialize, Deserializer};
    use serde::private::de::{ContentDeserializer, TaggedContentVisitor};
    use serde::de::{Visitor, Error, Unexpected};
    use std::fmt::{self, Formatter};
    use super::{Category, TextChannel, VoiceChannel, NewsChannel, StoreChannel, DirectChannel, GroupChannel};
    use super::{Channel, GuildChannel, PrivateChannel};

    enum ChannelTag {
        Category,
        Text,
        Voice,
        News,
        Store,
        Direct,
        Group,
    }

    struct ChannelTypeVisitor;

    impl<'de> Visitor<'de> for ChannelTypeVisitor {
        type Value = ChannelTag;

        fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
            Formatter::write_str(formatter, "variant number")
        }

        fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> where E: Error {
            match value {
                0 => Ok(ChannelTag::Text),
                1 => Ok(ChannelTag::Direct),
                2 => Ok(ChannelTag::Voice),
                3 => Ok(ChannelTag::Group),
                4 => Ok(ChannelTag::Category),
                5 => Ok(ChannelTag::News),
                6 => Ok(ChannelTag::Store),
                _ => Err(Error::invalid_value(Unexpected::Unsigned(value), &"variant index 0 <= i < 7")),
            }
        }
    }

    impl<'de> Deserialize<'de> for ChannelTag {
        #[inline]
        fn deserialize<D>(d: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
            Deserializer::deserialize_u64(d, ChannelTypeVisitor)
        }
    }

    impl<'de> Deserialize<'de> for Channel {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
            let tagged = Deserializer::deserialize_any(
                deserializer,
                TaggedContentVisitor::<ChannelTag>::new("type"),
            )?;

            match tagged.tag {
                ChannelTag::Category => Result::map(
                    Category::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    Channel::Category,
                ),
                ChannelTag::Text => Result::map(
                    TextChannel::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    Channel::Text,
                ),
                ChannelTag::Voice => Result::map(
                    VoiceChannel::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    Channel::Voice,
                ),
                ChannelTag::News => Result::map(
                    NewsChannel::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    Channel::News,
                ),
                ChannelTag::Store => Result::map(
                    StoreChannel::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    Channel::Store,
                ),
                ChannelTag::Direct => Result::map(
                    DirectChannel::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    Channel::Direct,
                ),
                ChannelTag::Group => Result::map(
                    GroupChannel::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    Channel::Group,
                ),
            }
        }
    }

    impl<'de> Deserialize<'de> for GuildChannel {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
            let tagged = Deserializer::deserialize_any(
                deserializer,
                TaggedContentVisitor::<ChannelTag>::new("type"),
            )?;

            match tagged.tag {
                ChannelTag::Category => Result::map(
                    Category::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    GuildChannel::Category,
                ),
                ChannelTag::Text => Result::map(
                    TextChannel::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    GuildChannel::Text,
                ),
                ChannelTag::Voice => Result::map(
                    VoiceChannel::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    GuildChannel::Voice,
                ),
                ChannelTag::News => Result::map(
                    NewsChannel::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    GuildChannel::News,
                ),
                ChannelTag::Store => Result::map(
                    StoreChannel::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    GuildChannel::Store,
                ),
                _ => Err(Error::custom("disallowed type for guild channel"))
            }
        }
    }

    impl<'de> Deserialize<'de> for PrivateChannel {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
            let tagged = Deserializer::deserialize_any(
                deserializer,
                TaggedContentVisitor::<ChannelTag>::new("type"),
            )?;

            match tagged.tag {
                ChannelTag::Direct => Result::map(
                    DirectChannel::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    PrivateChannel::Direct,
                ),
                ChannelTag::Group => Result::map(
                    GroupChannel::deserialize(ContentDeserializer::<D::Error>::new(tagged.content)),
                    PrivateChannel::Group,
                ),
                _ => Err(Error::custom("disallowed type for private channel"))
            }
        }
    }
}