twilight_model/gateway/payload/incoming/
guild_create.rs

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
use crate::{
    guild::{Guild, UnavailableGuild},
    id::{marker::GuildMarker, Id},
};
use serde::{Deserialize, Serialize};

// Developer note: Do not change order as we want unavailable to fail
// first.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(untagged)]
pub enum GuildCreate {
    Unavailable(UnavailableGuild),
    Available(Guild),
}

impl GuildCreate {
    /// ID of the guild.
    pub const fn id(&self) -> Id<GuildMarker> {
        match self {
            GuildCreate::Available(g) => g.id,
            GuildCreate::Unavailable(g) => g.id,
        }
    }
}

#[cfg(test)]
mod tests {
    use serde_test::Token;

    use crate::{guild::UnavailableGuild, id::Id};

    use super::GuildCreate;

    #[test]
    fn unavailable_guild() {
        let expected = GuildCreate::Unavailable(UnavailableGuild {
            id: Id::new(1234),
            unavailable: true,
        });

        // Note: serde(untagged) makes the enum transparent which is
        //       the reason we don't use the variant here.
        serde_test::assert_tokens(
            &expected,
            &[
                Token::Struct {
                    name: "UnavailableGuild",
                    len: 2,
                },
                Token::Str("id"),
                Token::NewtypeStruct { name: "Id" },
                Token::Str("1234"),
                Token::Str("unavailable"),
                Token::Bool(true),
                Token::StructEnd,
            ],
        );
    }
}