rust_tdlib/types/
user_type.rs

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