twilight_model/guild/onboarding/
mode.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::fmt::{Display, Formatter, Result as FmtResult};

use serde::{Deserialize, Serialize};

/// Defines the criteria used to satisfy Onboarding constraints that are required for enabling.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum OnboardingMode {
    /// Counts only Default Channels towards constraints
    OnboardingDefault,
    /// Counts Default Channels and Questions towards constraint.
    OnboardingAdvanced,
    /// Variant is unknown to the library.
    Unknown(u8),
}

impl From<u8> for OnboardingMode {
    fn from(value: u8) -> Self {
        match value {
            0 => OnboardingMode::OnboardingDefault,
            1 => OnboardingMode::OnboardingAdvanced,
            unknown => OnboardingMode::Unknown(unknown),
        }
    }
}

impl From<OnboardingMode> for u8 {
    fn from(value: OnboardingMode) -> Self {
        match value {
            OnboardingMode::OnboardingDefault => 0,
            OnboardingMode::OnboardingAdvanced => 1,
            OnboardingMode::Unknown(unknown) => unknown,
        }
    }
}

impl OnboardingMode {
    /// Name of the onboarding mode.
    pub const fn name(self) -> &'static str {
        match self {
            Self::OnboardingDefault => "OnboardingDefault",
            Self::OnboardingAdvanced => "OnboardingAdvanced",
            Self::Unknown(_) => "Unknown",
        }
    }
}

impl Display for OnboardingMode {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str(self.name())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_test::Token;
    use static_assertions::assert_impl_all;
    use std::{fmt::Debug, hash::Hash};

    assert_impl_all!(
        OnboardingMode: Clone,
        Copy,
        Debug,
        Deserialize<'static>,
        Eq,
        Hash,
        PartialEq,
        Send,
        Serialize,
        Sync
    );

    #[test]
    fn variants() {
        serde_test::assert_tokens(&OnboardingMode::OnboardingDefault, &[Token::U8(0)]);
        serde_test::assert_tokens(&OnboardingMode::OnboardingAdvanced, &[Token::U8(1)]);
        serde_test::assert_tokens(&OnboardingMode::Unknown(99), &[Token::U8(99)]);
    }

    #[test]
    fn names() {
        assert_eq!(
            "OnboardingDefault",
            OnboardingMode::OnboardingDefault.name()
        );
        assert_eq!(
            "OnboardingAdvanced",
            OnboardingMode::OnboardingAdvanced.name()
        );
        assert_eq!("Unknown", OnboardingMode::Unknown(99).name());
    }
}