rust_tdlib/types/
user.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// Represents a user
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct User {
8    #[doc(hidden)]
9    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
10    extra: Option<String>,
11    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
12    client_id: Option<i32>,
13    /// User identifier
14
15    #[serde(default)]
16    id: i64,
17    /// First name of the user
18
19    #[serde(default)]
20    first_name: String,
21    /// Last name of the user
22
23    #[serde(default)]
24    last_name: String,
25    /// Username of the user
26
27    #[serde(default)]
28    username: String,
29    /// Phone number of the user
30
31    #[serde(default)]
32    phone_number: String,
33    /// Current online status of the user
34
35    #[serde(skip_serializing_if = "UserStatus::_is_default")]
36    status: UserStatus,
37    /// Profile photo of the user; may be null
38    profile_photo: Option<ProfilePhoto>,
39    /// The user is a contact of the current user
40
41    #[serde(default)]
42    is_contact: bool,
43    /// The user is a contact of the current user and the current user is a contact of the user
44
45    #[serde(default)]
46    is_mutual_contact: bool,
47    /// True, if the user is verified
48
49    #[serde(default)]
50    is_verified: bool,
51    /// True, if the user is Telegram support account
52
53    #[serde(default)]
54    is_support: bool,
55    /// If non-empty, it contains a human-readable description of the reason why access to this user must be restricted
56
57    #[serde(default)]
58    restriction_reason: String,
59    /// True, if many users reported this user as a scam
60
61    #[serde(default)]
62    is_scam: bool,
63    /// True, if many users reported this user as a fake account
64
65    #[serde(default)]
66    is_fake: bool,
67    /// If false, the user is inaccessible, and the only information known about the user is inside this class. It can't be passed to any method except GetUser
68
69    #[serde(default)]
70    have_access: bool,
71    /// Type of the user
72
73    #[serde(rename(serialize = "type", deserialize = "type"))]
74    #[serde(skip_serializing_if = "UserType::_is_default")]
75    type_: UserType,
76    /// IETF language tag of the user's language; only available to bots
77
78    #[serde(default)]
79    language_code: String,
80}
81
82impl RObject for User {
83    #[doc(hidden)]
84    fn extra(&self) -> Option<&str> {
85        self.extra.as_deref()
86    }
87    #[doc(hidden)]
88    fn client_id(&self) -> Option<i32> {
89        self.client_id
90    }
91}
92
93impl User {
94    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
95        Ok(serde_json::from_str(json.as_ref())?)
96    }
97    pub fn builder() -> UserBuilder {
98        let mut inner = User::default();
99        inner.extra = Some(Uuid::new_v4().to_string());
100
101        UserBuilder { inner }
102    }
103
104    pub fn id(&self) -> i64 {
105        self.id
106    }
107
108    pub fn first_name(&self) -> &String {
109        &self.first_name
110    }
111
112    pub fn last_name(&self) -> &String {
113        &self.last_name
114    }
115
116    pub fn username(&self) -> &String {
117        &self.username
118    }
119
120    pub fn phone_number(&self) -> &String {
121        &self.phone_number
122    }
123
124    pub fn status(&self) -> &UserStatus {
125        &self.status
126    }
127
128    pub fn profile_photo(&self) -> &Option<ProfilePhoto> {
129        &self.profile_photo
130    }
131
132    pub fn is_contact(&self) -> bool {
133        self.is_contact
134    }
135
136    pub fn is_mutual_contact(&self) -> bool {
137        self.is_mutual_contact
138    }
139
140    pub fn is_verified(&self) -> bool {
141        self.is_verified
142    }
143
144    pub fn is_support(&self) -> bool {
145        self.is_support
146    }
147
148    pub fn restriction_reason(&self) -> &String {
149        &self.restriction_reason
150    }
151
152    pub fn is_scam(&self) -> bool {
153        self.is_scam
154    }
155
156    pub fn is_fake(&self) -> bool {
157        self.is_fake
158    }
159
160    pub fn have_access(&self) -> bool {
161        self.have_access
162    }
163
164    pub fn type_(&self) -> &UserType {
165        &self.type_
166    }
167
168    pub fn language_code(&self) -> &String {
169        &self.language_code
170    }
171}
172
173#[doc(hidden)]
174pub struct UserBuilder {
175    inner: User,
176}
177
178#[deprecated]
179pub type RTDUserBuilder = UserBuilder;
180
181impl UserBuilder {
182    pub fn build(&self) -> User {
183        self.inner.clone()
184    }
185
186    pub fn id(&mut self, id: i64) -> &mut Self {
187        self.inner.id = id;
188        self
189    }
190
191    pub fn first_name<T: AsRef<str>>(&mut self, first_name: T) -> &mut Self {
192        self.inner.first_name = first_name.as_ref().to_string();
193        self
194    }
195
196    pub fn last_name<T: AsRef<str>>(&mut self, last_name: T) -> &mut Self {
197        self.inner.last_name = last_name.as_ref().to_string();
198        self
199    }
200
201    pub fn username<T: AsRef<str>>(&mut self, username: T) -> &mut Self {
202        self.inner.username = username.as_ref().to_string();
203        self
204    }
205
206    pub fn phone_number<T: AsRef<str>>(&mut self, phone_number: T) -> &mut Self {
207        self.inner.phone_number = phone_number.as_ref().to_string();
208        self
209    }
210
211    pub fn status<T: AsRef<UserStatus>>(&mut self, status: T) -> &mut Self {
212        self.inner.status = status.as_ref().clone();
213        self
214    }
215
216    pub fn profile_photo<T: AsRef<ProfilePhoto>>(&mut self, profile_photo: T) -> &mut Self {
217        self.inner.profile_photo = Some(profile_photo.as_ref().clone());
218        self
219    }
220
221    pub fn is_contact(&mut self, is_contact: bool) -> &mut Self {
222        self.inner.is_contact = is_contact;
223        self
224    }
225
226    pub fn is_mutual_contact(&mut self, is_mutual_contact: bool) -> &mut Self {
227        self.inner.is_mutual_contact = is_mutual_contact;
228        self
229    }
230
231    pub fn is_verified(&mut self, is_verified: bool) -> &mut Self {
232        self.inner.is_verified = is_verified;
233        self
234    }
235
236    pub fn is_support(&mut self, is_support: bool) -> &mut Self {
237        self.inner.is_support = is_support;
238        self
239    }
240
241    pub fn restriction_reason<T: AsRef<str>>(&mut self, restriction_reason: T) -> &mut Self {
242        self.inner.restriction_reason = restriction_reason.as_ref().to_string();
243        self
244    }
245
246    pub fn is_scam(&mut self, is_scam: bool) -> &mut Self {
247        self.inner.is_scam = is_scam;
248        self
249    }
250
251    pub fn is_fake(&mut self, is_fake: bool) -> &mut Self {
252        self.inner.is_fake = is_fake;
253        self
254    }
255
256    pub fn have_access(&mut self, have_access: bool) -> &mut Self {
257        self.inner.have_access = have_access;
258        self
259    }
260
261    pub fn type_<T: AsRef<UserType>>(&mut self, type_: T) -> &mut Self {
262        self.inner.type_ = type_.as_ref().clone();
263        self
264    }
265
266    pub fn language_code<T: AsRef<str>>(&mut self, language_code: T) -> &mut Self {
267        self.inner.language_code = language_code.as_ref().to_string();
268        self
269    }
270}
271
272impl AsRef<User> for User {
273    fn as_ref(&self) -> &User {
274        self
275    }
276}
277
278impl AsRef<User> for UserBuilder {
279    fn as_ref(&self) -> &User {
280        &self.inner
281    }
282}