rtdlib/types/
chat_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 the type of a chat
15pub trait TDChatType: Debug + RObject {}
16
17/// Describes the type of a chat
18#[derive(Debug, Clone, Serialize)]
19#[serde(untagged)]
20pub enum ChatType {
21  #[doc(hidden)] _Default(()),
22  /// A basic group (a chat with 0-200 other users)
23  BasicGroup(ChatTypeBasicGroup),
24  /// An ordinary chat with a user
25  Private(ChatTypePrivate),
26  /// A secret chat with a user
27  Secret(ChatTypeSecret),
28  /// A supergroup or channel (with unlimited members)
29  Supergroup(ChatTypeSupergroup),
30
31}
32
33impl Default for ChatType {
34  fn default() -> Self { ChatType::_Default(()) }
35}
36
37impl<'de> Deserialize<'de> for ChatType {
38  fn deserialize<D>(deserializer: D) -> Result<ChatType, D::Error> where D: Deserializer<'de> {
39    use serde::de::Error;
40    rtd_enum_deserialize!(
41      ChatType,
42      (chatTypeBasicGroup, BasicGroup);
43      (chatTypePrivate, Private);
44      (chatTypeSecret, Secret);
45      (chatTypeSupergroup, Supergroup);
46
47    )(deserializer)
48  }
49}
50
51impl RObject for ChatType {
52  #[doc(hidden)] fn td_name(&self) -> &'static str {
53    match self {
54      ChatType::BasicGroup(t) => t.td_name(),
55      ChatType::Private(t) => t.td_name(),
56      ChatType::Secret(t) => t.td_name(),
57      ChatType::Supergroup(t) => t.td_name(),
58
59      _ => "-1",
60    }
61  }
62  #[doc(hidden)] fn extra(&self) -> Option<String> {
63    match self {
64      ChatType::BasicGroup(t) => t.extra(),
65      ChatType::Private(t) => t.extra(),
66      ChatType::Secret(t) => t.extra(),
67      ChatType::Supergroup(t) => t.extra(),
68
69      _ => None,
70    }
71  }
72  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
73}
74
75impl ChatType {
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 ChatType::_Default(_) = self { true } else { false } }
78
79  pub fn is_basic_group(&self) -> bool { if let ChatType::BasicGroup(_) = self { true } else { false } }
80  pub fn is_private(&self) -> bool { if let ChatType::Private(_) = self { true } else { false } }
81  pub fn is_secret(&self) -> bool { if let ChatType::Secret(_) = self { true } else { false } }
82  pub fn is_supergroup(&self) -> bool { if let ChatType::Supergroup(_) = self { true } else { false } }
83
84  pub fn on_basic_group<F: FnOnce(&ChatTypeBasicGroup)>(&self, fnc: F) -> &Self { if let ChatType::BasicGroup(t) = self { fnc(t) }; self }
85  pub fn on_private<F: FnOnce(&ChatTypePrivate)>(&self, fnc: F) -> &Self { if let ChatType::Private(t) = self { fnc(t) }; self }
86  pub fn on_secret<F: FnOnce(&ChatTypeSecret)>(&self, fnc: F) -> &Self { if let ChatType::Secret(t) = self { fnc(t) }; self }
87  pub fn on_supergroup<F: FnOnce(&ChatTypeSupergroup)>(&self, fnc: F) -> &Self { if let ChatType::Supergroup(t) = self { fnc(t) }; self }
88
89  pub fn as_basic_group(&self) -> Option<&ChatTypeBasicGroup> { if let ChatType::BasicGroup(t) = self { return Some(t) } None }
90  pub fn as_private(&self) -> Option<&ChatTypePrivate> { if let ChatType::Private(t) = self { return Some(t) } None }
91  pub fn as_secret(&self) -> Option<&ChatTypeSecret> { if let ChatType::Secret(t) = self { return Some(t) } None }
92  pub fn as_supergroup(&self) -> Option<&ChatTypeSupergroup> { if let ChatType::Supergroup(t) = self { return Some(t) } None }
93
94
95
96  pub fn basic_group<T: AsRef<ChatTypeBasicGroup>>(t: T) -> Self { ChatType::BasicGroup(t.as_ref().clone()) }
97
98  pub fn private<T: AsRef<ChatTypePrivate>>(t: T) -> Self { ChatType::Private(t.as_ref().clone()) }
99
100  pub fn secret<T: AsRef<ChatTypeSecret>>(t: T) -> Self { ChatType::Secret(t.as_ref().clone()) }
101
102  pub fn supergroup<T: AsRef<ChatTypeSupergroup>>(t: T) -> Self { ChatType::Supergroup(t.as_ref().clone()) }
103
104}
105
106impl AsRef<ChatType> for ChatType {
107  fn as_ref(&self) -> &ChatType { self }
108}
109
110
111
112
113
114
115
116/// A basic group (a chat with 0-200 other users)
117#[derive(Debug, Clone, Default, Serialize, Deserialize)]
118pub struct ChatTypeBasicGroup {
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  /// Basic group identifier
126  basic_group_id: i64,
127  
128}
129
130impl RObject for ChatTypeBasicGroup {
131  #[doc(hidden)] fn td_name(&self) -> &'static str { "chatTypeBasicGroup" }
132  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
133  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
134}
135
136
137impl TDChatType for ChatTypeBasicGroup {}
138
139
140
141impl ChatTypeBasicGroup {
142  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
143  pub fn builder() -> RTDChatTypeBasicGroupBuilder {
144    let mut inner = ChatTypeBasicGroup::default();
145    inner.td_name = "chatTypeBasicGroup".to_string();
146    inner.extra = Some(Uuid::new_v4().to_string());
147    RTDChatTypeBasicGroupBuilder { inner }
148  }
149
150  pub fn basic_group_id(&self) -> i64 { self.basic_group_id }
151
152}
153
154#[doc(hidden)]
155pub struct RTDChatTypeBasicGroupBuilder {
156  inner: ChatTypeBasicGroup
157}
158
159impl RTDChatTypeBasicGroupBuilder {
160  pub fn build(&self) -> ChatTypeBasicGroup { self.inner.clone() }
161
162   
163  pub fn basic_group_id(&mut self, basic_group_id: i64) -> &mut Self {
164    self.inner.basic_group_id = basic_group_id;
165    self
166  }
167
168}
169
170impl AsRef<ChatTypeBasicGroup> for ChatTypeBasicGroup {
171  fn as_ref(&self) -> &ChatTypeBasicGroup { self }
172}
173
174impl AsRef<ChatTypeBasicGroup> for RTDChatTypeBasicGroupBuilder {
175  fn as_ref(&self) -> &ChatTypeBasicGroup { &self.inner }
176}
177
178
179
180
181
182
183
184/// An ordinary chat with a user
185#[derive(Debug, Clone, Default, Serialize, Deserialize)]
186pub struct ChatTypePrivate {
187  #[doc(hidden)]
188  #[serde(rename(serialize = "@type", deserialize = "@type"))]
189  td_name: String,
190  #[doc(hidden)]
191  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
192  extra: Option<String>,
193  /// User identifier
194  user_id: i64,
195  
196}
197
198impl RObject for ChatTypePrivate {
199  #[doc(hidden)] fn td_name(&self) -> &'static str { "chatTypePrivate" }
200  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
201  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
202}
203
204
205impl TDChatType for ChatTypePrivate {}
206
207
208
209impl ChatTypePrivate {
210  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
211  pub fn builder() -> RTDChatTypePrivateBuilder {
212    let mut inner = ChatTypePrivate::default();
213    inner.td_name = "chatTypePrivate".to_string();
214    inner.extra = Some(Uuid::new_v4().to_string());
215    RTDChatTypePrivateBuilder { inner }
216  }
217
218  pub fn user_id(&self) -> i64 { self.user_id }
219
220}
221
222#[doc(hidden)]
223pub struct RTDChatTypePrivateBuilder {
224  inner: ChatTypePrivate
225}
226
227impl RTDChatTypePrivateBuilder {
228  pub fn build(&self) -> ChatTypePrivate { self.inner.clone() }
229
230   
231  pub fn user_id(&mut self, user_id: i64) -> &mut Self {
232    self.inner.user_id = user_id;
233    self
234  }
235
236}
237
238impl AsRef<ChatTypePrivate> for ChatTypePrivate {
239  fn as_ref(&self) -> &ChatTypePrivate { self }
240}
241
242impl AsRef<ChatTypePrivate> for RTDChatTypePrivateBuilder {
243  fn as_ref(&self) -> &ChatTypePrivate { &self.inner }
244}
245
246
247
248
249
250
251
252/// A secret chat with a user
253#[derive(Debug, Clone, Default, Serialize, Deserialize)]
254pub struct ChatTypeSecret {
255  #[doc(hidden)]
256  #[serde(rename(serialize = "@type", deserialize = "@type"))]
257  td_name: String,
258  #[doc(hidden)]
259  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
260  extra: Option<String>,
261  /// Secret chat identifier
262  secret_chat_id: i64,
263  /// User identifier of the secret chat peer
264  user_id: i64,
265  
266}
267
268impl RObject for ChatTypeSecret {
269  #[doc(hidden)] fn td_name(&self) -> &'static str { "chatTypeSecret" }
270  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
271  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
272}
273
274
275impl TDChatType for ChatTypeSecret {}
276
277
278
279impl ChatTypeSecret {
280  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
281  pub fn builder() -> RTDChatTypeSecretBuilder {
282    let mut inner = ChatTypeSecret::default();
283    inner.td_name = "chatTypeSecret".to_string();
284    inner.extra = Some(Uuid::new_v4().to_string());
285    RTDChatTypeSecretBuilder { inner }
286  }
287
288  pub fn secret_chat_id(&self) -> i64 { self.secret_chat_id }
289
290  pub fn user_id(&self) -> i64 { self.user_id }
291
292}
293
294#[doc(hidden)]
295pub struct RTDChatTypeSecretBuilder {
296  inner: ChatTypeSecret
297}
298
299impl RTDChatTypeSecretBuilder {
300  pub fn build(&self) -> ChatTypeSecret { self.inner.clone() }
301
302   
303  pub fn secret_chat_id(&mut self, secret_chat_id: i64) -> &mut Self {
304    self.inner.secret_chat_id = secret_chat_id;
305    self
306  }
307
308   
309  pub fn user_id(&mut self, user_id: i64) -> &mut Self {
310    self.inner.user_id = user_id;
311    self
312  }
313
314}
315
316impl AsRef<ChatTypeSecret> for ChatTypeSecret {
317  fn as_ref(&self) -> &ChatTypeSecret { self }
318}
319
320impl AsRef<ChatTypeSecret> for RTDChatTypeSecretBuilder {
321  fn as_ref(&self) -> &ChatTypeSecret { &self.inner }
322}
323
324
325
326
327
328
329
330/// A supergroup or channel (with unlimited members)
331#[derive(Debug, Clone, Default, Serialize, Deserialize)]
332pub struct ChatTypeSupergroup {
333  #[doc(hidden)]
334  #[serde(rename(serialize = "@type", deserialize = "@type"))]
335  td_name: String,
336  #[doc(hidden)]
337  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
338  extra: Option<String>,
339  /// Supergroup or channel identifier
340  supergroup_id: i64,
341  /// True, if the supergroup is a channel
342  is_channel: bool,
343  
344}
345
346impl RObject for ChatTypeSupergroup {
347  #[doc(hidden)] fn td_name(&self) -> &'static str { "chatTypeSupergroup" }
348  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
349  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
350}
351
352
353impl TDChatType for ChatTypeSupergroup {}
354
355
356
357impl ChatTypeSupergroup {
358  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
359  pub fn builder() -> RTDChatTypeSupergroupBuilder {
360    let mut inner = ChatTypeSupergroup::default();
361    inner.td_name = "chatTypeSupergroup".to_string();
362    inner.extra = Some(Uuid::new_v4().to_string());
363    RTDChatTypeSupergroupBuilder { inner }
364  }
365
366  pub fn supergroup_id(&self) -> i64 { self.supergroup_id }
367
368  pub fn is_channel(&self) -> bool { self.is_channel }
369
370}
371
372#[doc(hidden)]
373pub struct RTDChatTypeSupergroupBuilder {
374  inner: ChatTypeSupergroup
375}
376
377impl RTDChatTypeSupergroupBuilder {
378  pub fn build(&self) -> ChatTypeSupergroup { self.inner.clone() }
379
380   
381  pub fn supergroup_id(&mut self, supergroup_id: i64) -> &mut Self {
382    self.inner.supergroup_id = supergroup_id;
383    self
384  }
385
386   
387  pub fn is_channel(&mut self, is_channel: bool) -> &mut Self {
388    self.inner.is_channel = is_channel;
389    self
390  }
391
392}
393
394impl AsRef<ChatTypeSupergroup> for ChatTypeSupergroup {
395  fn as_ref(&self) -> &ChatTypeSupergroup { self }
396}
397
398impl AsRef<ChatTypeSupergroup> for RTDChatTypeSupergroupBuilder {
399  fn as_ref(&self) -> &ChatTypeSupergroup { &self.inner }
400}
401
402
403