twilight_model/guild/
integration_type.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
60
61
62
63
64
65
66
67
68
69
70
71
72
#![allow(deprecated)]
use std::borrow::Cow;

use serde::{Deserialize, Serialize};

/// Special and optional guild features.
///
/// See [Discord Docs/Guild Features].
///
/// [Discord Docs/Guild Features]: https://discord.com/developers/docs/resources/guild#guild-object-guild-features
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "String", into = "Cow<'static, str>")]
pub enum GuildIntegrationType {
    /// Integration is a Discord application.
    Discord,
    /// Integration is a Twitch connection.
    Twitch,
    /// Integration is a YouTube connection.
    YouTube,
    /// Integration is a Guild Subscription.
    GuildSubscription,
    /// Variant value is unknown to the library.
    Unknown(String),
}

impl From<GuildIntegrationType> for Cow<'static, str> {
    fn from(value: GuildIntegrationType) -> Self {
        match value {
            GuildIntegrationType::Discord => "discord".into(),
            GuildIntegrationType::Twitch => "twitch".into(),
            GuildIntegrationType::YouTube => "youtube".into(),
            GuildIntegrationType::GuildSubscription => "guild_subscription".into(),
            GuildIntegrationType::Unknown(unknown) => unknown.into(),
        }
    }
}

impl From<String> for GuildIntegrationType {
    fn from(value: String) -> Self {
        match value.as_str() {
            "discord" => Self::Discord,
            "twitch" => Self::Twitch,
            "youtube" => Self::YouTube,
            "guild_subscription" => Self::GuildSubscription,
            _ => Self::Unknown(value),
        }
    }
}

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

    #[test]
    fn variants() {
        const MAP: &[(GuildIntegrationType, &str)] = &[
            (GuildIntegrationType::Discord, "discord"),
            (GuildIntegrationType::Twitch, "twitch"),
            (GuildIntegrationType::YouTube, "youtube"),
            (
                GuildIntegrationType::GuildSubscription,
                "guild_subscription",
            ),
        ];

        for (integration_type, value) in MAP {
            serde_test::assert_tokens(integration_type, &[Token::Str(value)]);
        }
    }
}