rust_tdlib/types/
set_option.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// Sets the value of an option. (Check the list of available options on https://core.telegram.org/tdlib/options.) Only writable options can be set. Can be called before authorization
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct SetOption {
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    /// The name of the option
14
15    #[serde(default)]
16    name: String,
17    /// The new value of the option; pass null to reset option value to a default value
18
19    #[serde(skip_serializing_if = "OptionValue::_is_default")]
20    value: OptionValue,
21
22    #[serde(rename(serialize = "@type"))]
23    td_type: String,
24}
25
26impl RObject for SetOption {
27    #[doc(hidden)]
28    fn extra(&self) -> Option<&str> {
29        self.extra.as_deref()
30    }
31    #[doc(hidden)]
32    fn client_id(&self) -> Option<i32> {
33        self.client_id
34    }
35}
36
37impl RFunction for SetOption {}
38
39impl SetOption {
40    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
41        Ok(serde_json::from_str(json.as_ref())?)
42    }
43    pub fn builder() -> SetOptionBuilder {
44        let mut inner = SetOption::default();
45        inner.extra = Some(Uuid::new_v4().to_string());
46
47        inner.td_type = "setOption".to_string();
48
49        SetOptionBuilder { inner }
50    }
51
52    pub fn name(&self) -> &String {
53        &self.name
54    }
55
56    pub fn value(&self) -> &OptionValue {
57        &self.value
58    }
59}
60
61#[doc(hidden)]
62pub struct SetOptionBuilder {
63    inner: SetOption,
64}
65
66#[deprecated]
67pub type RTDSetOptionBuilder = SetOptionBuilder;
68
69impl SetOptionBuilder {
70    pub fn build(&self) -> SetOption {
71        self.inner.clone()
72    }
73
74    pub fn name<T: AsRef<str>>(&mut self, name: T) -> &mut Self {
75        self.inner.name = name.as_ref().to_string();
76        self
77    }
78
79    pub fn value<T: AsRef<OptionValue>>(&mut self, value: T) -> &mut Self {
80        self.inner.value = value.as_ref().clone();
81        self
82    }
83}
84
85impl AsRef<SetOption> for SetOption {
86    fn as_ref(&self) -> &SetOption {
87        self
88    }
89}
90
91impl AsRef<SetOption> for SetOptionBuilder {
92    fn as_ref(&self) -> &SetOption {
93        &self.inner
94    }
95}