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
122
//! Types related to dice.
#![allow(clippy::non_ascii_literal)]

use is_macro::Is;
use serde::{
    de::{self, Deserializer, IgnoredAny, MapAccess, Visitor},
    ser::Serializer,
    Deserialize, Serialize,
};

/// Represents the kind of a thrown dice.
#[derive(Debug, PartialEq, Eq, Clone, Hash, Is)]
#[non_exhaustive]
pub enum Kind {
    /// 🎯
    Darts,
    /// 🎲
    Dice,
    /// 🏀
    Basketball,
    /// ⚽
    Football,
    /// 🎰
    SlotMachine,
    /// 🎳
    Bowling,
    /// Some emoji `tbot` isn't aware of yet.
    ///
    /// Please note that this field exists only to prevent parsing errors caused
    /// by unknown dice kinds, it is **not** meant to be matched on
    /// or constructed unless as a _temporary_ workaround until a new version
    /// of `tbot` with the new dice kind is released. In other words, we reserve
    /// the right to add new kinds to this enum and release them in patch
    /// updates, and we won't consider any breakage caused by this as a bug.
    /// You should also not construct this variant with an emoji covered by the
    /// above variants.
    Unknown(String),
}

/// Represents a [`Dice`].
///
/// [`Dice`]: https://core.telegram.org/bots/api#dice
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
#[non_exhaustive]
pub struct Dice {
    /// The value of the dice in the range [1, 6] for `🎲`, `🎯` and `🎳`;
    /// [1, 5] for `🏀` and `⚽`; [1, 64] for `🎰`.
    pub value: u8,
    /// The kind of the thrown dice.
    pub kind: Kind,
}

const VALUE: &str = "value";
const EMOJI: &str = "emoji";

struct DiceVisitor;

impl<'v> Visitor<'v> for DiceVisitor {
    type Value = Dice;

    fn expecting(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(fmt, "struct Dice")
    }

    fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
    where
        V: MapAccess<'v>,
    {
        let mut value = None;
        let mut emoji: Option<String> = None;

        while let Some(key) = map.next_key()? {
            match key {
                VALUE => value = Some(map.next_value()?),
                EMOJI => emoji = Some(map.next_value()?),
                _ => drop(map.next_value::<IgnoredAny>()),
            }
        }

        let kind = match emoji.as_deref() {
            Some("🎯") => Kind::Darts,
            Some("🎲") => Kind::Dice,
            Some("🏀") => Kind::Basketball,
            Some("⚽") => Kind::Football,
            Some("🎰") => Kind::SlotMachine,
            Some("🎳") => Kind::Bowling,
            Some(unknown) => Kind::Unknown(unknown.to_string()),
            None => return Err(de::Error::missing_field(EMOJI)),
        };

        Ok(Dice {
            kind,
            value: value.ok_or_else(|| de::Error::missing_field(VALUE))?,
        })
    }
}

impl<'de> Deserialize<'de> for Dice {
    fn deserialize<D>(d: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        d.deserialize_struct("Dice", &[VALUE, EMOJI], DiceVisitor)
    }
}

impl Serialize for Kind {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(match self {
            Self::Dice => "🎲",
            Self::Darts => "🎯",
            Self::Basketball => "🏀",
            Self::Football => "⚽",
            Self::SlotMachine => "🎰",
            Self::Bowling => "🎳",
            Self::Unknown(emoji) => emoji,
        })
    }
}