rust_tdlib/types/
option_value.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5use std::fmt::Debug;
6
7/// Represents the value of an option
8pub trait TDOptionValue: Debug + RObject {}
9
10/// Represents the value of an option
11#[derive(Debug, Clone, Deserialize, Serialize, Default)]
12#[serde(tag = "@type")]
13pub enum OptionValue {
14    #[doc(hidden)]
15    #[default]
16    _Default,
17    /// Returns the value of an option by its name. (Check the list of available options on https://core.telegram.org/tdlib/options.) Can be called before authorization
18    #[serde(rename = "getOption")]
19    GetOption(GetOption),
20    /// Represents a boolean option
21    #[serde(rename = "optionValueBoolean")]
22    Boolean(OptionValueBoolean),
23    /// Represents an unknown option or an option which has a default value
24    #[serde(rename = "optionValueEmpty")]
25    Empty(OptionValueEmpty),
26    /// Represents an integer option
27    #[serde(rename = "optionValueInteger")]
28    Integer(OptionValueInteger),
29    /// Represents a string option
30    #[serde(rename = "optionValueString")]
31    String(OptionValueString),
32}
33
34impl RObject for OptionValue {
35    #[doc(hidden)]
36    fn extra(&self) -> Option<&str> {
37        match self {
38            OptionValue::GetOption(t) => t.extra(),
39            OptionValue::Boolean(t) => t.extra(),
40            OptionValue::Empty(t) => t.extra(),
41            OptionValue::Integer(t) => t.extra(),
42            OptionValue::String(t) => t.extra(),
43
44            _ => None,
45        }
46    }
47    #[doc(hidden)]
48    fn client_id(&self) -> Option<i32> {
49        match self {
50            OptionValue::GetOption(t) => t.client_id(),
51            OptionValue::Boolean(t) => t.client_id(),
52            OptionValue::Empty(t) => t.client_id(),
53            OptionValue::Integer(t) => t.client_id(),
54            OptionValue::String(t) => t.client_id(),
55
56            _ => None,
57        }
58    }
59}
60
61impl OptionValue {
62    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
63        Ok(serde_json::from_str(json.as_ref())?)
64    }
65    #[doc(hidden)]
66    pub fn _is_default(&self) -> bool {
67        matches!(self, OptionValue::_Default)
68    }
69}
70
71impl AsRef<OptionValue> for OptionValue {
72    fn as_ref(&self) -> &OptionValue {
73        self
74    }
75}
76
77/// Represents a boolean option
78#[derive(Debug, Clone, Default, Serialize, Deserialize)]
79pub struct OptionValueBoolean {
80    #[doc(hidden)]
81    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
82    extra: Option<String>,
83    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
84    client_id: Option<i32>,
85    /// The value of the option
86
87    #[serde(default)]
88    value: bool,
89}
90
91impl RObject for OptionValueBoolean {
92    #[doc(hidden)]
93    fn extra(&self) -> Option<&str> {
94        self.extra.as_deref()
95    }
96    #[doc(hidden)]
97    fn client_id(&self) -> Option<i32> {
98        self.client_id
99    }
100}
101
102impl TDOptionValue for OptionValueBoolean {}
103
104impl OptionValueBoolean {
105    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
106        Ok(serde_json::from_str(json.as_ref())?)
107    }
108    pub fn builder() -> OptionValueBooleanBuilder {
109        let mut inner = OptionValueBoolean::default();
110        inner.extra = Some(Uuid::new_v4().to_string());
111
112        OptionValueBooleanBuilder { inner }
113    }
114
115    pub fn value(&self) -> bool {
116        self.value
117    }
118}
119
120#[doc(hidden)]
121pub struct OptionValueBooleanBuilder {
122    inner: OptionValueBoolean,
123}
124
125#[deprecated]
126pub type RTDOptionValueBooleanBuilder = OptionValueBooleanBuilder;
127
128impl OptionValueBooleanBuilder {
129    pub fn build(&self) -> OptionValueBoolean {
130        self.inner.clone()
131    }
132
133    pub fn value(&mut self, value: bool) -> &mut Self {
134        self.inner.value = value;
135        self
136    }
137}
138
139impl AsRef<OptionValueBoolean> for OptionValueBoolean {
140    fn as_ref(&self) -> &OptionValueBoolean {
141        self
142    }
143}
144
145impl AsRef<OptionValueBoolean> for OptionValueBooleanBuilder {
146    fn as_ref(&self) -> &OptionValueBoolean {
147        &self.inner
148    }
149}
150
151/// Represents an unknown option or an option which has a default value
152#[derive(Debug, Clone, Default, Serialize, Deserialize)]
153pub struct OptionValueEmpty {
154    #[doc(hidden)]
155    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
156    extra: Option<String>,
157    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
158    client_id: Option<i32>,
159}
160
161impl RObject for OptionValueEmpty {
162    #[doc(hidden)]
163    fn extra(&self) -> Option<&str> {
164        self.extra.as_deref()
165    }
166    #[doc(hidden)]
167    fn client_id(&self) -> Option<i32> {
168        self.client_id
169    }
170}
171
172impl TDOptionValue for OptionValueEmpty {}
173
174impl OptionValueEmpty {
175    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
176        Ok(serde_json::from_str(json.as_ref())?)
177    }
178    pub fn builder() -> OptionValueEmptyBuilder {
179        let mut inner = OptionValueEmpty::default();
180        inner.extra = Some(Uuid::new_v4().to_string());
181
182        OptionValueEmptyBuilder { inner }
183    }
184}
185
186#[doc(hidden)]
187pub struct OptionValueEmptyBuilder {
188    inner: OptionValueEmpty,
189}
190
191#[deprecated]
192pub type RTDOptionValueEmptyBuilder = OptionValueEmptyBuilder;
193
194impl OptionValueEmptyBuilder {
195    pub fn build(&self) -> OptionValueEmpty {
196        self.inner.clone()
197    }
198}
199
200impl AsRef<OptionValueEmpty> for OptionValueEmpty {
201    fn as_ref(&self) -> &OptionValueEmpty {
202        self
203    }
204}
205
206impl AsRef<OptionValueEmpty> for OptionValueEmptyBuilder {
207    fn as_ref(&self) -> &OptionValueEmpty {
208        &self.inner
209    }
210}
211
212/// Represents an integer option
213#[derive(Debug, Clone, Default, Serialize, Deserialize)]
214pub struct OptionValueInteger {
215    #[doc(hidden)]
216    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
217    extra: Option<String>,
218    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
219    client_id: Option<i32>,
220    /// The value of the option
221
222    #[serde(
223        deserialize_with = "super::_common::number_from_string",
224        serialize_with = "super::_common::string_to_number"
225    )]
226    #[serde(default)]
227    value: i64,
228}
229
230impl RObject for OptionValueInteger {
231    #[doc(hidden)]
232    fn extra(&self) -> Option<&str> {
233        self.extra.as_deref()
234    }
235    #[doc(hidden)]
236    fn client_id(&self) -> Option<i32> {
237        self.client_id
238    }
239}
240
241impl TDOptionValue for OptionValueInteger {}
242
243impl OptionValueInteger {
244    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
245        Ok(serde_json::from_str(json.as_ref())?)
246    }
247    pub fn builder() -> OptionValueIntegerBuilder {
248        let mut inner = OptionValueInteger::default();
249        inner.extra = Some(Uuid::new_v4().to_string());
250
251        OptionValueIntegerBuilder { inner }
252    }
253
254    pub fn value(&self) -> i64 {
255        self.value
256    }
257}
258
259#[doc(hidden)]
260pub struct OptionValueIntegerBuilder {
261    inner: OptionValueInteger,
262}
263
264#[deprecated]
265pub type RTDOptionValueIntegerBuilder = OptionValueIntegerBuilder;
266
267impl OptionValueIntegerBuilder {
268    pub fn build(&self) -> OptionValueInteger {
269        self.inner.clone()
270    }
271
272    pub fn value(&mut self, value: i64) -> &mut Self {
273        self.inner.value = value;
274        self
275    }
276}
277
278impl AsRef<OptionValueInteger> for OptionValueInteger {
279    fn as_ref(&self) -> &OptionValueInteger {
280        self
281    }
282}
283
284impl AsRef<OptionValueInteger> for OptionValueIntegerBuilder {
285    fn as_ref(&self) -> &OptionValueInteger {
286        &self.inner
287    }
288}
289
290/// Represents a string option
291#[derive(Debug, Clone, Default, Serialize, Deserialize)]
292pub struct OptionValueString {
293    #[doc(hidden)]
294    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
295    extra: Option<String>,
296    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
297    client_id: Option<i32>,
298    /// The value of the option
299
300    #[serde(default)]
301    value: String,
302}
303
304impl RObject for OptionValueString {
305    #[doc(hidden)]
306    fn extra(&self) -> Option<&str> {
307        self.extra.as_deref()
308    }
309    #[doc(hidden)]
310    fn client_id(&self) -> Option<i32> {
311        self.client_id
312    }
313}
314
315impl TDOptionValue for OptionValueString {}
316
317impl OptionValueString {
318    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
319        Ok(serde_json::from_str(json.as_ref())?)
320    }
321    pub fn builder() -> OptionValueStringBuilder {
322        let mut inner = OptionValueString::default();
323        inner.extra = Some(Uuid::new_v4().to_string());
324
325        OptionValueStringBuilder { inner }
326    }
327
328    pub fn value(&self) -> &String {
329        &self.value
330    }
331}
332
333#[doc(hidden)]
334pub struct OptionValueStringBuilder {
335    inner: OptionValueString,
336}
337
338#[deprecated]
339pub type RTDOptionValueStringBuilder = OptionValueStringBuilder;
340
341impl OptionValueStringBuilder {
342    pub fn build(&self) -> OptionValueString {
343        self.inner.clone()
344    }
345
346    pub fn value<T: AsRef<str>>(&mut self, value: T) -> &mut Self {
347        self.inner.value = value.as_ref().to_string();
348        self
349    }
350}
351
352impl AsRef<OptionValueString> for OptionValueString {
353    fn as_ref(&self) -> &OptionValueString {
354        self
355    }
356}
357
358impl AsRef<OptionValueString> for OptionValueStringBuilder {
359    fn as_ref(&self) -> &OptionValueString {
360        &self.inner
361    }
362}