rtdlib/types/
keyboard_button_type.rs

1
2use crate::types::*;
3use crate::errors::*;
4use uuid::Uuid;
5
6
7
8
9use std::fmt::Debug;
10use serde::de::{Deserialize, Deserializer};
11
12
13
14/// TRAIT | Describes a keyboard button type
15pub trait TDKeyboardButtonType: Debug + RObject {}
16
17/// Describes a keyboard button type
18#[derive(Debug, Clone, Serialize)]
19#[serde(untagged)]
20pub enum KeyboardButtonType {
21  #[doc(hidden)] _Default(()),
22  /// A button that sends the user's location when pressed; available only in private chats
23  RequestLocation(KeyboardButtonTypeRequestLocation),
24  /// A button that sends the user's phone number when pressed; available only in private chats
25  RequestPhoneNumber(KeyboardButtonTypeRequestPhoneNumber),
26  /// A button that allows the user to create and send a poll when pressed; available only in private chats
27  RequestPoll(KeyboardButtonTypeRequestPoll),
28  /// A simple button, with text that must be sent when the button is pressed
29  Text(KeyboardButtonTypeText),
30
31}
32
33impl Default for KeyboardButtonType {
34  fn default() -> Self { KeyboardButtonType::_Default(()) }
35}
36
37impl<'de> Deserialize<'de> for KeyboardButtonType {
38  fn deserialize<D>(deserializer: D) -> Result<KeyboardButtonType, D::Error> where D: Deserializer<'de> {
39    use serde::de::Error;
40    rtd_enum_deserialize!(
41      KeyboardButtonType,
42      (keyboardButtonTypeRequestLocation, RequestLocation);
43      (keyboardButtonTypeRequestPhoneNumber, RequestPhoneNumber);
44      (keyboardButtonTypeRequestPoll, RequestPoll);
45      (keyboardButtonTypeText, Text);
46
47    )(deserializer)
48  }
49}
50
51impl RObject for KeyboardButtonType {
52  #[doc(hidden)] fn td_name(&self) -> &'static str {
53    match self {
54      KeyboardButtonType::RequestLocation(t) => t.td_name(),
55      KeyboardButtonType::RequestPhoneNumber(t) => t.td_name(),
56      KeyboardButtonType::RequestPoll(t) => t.td_name(),
57      KeyboardButtonType::Text(t) => t.td_name(),
58
59      _ => "-1",
60    }
61  }
62  #[doc(hidden)] fn extra(&self) -> Option<String> {
63    match self {
64      KeyboardButtonType::RequestLocation(t) => t.extra(),
65      KeyboardButtonType::RequestPhoneNumber(t) => t.extra(),
66      KeyboardButtonType::RequestPoll(t) => t.extra(),
67      KeyboardButtonType::Text(t) => t.extra(),
68
69      _ => None,
70    }
71  }
72  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
73}
74
75impl KeyboardButtonType {
76  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
77  #[doc(hidden)] pub fn _is_default(&self) -> bool { if let KeyboardButtonType::_Default(_) = self { true } else { false } }
78
79  pub fn is_request_location(&self) -> bool { if let KeyboardButtonType::RequestLocation(_) = self { true } else { false } }
80  pub fn is_request_phone_number(&self) -> bool { if let KeyboardButtonType::RequestPhoneNumber(_) = self { true } else { false } }
81  pub fn is_request_poll(&self) -> bool { if let KeyboardButtonType::RequestPoll(_) = self { true } else { false } }
82  pub fn is_text(&self) -> bool { if let KeyboardButtonType::Text(_) = self { true } else { false } }
83
84  pub fn on_request_location<F: FnOnce(&KeyboardButtonTypeRequestLocation)>(&self, fnc: F) -> &Self { if let KeyboardButtonType::RequestLocation(t) = self { fnc(t) }; self }
85  pub fn on_request_phone_number<F: FnOnce(&KeyboardButtonTypeRequestPhoneNumber)>(&self, fnc: F) -> &Self { if let KeyboardButtonType::RequestPhoneNumber(t) = self { fnc(t) }; self }
86  pub fn on_request_poll<F: FnOnce(&KeyboardButtonTypeRequestPoll)>(&self, fnc: F) -> &Self { if let KeyboardButtonType::RequestPoll(t) = self { fnc(t) }; self }
87  pub fn on_text<F: FnOnce(&KeyboardButtonTypeText)>(&self, fnc: F) -> &Self { if let KeyboardButtonType::Text(t) = self { fnc(t) }; self }
88
89  pub fn as_request_location(&self) -> Option<&KeyboardButtonTypeRequestLocation> { if let KeyboardButtonType::RequestLocation(t) = self { return Some(t) } None }
90  pub fn as_request_phone_number(&self) -> Option<&KeyboardButtonTypeRequestPhoneNumber> { if let KeyboardButtonType::RequestPhoneNumber(t) = self { return Some(t) } None }
91  pub fn as_request_poll(&self) -> Option<&KeyboardButtonTypeRequestPoll> { if let KeyboardButtonType::RequestPoll(t) = self { return Some(t) } None }
92  pub fn as_text(&self) -> Option<&KeyboardButtonTypeText> { if let KeyboardButtonType::Text(t) = self { return Some(t) } None }
93
94
95
96  pub fn request_location<T: AsRef<KeyboardButtonTypeRequestLocation>>(t: T) -> Self { KeyboardButtonType::RequestLocation(t.as_ref().clone()) }
97
98  pub fn request_phone_number<T: AsRef<KeyboardButtonTypeRequestPhoneNumber>>(t: T) -> Self { KeyboardButtonType::RequestPhoneNumber(t.as_ref().clone()) }
99
100  pub fn request_poll<T: AsRef<KeyboardButtonTypeRequestPoll>>(t: T) -> Self { KeyboardButtonType::RequestPoll(t.as_ref().clone()) }
101
102  pub fn text<T: AsRef<KeyboardButtonTypeText>>(t: T) -> Self { KeyboardButtonType::Text(t.as_ref().clone()) }
103
104}
105
106impl AsRef<KeyboardButtonType> for KeyboardButtonType {
107  fn as_ref(&self) -> &KeyboardButtonType { self }
108}
109
110
111
112
113
114
115
116/// A button that sends the user's location when pressed; available only in private chats
117#[derive(Debug, Clone, Default, Serialize, Deserialize)]
118pub struct KeyboardButtonTypeRequestLocation {
119  #[doc(hidden)]
120  #[serde(rename(serialize = "@type", deserialize = "@type"))]
121  td_name: String,
122  #[doc(hidden)]
123  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
124  extra: Option<String>,
125  
126}
127
128impl RObject for KeyboardButtonTypeRequestLocation {
129  #[doc(hidden)] fn td_name(&self) -> &'static str { "keyboardButtonTypeRequestLocation" }
130  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
131  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
132}
133
134
135impl TDKeyboardButtonType for KeyboardButtonTypeRequestLocation {}
136
137
138
139impl KeyboardButtonTypeRequestLocation {
140  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
141  pub fn builder() -> RTDKeyboardButtonTypeRequestLocationBuilder {
142    let mut inner = KeyboardButtonTypeRequestLocation::default();
143    inner.td_name = "keyboardButtonTypeRequestLocation".to_string();
144    inner.extra = Some(Uuid::new_v4().to_string());
145    RTDKeyboardButtonTypeRequestLocationBuilder { inner }
146  }
147
148}
149
150#[doc(hidden)]
151pub struct RTDKeyboardButtonTypeRequestLocationBuilder {
152  inner: KeyboardButtonTypeRequestLocation
153}
154
155impl RTDKeyboardButtonTypeRequestLocationBuilder {
156  pub fn build(&self) -> KeyboardButtonTypeRequestLocation { self.inner.clone() }
157
158}
159
160impl AsRef<KeyboardButtonTypeRequestLocation> for KeyboardButtonTypeRequestLocation {
161  fn as_ref(&self) -> &KeyboardButtonTypeRequestLocation { self }
162}
163
164impl AsRef<KeyboardButtonTypeRequestLocation> for RTDKeyboardButtonTypeRequestLocationBuilder {
165  fn as_ref(&self) -> &KeyboardButtonTypeRequestLocation { &self.inner }
166}
167
168
169
170
171
172
173
174/// A button that sends the user's phone number when pressed; available only in private chats
175#[derive(Debug, Clone, Default, Serialize, Deserialize)]
176pub struct KeyboardButtonTypeRequestPhoneNumber {
177  #[doc(hidden)]
178  #[serde(rename(serialize = "@type", deserialize = "@type"))]
179  td_name: String,
180  #[doc(hidden)]
181  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
182  extra: Option<String>,
183  
184}
185
186impl RObject for KeyboardButtonTypeRequestPhoneNumber {
187  #[doc(hidden)] fn td_name(&self) -> &'static str { "keyboardButtonTypeRequestPhoneNumber" }
188  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
189  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
190}
191
192
193impl TDKeyboardButtonType for KeyboardButtonTypeRequestPhoneNumber {}
194
195
196
197impl KeyboardButtonTypeRequestPhoneNumber {
198  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
199  pub fn builder() -> RTDKeyboardButtonTypeRequestPhoneNumberBuilder {
200    let mut inner = KeyboardButtonTypeRequestPhoneNumber::default();
201    inner.td_name = "keyboardButtonTypeRequestPhoneNumber".to_string();
202    inner.extra = Some(Uuid::new_v4().to_string());
203    RTDKeyboardButtonTypeRequestPhoneNumberBuilder { inner }
204  }
205
206}
207
208#[doc(hidden)]
209pub struct RTDKeyboardButtonTypeRequestPhoneNumberBuilder {
210  inner: KeyboardButtonTypeRequestPhoneNumber
211}
212
213impl RTDKeyboardButtonTypeRequestPhoneNumberBuilder {
214  pub fn build(&self) -> KeyboardButtonTypeRequestPhoneNumber { self.inner.clone() }
215
216}
217
218impl AsRef<KeyboardButtonTypeRequestPhoneNumber> for KeyboardButtonTypeRequestPhoneNumber {
219  fn as_ref(&self) -> &KeyboardButtonTypeRequestPhoneNumber { self }
220}
221
222impl AsRef<KeyboardButtonTypeRequestPhoneNumber> for RTDKeyboardButtonTypeRequestPhoneNumberBuilder {
223  fn as_ref(&self) -> &KeyboardButtonTypeRequestPhoneNumber { &self.inner }
224}
225
226
227
228
229
230
231
232/// A button that allows the user to create and send a poll when pressed; available only in private chats
233#[derive(Debug, Clone, Default, Serialize, Deserialize)]
234pub struct KeyboardButtonTypeRequestPoll {
235  #[doc(hidden)]
236  #[serde(rename(serialize = "@type", deserialize = "@type"))]
237  td_name: String,
238  #[doc(hidden)]
239  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
240  extra: Option<String>,
241  /// If true, only regular polls must be allowed to create
242  force_regular: bool,
243  /// If true, only polls in quiz mode must be allowed to create
244  force_quiz: bool,
245  
246}
247
248impl RObject for KeyboardButtonTypeRequestPoll {
249  #[doc(hidden)] fn td_name(&self) -> &'static str { "keyboardButtonTypeRequestPoll" }
250  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
251  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
252}
253
254
255impl TDKeyboardButtonType for KeyboardButtonTypeRequestPoll {}
256
257
258
259impl KeyboardButtonTypeRequestPoll {
260  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
261  pub fn builder() -> RTDKeyboardButtonTypeRequestPollBuilder {
262    let mut inner = KeyboardButtonTypeRequestPoll::default();
263    inner.td_name = "keyboardButtonTypeRequestPoll".to_string();
264    inner.extra = Some(Uuid::new_v4().to_string());
265    RTDKeyboardButtonTypeRequestPollBuilder { inner }
266  }
267
268  pub fn force_regular(&self) -> bool { self.force_regular }
269
270  pub fn force_quiz(&self) -> bool { self.force_quiz }
271
272}
273
274#[doc(hidden)]
275pub struct RTDKeyboardButtonTypeRequestPollBuilder {
276  inner: KeyboardButtonTypeRequestPoll
277}
278
279impl RTDKeyboardButtonTypeRequestPollBuilder {
280  pub fn build(&self) -> KeyboardButtonTypeRequestPoll { self.inner.clone() }
281
282   
283  pub fn force_regular(&mut self, force_regular: bool) -> &mut Self {
284    self.inner.force_regular = force_regular;
285    self
286  }
287
288   
289  pub fn force_quiz(&mut self, force_quiz: bool) -> &mut Self {
290    self.inner.force_quiz = force_quiz;
291    self
292  }
293
294}
295
296impl AsRef<KeyboardButtonTypeRequestPoll> for KeyboardButtonTypeRequestPoll {
297  fn as_ref(&self) -> &KeyboardButtonTypeRequestPoll { self }
298}
299
300impl AsRef<KeyboardButtonTypeRequestPoll> for RTDKeyboardButtonTypeRequestPollBuilder {
301  fn as_ref(&self) -> &KeyboardButtonTypeRequestPoll { &self.inner }
302}
303
304
305
306
307
308
309
310/// A simple button, with text that must be sent when the button is pressed
311#[derive(Debug, Clone, Default, Serialize, Deserialize)]
312pub struct KeyboardButtonTypeText {
313  #[doc(hidden)]
314  #[serde(rename(serialize = "@type", deserialize = "@type"))]
315  td_name: String,
316  #[doc(hidden)]
317  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
318  extra: Option<String>,
319  
320}
321
322impl RObject for KeyboardButtonTypeText {
323  #[doc(hidden)] fn td_name(&self) -> &'static str { "keyboardButtonTypeText" }
324  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
325  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
326}
327
328
329impl TDKeyboardButtonType for KeyboardButtonTypeText {}
330
331
332
333impl KeyboardButtonTypeText {
334  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
335  pub fn builder() -> RTDKeyboardButtonTypeTextBuilder {
336    let mut inner = KeyboardButtonTypeText::default();
337    inner.td_name = "keyboardButtonTypeText".to_string();
338    inner.extra = Some(Uuid::new_v4().to_string());
339    RTDKeyboardButtonTypeTextBuilder { inner }
340  }
341
342}
343
344#[doc(hidden)]
345pub struct RTDKeyboardButtonTypeTextBuilder {
346  inner: KeyboardButtonTypeText
347}
348
349impl RTDKeyboardButtonTypeTextBuilder {
350  pub fn build(&self) -> KeyboardButtonTypeText { self.inner.clone() }
351
352}
353
354impl AsRef<KeyboardButtonTypeText> for KeyboardButtonTypeText {
355  fn as_ref(&self) -> &KeyboardButtonTypeText { self }
356}
357
358impl AsRef<KeyboardButtonTypeText> for RTDKeyboardButtonTypeTextBuilder {
359  fn as_ref(&self) -> &KeyboardButtonTypeText { &self.inner }
360}
361
362
363