rust_tdlib/types/
background_type.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5use std::fmt::Debug;
6
7/// Describes the type of a background
8pub trait TDBackgroundType: Debug + RObject {}
9
10/// Describes the type of a background
11#[derive(Debug, Clone, Deserialize, Serialize, Default)]
12#[serde(tag = "@type")]
13pub enum BackgroundType {
14    #[doc(hidden)]
15    #[default]
16    _Default,
17    /// A filled background
18    #[serde(rename = "backgroundTypeFill")]
19    Fill(BackgroundTypeFill),
20    /// A PNG or TGV (gzipped subset of SVG with MIME type "application/x-tgwallpattern") pattern to be combined with the background fill chosen by the user
21    #[serde(rename = "backgroundTypePattern")]
22    Pattern(BackgroundTypePattern),
23    /// A wallpaper in JPEG format
24    #[serde(rename = "backgroundTypeWallpaper")]
25    Wallpaper(BackgroundTypeWallpaper),
26}
27
28impl RObject for BackgroundType {
29    #[doc(hidden)]
30    fn extra(&self) -> Option<&str> {
31        match self {
32            BackgroundType::Fill(t) => t.extra(),
33            BackgroundType::Pattern(t) => t.extra(),
34            BackgroundType::Wallpaper(t) => t.extra(),
35
36            _ => None,
37        }
38    }
39    #[doc(hidden)]
40    fn client_id(&self) -> Option<i32> {
41        match self {
42            BackgroundType::Fill(t) => t.client_id(),
43            BackgroundType::Pattern(t) => t.client_id(),
44            BackgroundType::Wallpaper(t) => t.client_id(),
45
46            _ => None,
47        }
48    }
49}
50
51impl BackgroundType {
52    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
53        Ok(serde_json::from_str(json.as_ref())?)
54    }
55    #[doc(hidden)]
56    pub fn _is_default(&self) -> bool {
57        matches!(self, BackgroundType::_Default)
58    }
59}
60
61impl AsRef<BackgroundType> for BackgroundType {
62    fn as_ref(&self) -> &BackgroundType {
63        self
64    }
65}
66
67/// A filled background
68#[derive(Debug, Clone, Default, Serialize, Deserialize)]
69pub struct BackgroundTypeFill {
70    #[doc(hidden)]
71    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
72    extra: Option<String>,
73    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
74    client_id: Option<i32>,
75    /// The background fill
76
77    #[serde(skip_serializing_if = "BackgroundFill::_is_default")]
78    fill: BackgroundFill,
79}
80
81impl RObject for BackgroundTypeFill {
82    #[doc(hidden)]
83    fn extra(&self) -> Option<&str> {
84        self.extra.as_deref()
85    }
86    #[doc(hidden)]
87    fn client_id(&self) -> Option<i32> {
88        self.client_id
89    }
90}
91
92impl TDBackgroundType for BackgroundTypeFill {}
93
94impl BackgroundTypeFill {
95    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
96        Ok(serde_json::from_str(json.as_ref())?)
97    }
98    pub fn builder() -> BackgroundTypeFillBuilder {
99        let mut inner = BackgroundTypeFill::default();
100        inner.extra = Some(Uuid::new_v4().to_string());
101
102        BackgroundTypeFillBuilder { inner }
103    }
104
105    pub fn fill(&self) -> &BackgroundFill {
106        &self.fill
107    }
108}
109
110#[doc(hidden)]
111pub struct BackgroundTypeFillBuilder {
112    inner: BackgroundTypeFill,
113}
114
115#[deprecated]
116pub type RTDBackgroundTypeFillBuilder = BackgroundTypeFillBuilder;
117
118impl BackgroundTypeFillBuilder {
119    pub fn build(&self) -> BackgroundTypeFill {
120        self.inner.clone()
121    }
122
123    pub fn fill<T: AsRef<BackgroundFill>>(&mut self, fill: T) -> &mut Self {
124        self.inner.fill = fill.as_ref().clone();
125        self
126    }
127}
128
129impl AsRef<BackgroundTypeFill> for BackgroundTypeFill {
130    fn as_ref(&self) -> &BackgroundTypeFill {
131        self
132    }
133}
134
135impl AsRef<BackgroundTypeFill> for BackgroundTypeFillBuilder {
136    fn as_ref(&self) -> &BackgroundTypeFill {
137        &self.inner
138    }
139}
140
141/// A PNG or TGV (gzipped subset of SVG with MIME type "application/x-tgwallpattern") pattern to be combined with the background fill chosen by the user
142#[derive(Debug, Clone, Default, Serialize, Deserialize)]
143pub struct BackgroundTypePattern {
144    #[doc(hidden)]
145    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
146    extra: Option<String>,
147    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
148    client_id: Option<i32>,
149    /// Fill of the background
150
151    #[serde(skip_serializing_if = "BackgroundFill::_is_default")]
152    fill: BackgroundFill,
153    /// Intensity of the pattern when it is shown above the filled background; 0-100.
154
155    #[serde(default)]
156    intensity: i32,
157    /// True, if the background fill must be applied only to the pattern itself. All other pixels are black in this case. For dark themes only
158
159    #[serde(default)]
160    is_inverted: bool,
161    /// True, if the background needs to be slightly moved when device is tilted
162
163    #[serde(default)]
164    is_moving: bool,
165}
166
167impl RObject for BackgroundTypePattern {
168    #[doc(hidden)]
169    fn extra(&self) -> Option<&str> {
170        self.extra.as_deref()
171    }
172    #[doc(hidden)]
173    fn client_id(&self) -> Option<i32> {
174        self.client_id
175    }
176}
177
178impl TDBackgroundType for BackgroundTypePattern {}
179
180impl BackgroundTypePattern {
181    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
182        Ok(serde_json::from_str(json.as_ref())?)
183    }
184    pub fn builder() -> BackgroundTypePatternBuilder {
185        let mut inner = BackgroundTypePattern::default();
186        inner.extra = Some(Uuid::new_v4().to_string());
187
188        BackgroundTypePatternBuilder { inner }
189    }
190
191    pub fn fill(&self) -> &BackgroundFill {
192        &self.fill
193    }
194
195    pub fn intensity(&self) -> i32 {
196        self.intensity
197    }
198
199    pub fn is_inverted(&self) -> bool {
200        self.is_inverted
201    }
202
203    pub fn is_moving(&self) -> bool {
204        self.is_moving
205    }
206}
207
208#[doc(hidden)]
209pub struct BackgroundTypePatternBuilder {
210    inner: BackgroundTypePattern,
211}
212
213#[deprecated]
214pub type RTDBackgroundTypePatternBuilder = BackgroundTypePatternBuilder;
215
216impl BackgroundTypePatternBuilder {
217    pub fn build(&self) -> BackgroundTypePattern {
218        self.inner.clone()
219    }
220
221    pub fn fill<T: AsRef<BackgroundFill>>(&mut self, fill: T) -> &mut Self {
222        self.inner.fill = fill.as_ref().clone();
223        self
224    }
225
226    pub fn intensity(&mut self, intensity: i32) -> &mut Self {
227        self.inner.intensity = intensity;
228        self
229    }
230
231    pub fn is_inverted(&mut self, is_inverted: bool) -> &mut Self {
232        self.inner.is_inverted = is_inverted;
233        self
234    }
235
236    pub fn is_moving(&mut self, is_moving: bool) -> &mut Self {
237        self.inner.is_moving = is_moving;
238        self
239    }
240}
241
242impl AsRef<BackgroundTypePattern> for BackgroundTypePattern {
243    fn as_ref(&self) -> &BackgroundTypePattern {
244        self
245    }
246}
247
248impl AsRef<BackgroundTypePattern> for BackgroundTypePatternBuilder {
249    fn as_ref(&self) -> &BackgroundTypePattern {
250        &self.inner
251    }
252}
253
254/// A wallpaper in JPEG format
255#[derive(Debug, Clone, Default, Serialize, Deserialize)]
256pub struct BackgroundTypeWallpaper {
257    #[doc(hidden)]
258    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
259    extra: Option<String>,
260    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
261    client_id: Option<i32>,
262    /// True, if the wallpaper must be downscaled to fit in 450x450 square and then box-blurred with radius 12
263
264    #[serde(default)]
265    is_blurred: bool,
266    /// True, if the background needs to be slightly moved when device is tilted
267
268    #[serde(default)]
269    is_moving: bool,
270}
271
272impl RObject for BackgroundTypeWallpaper {
273    #[doc(hidden)]
274    fn extra(&self) -> Option<&str> {
275        self.extra.as_deref()
276    }
277    #[doc(hidden)]
278    fn client_id(&self) -> Option<i32> {
279        self.client_id
280    }
281}
282
283impl TDBackgroundType for BackgroundTypeWallpaper {}
284
285impl BackgroundTypeWallpaper {
286    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
287        Ok(serde_json::from_str(json.as_ref())?)
288    }
289    pub fn builder() -> BackgroundTypeWallpaperBuilder {
290        let mut inner = BackgroundTypeWallpaper::default();
291        inner.extra = Some(Uuid::new_v4().to_string());
292
293        BackgroundTypeWallpaperBuilder { inner }
294    }
295
296    pub fn is_blurred(&self) -> bool {
297        self.is_blurred
298    }
299
300    pub fn is_moving(&self) -> bool {
301        self.is_moving
302    }
303}
304
305#[doc(hidden)]
306pub struct BackgroundTypeWallpaperBuilder {
307    inner: BackgroundTypeWallpaper,
308}
309
310#[deprecated]
311pub type RTDBackgroundTypeWallpaperBuilder = BackgroundTypeWallpaperBuilder;
312
313impl BackgroundTypeWallpaperBuilder {
314    pub fn build(&self) -> BackgroundTypeWallpaper {
315        self.inner.clone()
316    }
317
318    pub fn is_blurred(&mut self, is_blurred: bool) -> &mut Self {
319        self.inner.is_blurred = is_blurred;
320        self
321    }
322
323    pub fn is_moving(&mut self, is_moving: bool) -> &mut Self {
324        self.inner.is_moving = is_moving;
325        self
326    }
327}
328
329impl AsRef<BackgroundTypeWallpaper> for BackgroundTypeWallpaper {
330    fn as_ref(&self) -> &BackgroundTypeWallpaper {
331        self
332    }
333}
334
335impl AsRef<BackgroundTypeWallpaper> for BackgroundTypeWallpaperBuilder {
336    fn as_ref(&self) -> &BackgroundTypeWallpaper {
337        &self.inner
338    }
339}