Skip to main content

tbot/types/
dice.rs

1//! Types related to dice.
2#![allow(clippy::non_ascii_literal)]
3
4use is_macro::Is;
5use serde::{
6    de::{self, Deserializer, IgnoredAny, MapAccess, Visitor},
7    ser::Serializer,
8    Deserialize, Serialize,
9};
10
11/// Represents the kind of a thrown dice.
12#[derive(Debug, PartialEq, Eq, Clone, Hash, Is)]
13#[non_exhaustive]
14pub enum Kind {
15    /// 🎯
16    Darts,
17    /// 🎲
18    Dice,
19    /// 🏀
20    Basketball,
21    /// Some emoji `tbot` isn't aware of yet.
22    ///
23    /// Please note that this field exists only to prevent parsing errors caused
24    /// by unknown dice kinds, it is **not** meant to be matched on
25    /// or constructed unless as a _temporary_ workaround until a new version
26    /// of `tbot` with the new dice kind is released. In other words, we reserve
27    /// the right to add new kinds to this enum and release them in patch
28    /// updates, and we won't consider any breakage caused by this as a bug.
29    /// You should also not construct this variant with an emoji covered by the
30    /// above variants.
31    Unknown(String),
32}
33
34/// Represents a [`Dice`].
35///
36/// [`Dice`]: https://core.telegram.org/bots/api#dice
37#[derive(Debug, PartialEq, Eq, Clone, Hash)]
38#[non_exhaustive]
39pub struct Dice {
40    /// The value of the dice in the range [1, 6].
41    pub value: u8,
42    /// The kind of the thrown dice.
43    pub kind: Kind,
44}
45
46const VALUE: &str = "value";
47const EMOJI: &str = "emoji";
48
49struct DiceVisitor;
50
51impl<'v> Visitor<'v> for DiceVisitor {
52    type Value = Dice;
53
54    fn expecting(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
55        write!(fmt, "struct Dice")
56    }
57
58    fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
59    where
60        V: MapAccess<'v>,
61    {
62        let mut value = None;
63        let mut emoji: Option<String> = None;
64
65        while let Some(key) = map.next_key()? {
66            match key {
67                VALUE => value = Some(map.next_value()?),
68                EMOJI => emoji = Some(map.next_value()?),
69                _ => {
70                    let _ = map.next_value::<IgnoredAny>();
71                }
72            }
73        }
74
75        let kind = match emoji.as_deref() {
76            Some("🎯") => Kind::Darts,
77            Some("🎲") => Kind::Dice,
78            Some("🏀") => Kind::Basketball,
79            Some(unknown) => Kind::Unknown(unknown.to_string()),
80            None => return Err(de::Error::missing_field(EMOJI)),
81        };
82
83        Ok(Dice {
84            kind,
85            value: value.ok_or_else(|| de::Error::missing_field(VALUE))?,
86        })
87    }
88}
89
90impl<'de> Deserialize<'de> for Dice {
91    fn deserialize<D>(d: D) -> Result<Self, D::Error>
92    where
93        D: Deserializer<'de>,
94    {
95        d.deserialize_struct("Dice", &[VALUE, EMOJI], DiceVisitor)
96    }
97}
98
99impl Serialize for Kind {
100    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
101    where
102        S: Serializer,
103    {
104        serializer.serialize_str(match self {
105            Self::Dice => "🎲",
106            Self::Darts => "🎯",
107            Self::Basketball => "🏀",
108            Self::Unknown(emoji) => emoji,
109        })
110    }
111}