1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6 api::{Method, Payload},
7 types::{ChatId, Integer, Message, ReplyMarkup, ReplyParameters},
8};
9
10#[cfg(test)]
11mod tests;
12
13#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize)]
15pub struct Dice {
16 #[serde(rename = "emoji")]
17 dice_type: DiceType,
18 value: Integer,
19}
20
21impl Dice {
22 pub fn new(dice_type: DiceType, value: Integer) -> Self {
29 Self { dice_type, value }
30 }
31
32 pub fn dice_type(&self) -> DiceType {
34 self.dice_type
35 }
36
37 pub fn value(&self) -> Integer {
39 self.value
40 }
41}
42
43#[derive(Debug, Copy, Clone, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
45#[non_exhaustive]
46pub enum DiceType {
47 #[serde(rename = "🏀")]
49 Basketball,
50 #[serde(rename = "🎲")]
52 Bones,
53 #[serde(rename = "🎳")]
55 Bowling,
56 #[serde(rename = "🎯")]
58 Darts,
59 #[serde(rename = "⚽")]
61 Football,
62 #[serde(rename = "🎰")]
64 SlotMachine,
65}
66
67impl DiceType {
68 fn as_char(self) -> char {
69 use super::DiceType::*;
70 match self {
71 Basketball => '🏀',
72 Bones => '🎲',
73 Bowling => '🎳',
74 Darts => '🎯',
75 Football => '⚽',
76 SlotMachine => '🎰',
77 }
78 }
79}
80
81impl From<DiceType> for char {
82 fn from(value: DiceType) -> Self {
83 value.as_char()
84 }
85}
86
87impl fmt::Display for DiceType {
88 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89 fmt::Display::fmt(&self.as_char(), f)
90 }
91}
92
93#[serde_with::skip_serializing_none]
95#[derive(Clone, Debug, Serialize)]
96pub struct SendDice {
97 chat_id: ChatId,
98 emoji: DiceType,
99 allow_paid_broadcast: Option<bool>,
100 business_connection_id: Option<String>,
101 disable_notification: Option<bool>,
102 message_effect_id: Option<String>,
103 message_thread_id: Option<Integer>,
104 protect_content: Option<bool>,
105 reply_markup: Option<ReplyMarkup>,
106 reply_parameters: Option<ReplyParameters>,
107}
108
109impl SendDice {
110 pub fn new<T>(chat_id: T, dice_type: DiceType) -> Self
117 where
118 T: Into<ChatId>,
119 {
120 Self {
121 chat_id: chat_id.into(),
122 emoji: dice_type,
123 allow_paid_broadcast: None,
124 business_connection_id: None,
125 disable_notification: None,
126 message_effect_id: None,
127 message_thread_id: None,
128 protect_content: None,
129 reply_markup: None,
130 reply_parameters: None,
131 }
132 }
133
134 pub fn with_allow_paid_broadcast(mut self, value: bool) -> Self {
142 self.allow_paid_broadcast = Some(value);
143 self
144 }
145
146 pub fn with_business_connection_id<T>(mut self, value: T) -> Self
152 where
153 T: Into<String>,
154 {
155 self.business_connection_id = Some(value.into());
156 self
157 }
158
159 pub fn with_disable_notification(mut self, value: bool) -> Self {
166 self.disable_notification = Some(value);
167 self
168 }
169
170 pub fn with_message_effect_id<T>(mut self, value: T) -> Self
176 where
177 T: Into<String>,
178 {
179 self.message_effect_id = Some(value.into());
180 self
181 }
182
183 pub fn with_message_thread_id(mut self, value: Integer) -> Self {
190 self.message_thread_id = Some(value);
191 self
192 }
193
194 pub fn with_protect_content(mut self, value: bool) -> Self {
201 self.protect_content = Some(value);
202 self
203 }
204
205 pub fn with_reply_markup<T>(mut self, value: T) -> Self
211 where
212 T: Into<ReplyMarkup>,
213 {
214 self.reply_markup = Some(value.into());
215 self
216 }
217
218 pub fn with_reply_parameters(mut self, value: ReplyParameters) -> Self {
224 self.reply_parameters = Some(value);
225 self
226 }
227}
228
229impl Method for SendDice {
230 type Response = Message;
231
232 fn into_payload(self) -> Payload {
233 Payload::json("sendDice", self)
234 }
235}