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
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fmt::{Display, Formatter, Result as FmtResult};
#[derive(
    Clone, Copy, Debug, Deserialize_repr, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize_repr,
)]
#[repr(u8)]
pub enum InteractionType {
    Ping = 1,
    ApplicationCommand = 2,
    
    
    
    MessageComponent = 3,
    
    ApplicationCommandAutocomplete = 4,
}
impl InteractionType {
    pub const fn kind(self) -> &'static str {
        match self {
            Self::Ping => "Ping",
            Self::ApplicationCommand => "ApplicationCommand",
            Self::MessageComponent => "MessageComponent",
            Self::ApplicationCommandAutocomplete => "ApplicationCommandAutocomplete",
        }
    }
}
#[derive(Debug)]
pub struct UnknownInteractionTypeError {
    value: u8,
}
impl Display for UnknownInteractionTypeError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str("unknown interaction type: ")?;
        Display::fmt(&self.value, f)
    }
}
impl TryFrom<u8> for InteractionType {
    type Error = UnknownInteractionTypeError;
    fn try_from(i: u8) -> Result<Self, Self::Error> {
        match i {
            1 => Ok(Self::Ping),
            2 => Ok(Self::ApplicationCommand),
            3 => Ok(Self::MessageComponent),
            4 => Ok(Self::ApplicationCommandAutocomplete),
            other => Err(UnknownInteractionTypeError { value: other }),
        }
    }
}
#[cfg(test)]
mod tests {
    use super::{InteractionType, UnknownInteractionTypeError};
    use serde::{Deserialize, Serialize};
    use static_assertions::{assert_impl_all, const_assert_eq};
    use std::{fmt::Debug, hash::Hash};
    assert_impl_all!(
        InteractionType: Clone,
        Copy,
        Debug,
        Deserialize<'static>,
        Eq,
        Hash,
        Ord,
        PartialEq,
        PartialOrd,
        Serialize,
        Send,
        Sync
    );
    const_assert_eq!(1, InteractionType::Ping as u8);
    const_assert_eq!(2, InteractionType::ApplicationCommand as u8);
    const_assert_eq!(3, InteractionType::MessageComponent as u8);
    const_assert_eq!(4, InteractionType::ApplicationCommandAutocomplete as u8);
    #[test]
    fn test_kind() {
        assert_eq!("Ping", InteractionType::Ping.kind());
        assert_eq!(
            "ApplicationCommand",
            InteractionType::ApplicationCommand.kind()
        );
        assert_eq!("MessageComponent", InteractionType::MessageComponent.kind());
        assert_eq!(
            "ApplicationCommandAutocomplete",
            InteractionType::ApplicationCommandAutocomplete.kind()
        );
    }
    #[test]
    fn test_try_from() -> Result<(), UnknownInteractionTypeError> {
        assert_eq!(InteractionType::Ping, InteractionType::try_from(1)?);
        assert_eq!(
            InteractionType::ApplicationCommand,
            InteractionType::try_from(2)?
        );
        assert_eq!(
            InteractionType::MessageComponent,
            InteractionType::try_from(3)?
        );
        assert_eq!(
            InteractionType::ApplicationCommandAutocomplete,
            InteractionType::try_from(4)?,
        );
        assert!(InteractionType::try_from(u8::MAX).is_err());
        Ok(())
    }
}