rtdlib/types/
user_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 | Represents the type of a user. The following types are possible: regular users, deleted users and bots
15pub trait TDUserType: Debug + RObject {}
16
17/// Represents the type of a user. The following types are possible: regular users, deleted users and bots
18#[derive(Debug, Clone, Serialize)]
19#[serde(untagged)]
20pub enum UserType {
21  #[doc(hidden)] _Default(()),
22  /// A bot (see https://core.telegram.org/bots)
23  Bot(UserTypeBot),
24  /// A deleted user or deleted bot. No information on the user besides the user identifier is available. It is not possible to perform any active actions on this type of user
25  Deleted(UserTypeDeleted),
26  /// A regular user
27  Regular(UserTypeRegular),
28  /// No information on the user besides the user identifier is available, yet this user has not been deleted. This object is extremely rare and must be handled like a deleted user. It is not possible to perform any actions on users of this type
29  Unknown(UserTypeUnknown),
30
31}
32
33impl Default for UserType {
34  fn default() -> Self { UserType::_Default(()) }
35}
36
37impl<'de> Deserialize<'de> for UserType {
38  fn deserialize<D>(deserializer: D) -> Result<UserType, D::Error> where D: Deserializer<'de> {
39    use serde::de::Error;
40    rtd_enum_deserialize!(
41      UserType,
42      (userTypeBot, Bot);
43      (userTypeDeleted, Deleted);
44      (userTypeRegular, Regular);
45      (userTypeUnknown, Unknown);
46
47    )(deserializer)
48  }
49}
50
51impl RObject for UserType {
52  #[doc(hidden)] fn td_name(&self) -> &'static str {
53    match self {
54      UserType::Bot(t) => t.td_name(),
55      UserType::Deleted(t) => t.td_name(),
56      UserType::Regular(t) => t.td_name(),
57      UserType::Unknown(t) => t.td_name(),
58
59      _ => "-1",
60    }
61  }
62  #[doc(hidden)] fn extra(&self) -> Option<String> {
63    match self {
64      UserType::Bot(t) => t.extra(),
65      UserType::Deleted(t) => t.extra(),
66      UserType::Regular(t) => t.extra(),
67      UserType::Unknown(t) => t.extra(),
68
69      _ => None,
70    }
71  }
72  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
73}
74
75impl UserType {
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 UserType::_Default(_) = self { true } else { false } }
78
79  pub fn is_bot(&self) -> bool { if let UserType::Bot(_) = self { true } else { false } }
80  pub fn is_deleted(&self) -> bool { if let UserType::Deleted(_) = self { true } else { false } }
81  pub fn is_regular(&self) -> bool { if let UserType::Regular(_) = self { true } else { false } }
82  pub fn is_unknown(&self) -> bool { if let UserType::Unknown(_) = self { true } else { false } }
83
84  pub fn on_bot<F: FnOnce(&UserTypeBot)>(&self, fnc: F) -> &Self { if let UserType::Bot(t) = self { fnc(t) }; self }
85  pub fn on_deleted<F: FnOnce(&UserTypeDeleted)>(&self, fnc: F) -> &Self { if let UserType::Deleted(t) = self { fnc(t) }; self }
86  pub fn on_regular<F: FnOnce(&UserTypeRegular)>(&self, fnc: F) -> &Self { if let UserType::Regular(t) = self { fnc(t) }; self }
87  pub fn on_unknown<F: FnOnce(&UserTypeUnknown)>(&self, fnc: F) -> &Self { if let UserType::Unknown(t) = self { fnc(t) }; self }
88
89  pub fn as_bot(&self) -> Option<&UserTypeBot> { if let UserType::Bot(t) = self { return Some(t) } None }
90  pub fn as_deleted(&self) -> Option<&UserTypeDeleted> { if let UserType::Deleted(t) = self { return Some(t) } None }
91  pub fn as_regular(&self) -> Option<&UserTypeRegular> { if let UserType::Regular(t) = self { return Some(t) } None }
92  pub fn as_unknown(&self) -> Option<&UserTypeUnknown> { if let UserType::Unknown(t) = self { return Some(t) } None }
93
94
95
96  pub fn bot<T: AsRef<UserTypeBot>>(t: T) -> Self { UserType::Bot(t.as_ref().clone()) }
97
98  pub fn deleted<T: AsRef<UserTypeDeleted>>(t: T) -> Self { UserType::Deleted(t.as_ref().clone()) }
99
100  pub fn regular<T: AsRef<UserTypeRegular>>(t: T) -> Self { UserType::Regular(t.as_ref().clone()) }
101
102  pub fn unknown<T: AsRef<UserTypeUnknown>>(t: T) -> Self { UserType::Unknown(t.as_ref().clone()) }
103
104}
105
106impl AsRef<UserType> for UserType {
107  fn as_ref(&self) -> &UserType { self }
108}
109
110
111
112
113
114
115
116/// A bot (see https://core.telegram.org/bots)
117#[derive(Debug, Clone, Default, Serialize, Deserialize)]
118pub struct UserTypeBot {
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  /// True, if the bot can be invited to basic group and supergroup chats
126  can_join_groups: bool,
127  /// True, if the bot can read all messages in basic group or supergroup chats and not just those addressed to the bot. In private and channel chats a bot can always read all messages
128  can_read_all_group_messages: bool,
129  /// True, if the bot supports inline queries
130  is_inline: bool,
131  /// Placeholder for inline queries (displayed on the application input field)
132  inline_query_placeholder: String,
133  /// True, if the location of the user is expected to be sent with every inline query to this bot
134  need_location: bool,
135  
136}
137
138impl RObject for UserTypeBot {
139  #[doc(hidden)] fn td_name(&self) -> &'static str { "userTypeBot" }
140  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
141  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
142}
143
144
145impl TDUserType for UserTypeBot {}
146
147
148
149impl UserTypeBot {
150  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
151  pub fn builder() -> RTDUserTypeBotBuilder {
152    let mut inner = UserTypeBot::default();
153    inner.td_name = "userTypeBot".to_string();
154    inner.extra = Some(Uuid::new_v4().to_string());
155    RTDUserTypeBotBuilder { inner }
156  }
157
158  pub fn can_join_groups(&self) -> bool { self.can_join_groups }
159
160  pub fn can_read_all_group_messages(&self) -> bool { self.can_read_all_group_messages }
161
162  pub fn is_inline(&self) -> bool { self.is_inline }
163
164  pub fn inline_query_placeholder(&self) -> &String { &self.inline_query_placeholder }
165
166  pub fn need_location(&self) -> bool { self.need_location }
167
168}
169
170#[doc(hidden)]
171pub struct RTDUserTypeBotBuilder {
172  inner: UserTypeBot
173}
174
175impl RTDUserTypeBotBuilder {
176  pub fn build(&self) -> UserTypeBot { self.inner.clone() }
177
178   
179  pub fn can_join_groups(&mut self, can_join_groups: bool) -> &mut Self {
180    self.inner.can_join_groups = can_join_groups;
181    self
182  }
183
184   
185  pub fn can_read_all_group_messages(&mut self, can_read_all_group_messages: bool) -> &mut Self {
186    self.inner.can_read_all_group_messages = can_read_all_group_messages;
187    self
188  }
189
190   
191  pub fn is_inline(&mut self, is_inline: bool) -> &mut Self {
192    self.inner.is_inline = is_inline;
193    self
194  }
195
196   
197  pub fn inline_query_placeholder<T: AsRef<str>>(&mut self, inline_query_placeholder: T) -> &mut Self {
198    self.inner.inline_query_placeholder = inline_query_placeholder.as_ref().to_string();
199    self
200  }
201
202   
203  pub fn need_location(&mut self, need_location: bool) -> &mut Self {
204    self.inner.need_location = need_location;
205    self
206  }
207
208}
209
210impl AsRef<UserTypeBot> for UserTypeBot {
211  fn as_ref(&self) -> &UserTypeBot { self }
212}
213
214impl AsRef<UserTypeBot> for RTDUserTypeBotBuilder {
215  fn as_ref(&self) -> &UserTypeBot { &self.inner }
216}
217
218
219
220
221
222
223
224/// A deleted user or deleted bot. No information on the user besides the user identifier is available. It is not possible to perform any active actions on this type of user
225#[derive(Debug, Clone, Default, Serialize, Deserialize)]
226pub struct UserTypeDeleted {
227  #[doc(hidden)]
228  #[serde(rename(serialize = "@type", deserialize = "@type"))]
229  td_name: String,
230  #[doc(hidden)]
231  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
232  extra: Option<String>,
233  
234}
235
236impl RObject for UserTypeDeleted {
237  #[doc(hidden)] fn td_name(&self) -> &'static str { "userTypeDeleted" }
238  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
239  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
240}
241
242
243impl TDUserType for UserTypeDeleted {}
244
245
246
247impl UserTypeDeleted {
248  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
249  pub fn builder() -> RTDUserTypeDeletedBuilder {
250    let mut inner = UserTypeDeleted::default();
251    inner.td_name = "userTypeDeleted".to_string();
252    inner.extra = Some(Uuid::new_v4().to_string());
253    RTDUserTypeDeletedBuilder { inner }
254  }
255
256}
257
258#[doc(hidden)]
259pub struct RTDUserTypeDeletedBuilder {
260  inner: UserTypeDeleted
261}
262
263impl RTDUserTypeDeletedBuilder {
264  pub fn build(&self) -> UserTypeDeleted { self.inner.clone() }
265
266}
267
268impl AsRef<UserTypeDeleted> for UserTypeDeleted {
269  fn as_ref(&self) -> &UserTypeDeleted { self }
270}
271
272impl AsRef<UserTypeDeleted> for RTDUserTypeDeletedBuilder {
273  fn as_ref(&self) -> &UserTypeDeleted { &self.inner }
274}
275
276
277
278
279
280
281
282/// A regular user
283#[derive(Debug, Clone, Default, Serialize, Deserialize)]
284pub struct UserTypeRegular {
285  #[doc(hidden)]
286  #[serde(rename(serialize = "@type", deserialize = "@type"))]
287  td_name: String,
288  #[doc(hidden)]
289  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
290  extra: Option<String>,
291  
292}
293
294impl RObject for UserTypeRegular {
295  #[doc(hidden)] fn td_name(&self) -> &'static str { "userTypeRegular" }
296  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
297  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
298}
299
300
301impl TDUserType for UserTypeRegular {}
302
303
304
305impl UserTypeRegular {
306  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
307  pub fn builder() -> RTDUserTypeRegularBuilder {
308    let mut inner = UserTypeRegular::default();
309    inner.td_name = "userTypeRegular".to_string();
310    inner.extra = Some(Uuid::new_v4().to_string());
311    RTDUserTypeRegularBuilder { inner }
312  }
313
314}
315
316#[doc(hidden)]
317pub struct RTDUserTypeRegularBuilder {
318  inner: UserTypeRegular
319}
320
321impl RTDUserTypeRegularBuilder {
322  pub fn build(&self) -> UserTypeRegular { self.inner.clone() }
323
324}
325
326impl AsRef<UserTypeRegular> for UserTypeRegular {
327  fn as_ref(&self) -> &UserTypeRegular { self }
328}
329
330impl AsRef<UserTypeRegular> for RTDUserTypeRegularBuilder {
331  fn as_ref(&self) -> &UserTypeRegular { &self.inner }
332}
333
334
335
336
337
338
339
340/// No information on the user besides the user identifier is available, yet this user has not been deleted. This object is extremely rare and must be handled like a deleted user. It is not possible to perform any actions on users of this type
341#[derive(Debug, Clone, Default, Serialize, Deserialize)]
342pub struct UserTypeUnknown {
343  #[doc(hidden)]
344  #[serde(rename(serialize = "@type", deserialize = "@type"))]
345  td_name: String,
346  #[doc(hidden)]
347  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
348  extra: Option<String>,
349  
350}
351
352impl RObject for UserTypeUnknown {
353  #[doc(hidden)] fn td_name(&self) -> &'static str { "userTypeUnknown" }
354  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
355  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
356}
357
358
359impl TDUserType for UserTypeUnknown {}
360
361
362
363impl UserTypeUnknown {
364  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
365  pub fn builder() -> RTDUserTypeUnknownBuilder {
366    let mut inner = UserTypeUnknown::default();
367    inner.td_name = "userTypeUnknown".to_string();
368    inner.extra = Some(Uuid::new_v4().to_string());
369    RTDUserTypeUnknownBuilder { inner }
370  }
371
372}
373
374#[doc(hidden)]
375pub struct RTDUserTypeUnknownBuilder {
376  inner: UserTypeUnknown
377}
378
379impl RTDUserTypeUnknownBuilder {
380  pub fn build(&self) -> UserTypeUnknown { self.inner.clone() }
381
382}
383
384impl AsRef<UserTypeUnknown> for UserTypeUnknown {
385  fn as_ref(&self) -> &UserTypeUnknown { self }
386}
387
388impl AsRef<UserTypeUnknown> for RTDUserTypeUnknownBuilder {
389  fn as_ref(&self) -> &UserTypeUnknown { &self.inner }
390}
391
392
393