1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use crate::errors::Result;
use crate::types::*;
use uuid::Uuid;

/// Contains parameters for TDLib initialization
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TdlibParameters {
    #[doc(hidden)]
    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
    extra: Option<String>,
    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
    client_id: Option<i32>,
    /// If set to true, the Telegram test environment will be used instead of the production environment

    #[serde(default)]
    use_test_dc: bool,
    /// The path to the directory for the persistent database; if empty, the current working directory will be used

    #[serde(default)]
    database_directory: String,
    /// The path to the directory for storing files; if empty, database_directory will be used

    #[serde(default)]
    files_directory: String,
    /// If set to true, information about downloaded and uploaded files will be saved between application restarts

    #[serde(default)]
    use_file_database: bool,
    /// If set to true, the library will maintain a cache of users, basic groups, supergroups, channels and secret chats. Implies use_file_database

    #[serde(default)]
    use_chat_info_database: bool,
    /// If set to true, the library will maintain a cache of chats and messages. Implies use_chat_info_database

    #[serde(default)]
    use_message_database: bool,
    /// If set to true, support for secret chats will be enabled

    #[serde(default)]
    use_secret_chats: bool,
    /// Application identifier for Telegram API access, which can be obtained at https://my.telegram.org

    #[serde(default)]
    api_id: i32,
    /// Application identifier hash for Telegram API access, which can be obtained at https://my.telegram.org

    #[serde(default)]
    api_hash: String,
    /// IETF language tag of the user's operating system language; must be non-empty

    #[serde(default)]
    system_language_code: String,
    /// Model of the device the application is being run on; must be non-empty

    #[serde(default)]
    device_model: String,
    /// Version of the operating system the application is being run on. If empty, the version is automatically detected by TDLib

    #[serde(default)]
    system_version: String,
    /// Application version; must be non-empty

    #[serde(default)]
    application_version: String,
    /// If set to true, old files will automatically be deleted

    #[serde(default)]
    enable_storage_optimizer: bool,
    /// If set to true, original file names will be ignored. Otherwise, downloaded files will be saved under names as close as possible to the original name

    #[serde(default)]
    ignore_file_names: bool,
}

impl RObject for TdlibParameters {
    #[doc(hidden)]
    fn extra(&self) -> Option<&str> {
        self.extra.as_deref()
    }
    #[doc(hidden)]
    fn client_id(&self) -> Option<i32> {
        self.client_id
    }
}

impl TdlibParameters {
    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
        Ok(serde_json::from_str(json.as_ref())?)
    }
    pub fn builder() -> TdlibParametersBuilder {
        let mut inner = TdlibParameters::default();
        inner.extra = Some(Uuid::new_v4().to_string());

        TdlibParametersBuilder { inner }
    }

    pub fn use_test_dc(&self) -> bool {
        self.use_test_dc
    }

    pub fn database_directory(&self) -> &String {
        &self.database_directory
    }

    pub fn files_directory(&self) -> &String {
        &self.files_directory
    }

    pub fn use_file_database(&self) -> bool {
        self.use_file_database
    }

    pub fn use_chat_info_database(&self) -> bool {
        self.use_chat_info_database
    }

    pub fn use_message_database(&self) -> bool {
        self.use_message_database
    }

    pub fn use_secret_chats(&self) -> bool {
        self.use_secret_chats
    }

    pub fn api_id(&self) -> i32 {
        self.api_id
    }

    pub fn api_hash(&self) -> &String {
        &self.api_hash
    }

    pub fn system_language_code(&self) -> &String {
        &self.system_language_code
    }

    pub fn device_model(&self) -> &String {
        &self.device_model
    }

    pub fn system_version(&self) -> &String {
        &self.system_version
    }

    pub fn application_version(&self) -> &String {
        &self.application_version
    }

    pub fn enable_storage_optimizer(&self) -> bool {
        self.enable_storage_optimizer
    }

    pub fn ignore_file_names(&self) -> bool {
        self.ignore_file_names
    }
}

#[doc(hidden)]
pub struct TdlibParametersBuilder {
    inner: TdlibParameters,
}

#[deprecated]
pub type RTDTdlibParametersBuilder = TdlibParametersBuilder;

impl TdlibParametersBuilder {
    pub fn build(&self) -> TdlibParameters {
        self.inner.clone()
    }

    pub fn use_test_dc(&mut self, use_test_dc: bool) -> &mut Self {
        self.inner.use_test_dc = use_test_dc;
        self
    }

    pub fn database_directory<T: AsRef<str>>(&mut self, database_directory: T) -> &mut Self {
        self.inner.database_directory = database_directory.as_ref().to_string();
        self
    }

    pub fn files_directory<T: AsRef<str>>(&mut self, files_directory: T) -> &mut Self {
        self.inner.files_directory = files_directory.as_ref().to_string();
        self
    }

    pub fn use_file_database(&mut self, use_file_database: bool) -> &mut Self {
        self.inner.use_file_database = use_file_database;
        self
    }

    pub fn use_chat_info_database(&mut self, use_chat_info_database: bool) -> &mut Self {
        self.inner.use_chat_info_database = use_chat_info_database;
        self
    }

    pub fn use_message_database(&mut self, use_message_database: bool) -> &mut Self {
        self.inner.use_message_database = use_message_database;
        self
    }

    pub fn use_secret_chats(&mut self, use_secret_chats: bool) -> &mut Self {
        self.inner.use_secret_chats = use_secret_chats;
        self
    }

    pub fn api_id(&mut self, api_id: i32) -> &mut Self {
        self.inner.api_id = api_id;
        self
    }

    pub fn api_hash<T: AsRef<str>>(&mut self, api_hash: T) -> &mut Self {
        self.inner.api_hash = api_hash.as_ref().to_string();
        self
    }

    pub fn system_language_code<T: AsRef<str>>(&mut self, system_language_code: T) -> &mut Self {
        self.inner.system_language_code = system_language_code.as_ref().to_string();
        self
    }

    pub fn device_model<T: AsRef<str>>(&mut self, device_model: T) -> &mut Self {
        self.inner.device_model = device_model.as_ref().to_string();
        self
    }

    pub fn system_version<T: AsRef<str>>(&mut self, system_version: T) -> &mut Self {
        self.inner.system_version = system_version.as_ref().to_string();
        self
    }

    pub fn application_version<T: AsRef<str>>(&mut self, application_version: T) -> &mut Self {
        self.inner.application_version = application_version.as_ref().to_string();
        self
    }

    pub fn enable_storage_optimizer(&mut self, enable_storage_optimizer: bool) -> &mut Self {
        self.inner.enable_storage_optimizer = enable_storage_optimizer;
        self
    }

    pub fn ignore_file_names(&mut self, ignore_file_names: bool) -> &mut Self {
        self.inner.ignore_file_names = ignore_file_names;
        self
    }
}

impl AsRef<TdlibParameters> for TdlibParameters {
    fn as_ref(&self) -> &TdlibParameters {
        self
    }
}

impl AsRef<TdlibParameters> for TdlibParametersBuilder {
    fn as_ref(&self) -> &TdlibParameters {
        &self.inner
    }
}