twilight_model/guild/onboarding/
prompt_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
use serde::{Deserialize, Serialize};

/// The type of an onboarding prompt.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum OnboardingPromptType {
    /// A prompt that allows the user to select multiple options.
    MultipleChoice,
    /// A prompt that allows the user to use a dropdown to select an option.
    Dropdown,
    /// An unknown prompt type.
    Unknown(u8),
}

impl OnboardingPromptType {
    pub const fn kind(self) -> &'static str {
        match self {
            Self::MultipleChoice => "MultipleChoice",
            Self::Dropdown => "DropDown",
            Self::Unknown(_) => "Unknown",
        }
    }
}

impl From<u8> for OnboardingPromptType {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::MultipleChoice,
            1 => Self::Dropdown,
            unknown => Self::Unknown(unknown),
        }
    }
}

impl From<OnboardingPromptType> for u8 {
    fn from(value: OnboardingPromptType) -> Self {
        match value {
            OnboardingPromptType::MultipleChoice => 0,
            OnboardingPromptType::Dropdown => 1,
            OnboardingPromptType::Unknown(unknown) => unknown,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::OnboardingPromptType;
    use serde_test::{assert_tokens, Token};

    #[test]
    fn onboarding_prompt_type() {
        const MAP: &[(OnboardingPromptType, u8, &str)] = &[
            (OnboardingPromptType::MultipleChoice, 0, "MultipleChoice"),
            (OnboardingPromptType::Dropdown, 1, "DropDown"),
            (OnboardingPromptType::Unknown(3), 3, "Unknown"),
        ];

        for (prompt_type, number, name) in MAP {
            assert_eq!(prompt_type.kind(), *name);
            assert_eq!(u8::from(*prompt_type), *number);
            assert_eq!(OnboardingPromptType::from(*number), *prompt_type);
            assert_tokens(number, &[Token::U8(*number)])
        }
    }
}