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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use super::*;

/// Represents contents of an .attheme file.
#[derive(Debug, PartialEq, Clone)]
pub struct Attheme {
  /// An `IndexMap` of variables of the theme.
  pub variables: Variables,
  /// The image wallpaper of the theme.
  ///
  /// Note that Telegram only recognizes `.jpg` images, but the crate doesn't
  /// check that the wallpaper is actually a valid `.jpg`. You should do it on
  /// your own.
  pub wallpaper: Option<Wallpaper>,
}

impl Attheme {
  /// Creates an empty theme.
  ///
  /// # Examples
  ///
  /// ```
  /// use attheme::Attheme;
  ///
  /// let theme = Attheme::new();
  ///
  /// assert!(theme.variables.is_empty());
  /// assert_eq!(theme.wallpaper, None);
  /// ```
  #[must_use]
  pub fn new() -> Attheme {
    Attheme {
      variables: IndexMap::new(),
      wallpaper: None,
    }
  }

  /// Creates an empty theme, with preallocation for `capacity` variables.
  #[must_use]
  pub fn with_capacity(capacity: usize) -> Attheme {
    Attheme {
      variables: IndexMap::with_capacity(capacity),
      wallpaper: None,
    }
  }

  /// Parses .attheme contents passed as bytes.
  ///
  /// # Examples
  ///
  /// ```
  /// use attheme::Attheme;
  ///
  /// let contents = b"
  /// checkbox=-1
  /// checkboxCheck=#123456
  /// divider=#40302010
  ///
  /// WPS
  /// Pretend it's Mona Lisa here
  /// WPE
  /// ";
  /// let theme = Attheme::from_bytes(contents);
  ///
  /// assert_eq!(theme.variables["checkbox"], [0xff; 4]);
  /// assert_eq!(theme.variables["checkboxCheck"], [0x12, 0x34, 0x56, 0xff]);
  /// assert_eq!(theme.variables["divider"], [0x30, 0x20, 0x10, 0x40]);
  /// assert_eq!(
  ///   theme.wallpaper,
  ///   Some(b"Pretend it's Mona Lisa here".to_vec()),
  /// );
  /// ```
  ///
  /// # Notes
  ///
  /// If Telegram can't parse something, it will simply ignore it. This crate
  /// resembles this behavior.
  ///
  /// Though Telegram only recognizes `.jpg` images, the crate does not check
  /// if the image is a valid `.jpg`. You should do it on your own.
  #[must_use]
  pub fn from_bytes(contents: &[u8]) -> Attheme {
    parser::from_bytes(contents)
  }

  /// Serializes the theme.
  ///
  /// # Examples
  /// ```
  /// use attheme::*;
  ///
  /// let mut theme = Attheme::new();
  ///
  /// theme.variables.insert("divider".to_string(), [1, 2, 3, 4]);
  /// theme.variables.insert("checkbox".to_string(), [0xff; 4]);
  ///
  /// let expected_contents = b"divider=67174915
  /// checkbox=-1
  /// ".to_vec();
  ///
  /// assert_eq!(theme.to_bytes(ColorSignature::Int), expected_contents);
  ///
  /// theme.wallpaper = Some(b"Pretend it's Mona Lisa here".to_vec());
  ///
  /// let expected_contents = b"divider=#04010203
  /// checkbox=#ffffffff
  ///
  /// WPS
  /// Pretend it's Mona Lisa here
  /// WPE
  /// ".to_vec();
  ///
  /// assert_eq!(theme.to_bytes(ColorSignature::Hex), expected_contents);
  /// ```
  #[must_use]
  pub fn to_bytes(&self, color_signature: ColorSignature) -> Vec<u8> {
    serializer::theme_to_bytes(
      &self.variables,
      &self.wallpaper,
      color_signature,
    )
  }
}

/// Adds all variables from `other` to `self`.
///
/// # Example
///
/// ```
/// use attheme::Attheme;
///
/// let mut first_theme = Attheme::new();
/// let mut second_theme = Attheme::new();
/// let wallpaper = b"Just pretending".to_vec();
///
/// first_theme.variables.insert("checkbox".to_string(), [0xff; 4]);
/// first_theme.wallpaper = Some(wallpaper.clone());
/// second_theme.variables.insert("checkbox".to_string(), [0x80; 4]);
/// second_theme.variables.insert("divider".to_string(), [0x40; 4]);
///
/// let third_theme = first_theme + second_theme;
///
/// assert_eq!(third_theme.variables["checkbox"], [0x80; 4]);
/// assert_eq!(third_theme.variables["divider"], [0x40; 4]);
/// assert_eq!(third_theme.wallpaper, Some(wallpaper));
/// ```
///
/// # Notes
///
/// If `other.wallpaper` is `None`, it will fallback to `self.wallpaper`.
impl std::ops::Add for Attheme {
  type Output = Attheme;

  fn add(self, other: Attheme) -> Attheme {
    let mut merged_theme = Attheme::new();

    for (variable, color) in other.variables {
      merged_theme.variables.insert(variable, color);
    }

    for (variable, color) in self.variables {
      merged_theme.variables.entry(variable).or_insert(color);
    }

    if let Some(wallpaper) = other.wallpaper {
      merged_theme.wallpaper = Some(wallpaper);
    } else if let Some(wallpaper) = self.wallpaper {
      merged_theme.wallpaper = Some(wallpaper);
    }

    merged_theme
  }
}

/// Adds all variables from `other` to `self`.
///
/// # Notes
///
/// If `other.wallpaper` is `None`, it will fallback to `self.wallpaper`.
impl<'a> std::ops::Add for &'a Attheme {
  type Output = Attheme;

  fn add(self, other: &Attheme) -> Attheme {
    let mut merged_theme = Attheme::with_capacity(other.variables.len());

    for (variable, color) in &other.variables {
      merged_theme.variables.insert(variable.clone(), color.clone());
    }

    for (variable, color) in &self.variables {
      merged_theme.variables.entry(variable.clone()).or_insert(color.clone());
    }

    if let Some(wallpaper) = &other.wallpaper {
      merged_theme.wallpaper = Some(wallpaper.clone());
    } else if let Some(wallpaper) = &self.wallpaper {
      merged_theme.wallpaper = Some(wallpaper.clone());
    }

    merged_theme
  }
}

/// Adds all variables from the `other` theme to `self`.
///
/// # Example
///
/// ```
/// use attheme::Attheme;
///
/// let mut first_theme = Attheme::new();
/// let mut second_theme = Attheme::new();
/// let wallpaper = b"Just pretending".to_vec();
///
/// first_theme.variables.insert("checkbox".to_string(), [0xff; 4]);
/// first_theme.wallpaper = Some(wallpaper.clone());
/// second_theme.variables.insert("checkbox".to_string(), [0x80; 4]);
/// second_theme.variables.insert("divider".to_string(), [0x40; 4]);
///
/// first_theme += second_theme;
///
/// assert_eq!(first_theme.variables["checkbox"], [0x80; 4]);
/// assert_eq!(first_theme.variables["divider"], [0x40; 4]);
/// assert_eq!(first_theme.wallpaper, Some(wallpaper));
/// ```
///
/// # Notes
///
/// If `other.wallpaper` is `None`, the wallpaper won't be removed.
impl std::ops::AddAssign for Attheme {
  fn add_assign(&mut self, other: Attheme) {
    for (variable, color) in other.variables {
      self.variables.insert(variable, color);
    }

    if let Some(wallpaper) = other.wallpaper {
      self.wallpaper = Some(wallpaper);
    }
  }
}

/// Adds all variables from the `&other` theme to `self`.
///
/// # Example
///
/// ```
/// use attheme::Attheme;
///
/// let mut first_theme = Attheme::new();
/// let mut second_theme = Attheme::new();
/// let wallpaper = b"Just pretending".to_vec();
///
/// first_theme.variables.insert("checkbox".to_string(), [0xff; 4]);
/// first_theme.wallpaper = Some(wallpaper.clone());
/// second_theme.variables.insert("checkbox".to_string(), [0x80; 4]);
/// second_theme.variables.insert("divider".to_string(), [0x40; 4]);
///
/// first_theme += &second_theme;
///
/// assert_eq!(
///   first_theme.variables["checkbox"],
///   second_theme.variables["checkbox"],
/// );
/// assert_eq!(
///   first_theme.variables["divider"],
///   second_theme.variables["divider"],
/// );
/// assert_eq!(first_theme.wallpaper, Some(wallpaper));
/// ```
///
/// # Notes
///
/// If `other.wallpaper` is `None`, the wallpaper won't be removed.
impl<'a> std::ops::AddAssign<&'a Attheme> for Attheme {
  fn add_assign(&mut self, other: &Attheme) {
    for (variable, color) in &other.variables {
      self.variables.insert(variable.clone(), color.clone());
    }

    if let Some(wallpaper) = &other.wallpaper {
      self.wallpaper = Some(wallpaper.clone());
    }
  }
}

/// Adds variables from `other` if absent in `self`.
///
/// Note that in this case, this works exactly as `other + self`.
impl std::ops::BitOr for Attheme {
  type Output = Attheme;

  fn bitor(self, other: Attheme) -> Attheme {
    other + self
  }
}

/// Adds variables from `&other` if absent in `&self`.
///
/// Note that in this case, this works exactly as `&other + &self`.
impl<'a> std::ops::BitOr for &'a Attheme {
  type Output = Attheme;

  fn bitor(self, other: &Attheme) -> Attheme {
    other + self
  }
}

/// Adds variables from `other` that are absent in `self`.
///
/// # Example
///
/// ```
/// use attheme::Attheme;
///
/// let mut first_theme = Attheme::new();
/// let mut second_theme = Attheme::new();
/// let wallpaper = b"Just pretending".to_vec();
///
/// first_theme.variables.insert("checkbox".to_string(), [0xff; 4]);
/// first_theme.wallpaper = Some(wallpaper.clone());
/// second_theme.variables.insert("checkbox".to_string(), [0x80; 4]);
/// second_theme.variables.insert("divider".to_string(), [0x40; 4]);
///
/// first_theme |= second_theme;
///
/// assert_eq!(first_theme.variables["checkbox"], [0xff; 4]);
/// assert_eq!(first_theme.variables["divider"], [0x40; 4]);
/// assert_eq!(first_theme.wallpaper, Some(wallpaper));
/// ```
impl std::ops::BitOrAssign for Attheme {
  fn bitor_assign(&mut self, other: Attheme) {
    for (variable, color) in other.variables {
      self.variables.entry(variable).or_insert(color);
    }

    if let None = self.wallpaper {
      self.wallpaper = other.wallpaper;
    }
  }
}

/// Similar to `theme |= other`, but clones only when neccesary.
///
/// # Example
///
/// ```
/// use attheme::Attheme;
///
/// let mut first_theme = Attheme::new();
/// let mut second_theme = Attheme::new();
/// let wallpaper = b"Just pretending".to_vec();
///
/// first_theme.variables.insert("checkbox".to_string(), [0xff; 4]);
/// first_theme.wallpaper = Some(wallpaper.clone());
/// second_theme.variables.insert("checkbox".to_string(), [0x80; 4]);
/// second_theme.variables.insert("divider".to_string(), [0x40; 4]);
///
/// first_theme |= &second_theme;
///
/// assert_eq!(first_theme.variables["checkbox"], [0xff; 4]);
/// assert_eq!(
///   first_theme.variables["divider"],
///   second_theme.variables["divider"],
/// );
/// assert_eq!(first_theme.wallpaper, Some(wallpaper));
/// ```
impl<'a> std::ops::BitOrAssign<&'a Attheme> for Attheme {
  fn bitor_assign(&mut self, other: &'a Attheme) {
    for (variable, color) in &other.variables {
      if !self.variables.contains_key(variable) {
        self.variables.insert(variable.clone(), color.clone());
      }
    }

    if let None = self.wallpaper {
      self.wallpaper = other.wallpaper.clone();
    }
  }
}