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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use std::{convert::TryFrom, ops::Deref};

use serde::{Deserialize, Serialize};

use crate::types::{FileMeta, MaskPosition, PhotoSize};

/// This object represents a sticker.
///
/// [The official docs](https://core.telegram.org/bots/api#sticker).
#[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Sticker {
    /// Metadata of the sticker file.
    #[serde(flatten)]
    pub file: FileMeta,

    /// Sticker width, in pixels.
    ///
    /// You can assume that `max(width, height) = 512`, `min(width, height) <=
    /// 512`. In other words one dimension is exactly 512 pixels and the other
    /// is at most 512 pixels.
    pub width: u16,

    /// Sticker height, in pixels.
    ///
    /// You can assume that `max(width, height) = 512`, `min(width, height) <=
    /// 512`. In other words one dimension is exactly 512 pixels and the other
    /// is at most 512 pixels.
    pub height: u16,

    /// Kind of this sticker - regular, mask or custom emoji.
    ///
    /// In other words this represent how the sticker is presented, as a big
    /// picture/video, as a mask while editing pictures or as a custom emoji in
    /// messages.
    #[serde(flatten)]
    pub kind: StickerKind,

    /// Format of this sticker - raster/`.webp`, animated/`.tgs` or
    /// video/`.webm`.
    ///
    /// In other words this represents how the sticker is encoded.
    #[serde(flatten)]
    pub format: StickerFormat,

    /// Sticker thumbnail in the `.webp` or `.jpg` format.
    pub thumb: Option<PhotoSize>,

    /// Emoji associated with the sticker.
    pub emoji: Option<String>,

    /// Name of the sticker set to which the sticker belongs.
    pub set_name: Option<String>,
}

/// Kind of a [`Sticker`] - regular, mask or custom emoji.
///
/// Dataful version of [`StickerType`].
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum StickerKind {
    /// "Normal", raster, animated or video sticker.
    Regular {
        /// Premium animation for the sticker, if the sticker is premium.
        premium_animation: Option<FileMeta>,
    },
    /// Mask sticker.
    Mask {
        /// For mask stickers, the position where the mask should be placed.
        mask_position: MaskPosition,
    },
    /// Custom emoji sticker.
    CustomEmoji {
        /// A unique identifier of the custom emoji.
        // FIXME(waffle): newtype
        custom_emoji_id: String,
    },
}

/// Type of a [`Sticker`] - regular, mask or custom emoji.
///
/// Dataless version of [`StickerType`].
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "sticker_type")]
#[serde(rename_all = "snake_case")]
pub enum StickerType {
    /// "Normal", raster, animated or video sticker.
    Regular,
    /// Mask sticker.
    Mask,
    /// Custom emoji sticker.
    CustomEmoji,
}

/// Format of a [`Sticker`] - regular/webp, animated/tgs or video/webm.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "StickerFormatRaw", into = "StickerFormatRaw")]
pub enum StickerFormat {
    /// "Normal", raster, `.webp` sticker.
    Raster,
    /// [Animated], `.tgs` sticker.
    ///
    /// [Animated]: https://telegram.org/blog/animated-stickers
    Animated,
    /// [Video], `.webm` sticker.
    ///
    /// [Video]: https://telegram.org/blog/video-stickers-better-reactions
    Video,
}

/// This allows calling [`StickerKind`]'s methods directly on [`Sticker`].
///
/// ```no_run
/// use teloxide_core::types::Sticker;
///
/// let sticker: Sticker = todo!();
///
/// let _ = sticker.is_regular();
/// let _ = sticker.kind.is_regular();
///
/// let _ = sticker.mask_position();
/// let _ = sticker.kind.mask_position();
/// ```
impl Deref for Sticker {
    type Target = StickerKind;

    fn deref(&self) -> &Self::Target {
        &self.kind
    }
}

impl Sticker {
    /// Returns `true` is this is a "normal" raster sticker.
    ///
    /// Alias to [`self.format.is_raster()`].
    ///
    /// [`self.format.is_raster()`]: StickerFormat::is_raster
    #[must_use]
    pub fn is_raster(&self) -> bool {
        self.format.is_raster()
    }

    /// Returns `true` is this is an [animated] sticker.
    ///
    /// Alias to [`self.format.is_animated()`].
    ///
    /// [`self.format.is_animated()`]: StickerFormat::is_animated
    /// [animated]: https://telegram.org/blog/animated-stickers
    #[must_use]
    pub fn is_animated(&self) -> bool {
        self.format.is_animated()
    }

    /// Returns `true` is this is a [video] sticker.
    ///
    /// Alias to [`self.format.is_video()`].
    ///
    /// [`self.format.is_video()`]: StickerFormat::is_video
    /// [video]: https://telegram.org/blog/video-stickers-better-reactions
    #[must_use]
    pub fn is_video(&self) -> bool {
        self.format.is_video()
    }
}

impl StickerKind {
    /// Converts [`StickerKind`] to [`StickerType`]
    #[must_use]
    pub fn type_(&self) -> StickerType {
        match self {
            StickerKind::Regular { .. } => StickerType::Regular,
            StickerKind::Mask { .. } => StickerType::Mask,
            StickerKind::CustomEmoji { .. } => StickerType::CustomEmoji,
        }
    }

    /// Returns `true` if the sticker kind is [`Regular`].
    ///
    /// [`Regular`]: StickerKind::Regular
    #[must_use]
    pub fn is_regular(&self) -> bool {
        self.type_().is_regular()
    }

    /// Returns `true` if the sticker kind is [`Mask`].
    ///
    /// [`Mask`]: StickerKind::Mask
    #[must_use]
    pub fn is_mask(&self) -> bool {
        self.type_().is_mask()
    }

    /// Returns `true` if the sticker kind is [`CustomEmoji`].
    ///
    /// [`CustomEmoji`]: StickerKind::CustomEmoji
    #[must_use]
    pub fn is_custom_emoji(&self) -> bool {
        self.type_().is_custom_emoji()
    }

    /// Getter for [`StickerKind::Regular::premium_animation`].
    #[must_use]
    pub fn premium_animation(&self) -> Option<&FileMeta> {
        if let Self::Regular { premium_animation } = self {
            premium_animation.as_ref()
        } else {
            None
        }
    }

    /// Getter for [`StickerKind::Mask::mask_position`].
    #[must_use]
    pub fn mask_position(&self) -> Option<MaskPosition> {
        if let Self::Mask { mask_position } = self {
            Some(*mask_position)
        } else {
            None
        }
    }

    /// Getter for [`StickerKind::CustomEmoji::custom_emoji_id`].
    #[must_use]
    pub fn custom_emoji_id(&self) -> Option<&str> {
        if let Self::CustomEmoji { custom_emoji_id } = self {
            Some(custom_emoji_id)
        } else {
            None
        }
    }
}

impl StickerType {
    /// Returns `true` if the sticker type is [`Regular`].
    ///
    /// [`Regular`]: StickerType::Regular
    #[must_use]
    pub fn is_regular(&self) -> bool {
        matches!(self, Self::Regular)
    }

    /// Returns `true` if the sticker type is [`Mask`].
    ///
    /// [`Mask`]: StickerType::Mask
    #[must_use]
    pub fn is_mask(&self) -> bool {
        matches!(self, Self::Mask)
    }

    /// Returns `true` if the sticker type is [`CustomEmoji`].
    ///
    /// [`CustomEmoji`]: StickerType::CustomEmoji
    #[must_use]
    pub fn is_custom_emoji(&self) -> bool {
        matches!(self, Self::CustomEmoji)
    }
}

impl StickerFormat {
    /// Returns `true` if the sticker format is [`Raster`].
    ///
    /// [`Raster`]: StickerFormat::Raster
    #[must_use]
    pub fn is_raster(&self) -> bool {
        matches!(self, Self::Raster)
    }

    /// Returns `true` if the sticker format is [`Animated`].
    ///
    /// [`Animated`]: StickerFormat::Animated
    #[must_use]
    pub fn is_animated(&self) -> bool {
        matches!(self, Self::Animated)
    }

    /// Returns `true` if the sticker format is [`Video`].
    ///
    /// [`Video`]: StickerFormat::Video
    #[must_use]
    pub fn is_video(&self) -> bool {
        matches!(self, Self::Video)
    }
}

#[derive(Serialize, Deserialize)]
struct StickerFormatRaw {
    is_animated: bool,
    is_video: bool,
}

impl TryFrom<StickerFormatRaw> for StickerFormat {
    type Error = &'static str;

    fn try_from(
        StickerFormatRaw { is_animated, is_video }: StickerFormatRaw,
    ) -> Result<Self, Self::Error> {
        let ret = match (is_animated, is_video) {
            (false, false) => Self::Raster,
            (true, false) => Self::Animated,
            (false, true) => Self::Video,
            (true, true) => return Err("`is_animated` and `is_video` present at the same time"),
        };

        Ok(ret)
    }
}

impl From<StickerFormat> for StickerFormatRaw {
    fn from(kind: StickerFormat) -> Self {
        match kind {
            StickerFormat::Raster => Self { is_animated: false, is_video: false },
            StickerFormat::Animated => Self { is_animated: true, is_video: false },
            StickerFormat::Video => Self { is_animated: false, is_video: true },
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::types::{MaskPoint, Sticker, StickerFormat, StickerType};

    #[test]
    fn mask_serde() {
        // Taken from a real (mask) sticker set
        let json = r#"{
            "width": 512,
            "height": 512,
            "emoji": "🎭",
            "set_name": "Coronamask",
            "is_animated": false,
            "is_video": false,
            "type": "mask",
            "mask_position": {
                "point": "forehead",
                "x_shift": -0.0125,
                "y_shift": 0.5525,
                "scale": 1.94
            },
            "thumb": {
                "file_id": "AAMCAQADFQABYzA0qlYHijpjMzMwBFKnEVE5XdkAAjIKAAK_jJAE1TRw7D936M8BAAdtAAMpBA",
                "file_unique_id": "AQADMgoAAr-MkARy",
                "file_size": 11028,
                "width": 320,
                "height": 320
            },
            "file_id": "CAACAgEAAxUAAWMwNKpWB4o6YzMzMARSpxFROV3ZAAIyCgACv4yQBNU0cOw_d-jPKQQ",
            "file_unique_id": "AgADMgoAAr-MkAQ",
            "file_size": 18290
        }"#;

        let sticker: Sticker = serde_json::from_str(json).unwrap();

        // Assert some basic properties are correctly deserialized
        assert_eq!(sticker.type_(), StickerType::Mask);
        assert_eq!(sticker.mask_position().unwrap().point, MaskPoint::Forehead);
        assert_eq!(sticker.is_animated(), false);
        assert_eq!(sticker.is_video(), false);
        assert_eq!(sticker.thumb.clone().unwrap().file.size, 11028);
        assert_eq!(sticker.file.size, 18290);
        assert_eq!(sticker.width, 512);
        assert_eq!(sticker.height, 512);

        let json2 = serde_json::to_string(&sticker).unwrap();
        let sticker2: Sticker = serde_json::from_str(&json2).unwrap();
        assert_eq!(sticker, sticker2);
    }

    #[test]
    fn regular_serde() {
        // Taken from a real sticker set
        let json = r#"{
            "width": 463,
            "height": 512,
            "emoji": "🍿",
            "set_name": "menhera2",
            "is_animated": false,
            "is_video": false,
            "type": "regular",
            "thumb": {
                "file_id": "AAMCAgADFQABYzBxOJ1GWrttqL7FSRwdAtrq-AkAAtkHAALBGJ4LUUUh5CUew90BAAdtAAMpBA",
                "file_unique_id": "AQAD2QcAAsEYngty",
                "file_size": 4558,
                "width": 116,
                "height": 128
            },
            "file_id": "CAACAgIAAxUAAWMwcTidRlq7bai-xUkcHQLa6vgJAALZBwACwRieC1FFIeQlHsPdKQQ",
            "file_unique_id": "AgAD2QcAAsEYngs",
            "file_size": 25734
        }"#;

        let sticker: Sticker = serde_json::from_str(json).unwrap();

        // Assert some basic properties are correctly deserialized
        assert_eq!(sticker.type_(), StickerType::Regular);
        assert_eq!(sticker.premium_animation(), None);
        assert_eq!(sticker.is_animated(), false);
        assert_eq!(sticker.is_video(), false);
        assert_eq!(sticker.thumb.clone().unwrap().file.size, 4558);
        assert_eq!(sticker.file.size, 25734);
        assert_eq!(sticker.width, 463);
        assert_eq!(sticker.height, 512);
        assert_eq!(sticker.set_name.as_deref(), Some("menhera2"));

        let json2 = serde_json::to_string(&sticker).unwrap();
        let sticker2: Sticker = serde_json::from_str(&json2).unwrap();
        assert_eq!(sticker, sticker2);
    }

    #[test]
    fn sticker_format_serde() {
        {
            let json = r#"{"is_animated":false,"is_video":false}"#;
            let fmt: StickerFormat = serde_json::from_str(json).unwrap();
            assert_eq!(fmt, StickerFormat::Raster);

            let json2 = serde_json::to_string(&fmt).unwrap();
            assert_eq!(json, json2);
        }
        {
            let json = r#"{"is_animated":true,"is_video":false}"#;
            let fmt: StickerFormat = serde_json::from_str(json).unwrap();
            assert_eq!(fmt, StickerFormat::Animated);

            let json2 = serde_json::to_string(&fmt).unwrap();
            assert_eq!(json, json2);
        }
        {
            let json = r#"{"is_animated":false,"is_video":true}"#;
            let fmt: StickerFormat = serde_json::from_str(json).unwrap();
            assert_eq!(fmt, StickerFormat::Video);

            let json2 = serde_json::to_string(&fmt).unwrap();
            assert_eq!(json, json2);
        }
        {
            let json = r#"{"is_animated":true,"is_video":true}"#;
            let fmt: Result<StickerFormat, _> = serde_json::from_str(json);
            assert!(fmt.is_err());
        }
    }
}