rust_tdlib/types/
tdlib_parameters.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// Contains parameters for TDLib initialization
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct TdlibParameters {
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    /// If set to true, the Telegram test environment will be used instead of the production environment
14
15    #[serde(default)]
16    use_test_dc: bool,
17    /// The path to the directory for the persistent database; if empty, the current working directory will be used
18
19    #[serde(default)]
20    database_directory: String,
21    /// The path to the directory for storing files; if empty, database_directory will be used
22
23    #[serde(default)]
24    files_directory: String,
25    /// If set to true, information about downloaded and uploaded files will be saved between application restarts
26
27    #[serde(default)]
28    use_file_database: bool,
29    /// If set to true, the library will maintain a cache of users, basic groups, supergroups, channels and secret chats. Implies use_file_database
30
31    #[serde(default)]
32    use_chat_info_database: bool,
33    /// If set to true, the library will maintain a cache of chats and messages. Implies use_chat_info_database
34
35    #[serde(default)]
36    use_message_database: bool,
37    /// If set to true, support for secret chats will be enabled
38
39    #[serde(default)]
40    use_secret_chats: bool,
41    /// Application identifier for Telegram API access, which can be obtained at https://my.telegram.org
42
43    #[serde(default)]
44    api_id: i32,
45    /// Application identifier hash for Telegram API access, which can be obtained at https://my.telegram.org
46
47    #[serde(default)]
48    api_hash: String,
49    /// IETF language tag of the user's operating system language; must be non-empty
50
51    #[serde(default)]
52    system_language_code: String,
53    /// Model of the device the application is being run on; must be non-empty
54
55    #[serde(default)]
56    device_model: String,
57    /// Version of the operating system the application is being run on. If empty, the version is automatically detected by TDLib
58
59    #[serde(default)]
60    system_version: String,
61    /// Application version; must be non-empty
62
63    #[serde(default)]
64    application_version: String,
65    /// If set to true, old files will automatically be deleted
66
67    #[serde(default)]
68    enable_storage_optimizer: bool,
69    /// 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
70
71    #[serde(default)]
72    ignore_file_names: bool,
73}
74
75impl RObject for TdlibParameters {
76    #[doc(hidden)]
77    fn extra(&self) -> Option<&str> {
78        self.extra.as_deref()
79    }
80    #[doc(hidden)]
81    fn client_id(&self) -> Option<i32> {
82        self.client_id
83    }
84}
85
86impl TdlibParameters {
87    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
88        Ok(serde_json::from_str(json.as_ref())?)
89    }
90    pub fn builder() -> TdlibParametersBuilder {
91        let mut inner = TdlibParameters::default();
92        inner.extra = Some(Uuid::new_v4().to_string());
93
94        TdlibParametersBuilder { inner }
95    }
96
97    pub fn use_test_dc(&self) -> bool {
98        self.use_test_dc
99    }
100
101    pub fn database_directory(&self) -> &String {
102        &self.database_directory
103    }
104
105    pub fn files_directory(&self) -> &String {
106        &self.files_directory
107    }
108
109    pub fn use_file_database(&self) -> bool {
110        self.use_file_database
111    }
112
113    pub fn use_chat_info_database(&self) -> bool {
114        self.use_chat_info_database
115    }
116
117    pub fn use_message_database(&self) -> bool {
118        self.use_message_database
119    }
120
121    pub fn use_secret_chats(&self) -> bool {
122        self.use_secret_chats
123    }
124
125    pub fn api_id(&self) -> i32 {
126        self.api_id
127    }
128
129    pub fn api_hash(&self) -> &String {
130        &self.api_hash
131    }
132
133    pub fn system_language_code(&self) -> &String {
134        &self.system_language_code
135    }
136
137    pub fn device_model(&self) -> &String {
138        &self.device_model
139    }
140
141    pub fn system_version(&self) -> &String {
142        &self.system_version
143    }
144
145    pub fn application_version(&self) -> &String {
146        &self.application_version
147    }
148
149    pub fn enable_storage_optimizer(&self) -> bool {
150        self.enable_storage_optimizer
151    }
152
153    pub fn ignore_file_names(&self) -> bool {
154        self.ignore_file_names
155    }
156}
157
158#[doc(hidden)]
159pub struct TdlibParametersBuilder {
160    inner: TdlibParameters,
161}
162
163#[deprecated]
164pub type RTDTdlibParametersBuilder = TdlibParametersBuilder;
165
166impl TdlibParametersBuilder {
167    pub fn build(&self) -> TdlibParameters {
168        self.inner.clone()
169    }
170
171    pub fn use_test_dc(&mut self, use_test_dc: bool) -> &mut Self {
172        self.inner.use_test_dc = use_test_dc;
173        self
174    }
175
176    pub fn database_directory<T: AsRef<str>>(&mut self, database_directory: T) -> &mut Self {
177        self.inner.database_directory = database_directory.as_ref().to_string();
178        self
179    }
180
181    pub fn files_directory<T: AsRef<str>>(&mut self, files_directory: T) -> &mut Self {
182        self.inner.files_directory = files_directory.as_ref().to_string();
183        self
184    }
185
186    pub fn use_file_database(&mut self, use_file_database: bool) -> &mut Self {
187        self.inner.use_file_database = use_file_database;
188        self
189    }
190
191    pub fn use_chat_info_database(&mut self, use_chat_info_database: bool) -> &mut Self {
192        self.inner.use_chat_info_database = use_chat_info_database;
193        self
194    }
195
196    pub fn use_message_database(&mut self, use_message_database: bool) -> &mut Self {
197        self.inner.use_message_database = use_message_database;
198        self
199    }
200
201    pub fn use_secret_chats(&mut self, use_secret_chats: bool) -> &mut Self {
202        self.inner.use_secret_chats = use_secret_chats;
203        self
204    }
205
206    pub fn api_id(&mut self, api_id: i32) -> &mut Self {
207        self.inner.api_id = api_id;
208        self
209    }
210
211    pub fn api_hash<T: AsRef<str>>(&mut self, api_hash: T) -> &mut Self {
212        self.inner.api_hash = api_hash.as_ref().to_string();
213        self
214    }
215
216    pub fn system_language_code<T: AsRef<str>>(&mut self, system_language_code: T) -> &mut Self {
217        self.inner.system_language_code = system_language_code.as_ref().to_string();
218        self
219    }
220
221    pub fn device_model<T: AsRef<str>>(&mut self, device_model: T) -> &mut Self {
222        self.inner.device_model = device_model.as_ref().to_string();
223        self
224    }
225
226    pub fn system_version<T: AsRef<str>>(&mut self, system_version: T) -> &mut Self {
227        self.inner.system_version = system_version.as_ref().to_string();
228        self
229    }
230
231    pub fn application_version<T: AsRef<str>>(&mut self, application_version: T) -> &mut Self {
232        self.inner.application_version = application_version.as_ref().to_string();
233        self
234    }
235
236    pub fn enable_storage_optimizer(&mut self, enable_storage_optimizer: bool) -> &mut Self {
237        self.inner.enable_storage_optimizer = enable_storage_optimizer;
238        self
239    }
240
241    pub fn ignore_file_names(&mut self, ignore_file_names: bool) -> &mut Self {
242        self.inner.ignore_file_names = ignore_file_names;
243        self
244    }
245}
246
247impl AsRef<TdlibParameters> for TdlibParameters {
248    fn as_ref(&self) -> &TdlibParameters {
249        self
250    }
251}
252
253impl AsRef<TdlibParameters> for TdlibParametersBuilder {
254    fn as_ref(&self) -> &TdlibParameters {
255        &self.inner
256    }
257}