tg_flows/types/
sticker_set.rs

1use std::ops::Deref;
2
3use serde::{Deserialize, Serialize};
4
5use crate::types::{PhotoSize, Sticker, StickerFormat, StickerType};
6
7/// This object represents a sticker set.
8///
9/// [The official docs](https://core.telegram.org/bots/api#stickerset).
10#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
11pub struct StickerSet {
12    /// Sticker set name.
13    pub name: String,
14
15    /// Sticker set title.
16    pub title: String,
17
18    /// Sticker type shared by all stickers in this set.
19    #[serde(flatten)]
20    pub kind: StickerType,
21
22    /// Sticker format shared by all stickers in this set.
23    #[serde(flatten)]
24    pub format: StickerFormat,
25
26    /// List of all set stickers.
27    pub stickers: Vec<Sticker>,
28
29    /// Sticker set thumbnail in the `.webp`, `.tgs` or `.webm` format.
30    pub thumb: Option<PhotoSize>,
31}
32
33/// This allows calling [`StickerType`]'s methods directly on [`StickerSet`].
34///
35/// ```no_run
36/// use teloxide_core::types::StickerSet;
37///
38/// let sticker: StickerSet = todo!();
39///
40/// let _ = sticker.is_mask();
41/// let _ = sticker.kind.is_mask();
42/// ```
43impl Deref for StickerSet {
44    type Target = StickerType;
45
46    fn deref(&self) -> &Self::Target {
47        &self.kind
48    }
49}
50
51impl StickerSet {
52    /// Returns `true` is this is a "normal" raster sticker.
53    ///
54    /// Alias to [`self.format.is_raster()`].
55    ///
56    /// [`self.format.is_raster()`]: StickerFormat::is_raster
57    #[must_use]
58    pub fn is_raster(&self) -> bool {
59        self.format.is_raster()
60    }
61
62    /// Returns `true` is this is an [animated] sticker.
63    ///
64    /// Alias to [`self.format.is_animated()`].
65    ///
66    /// [`self.format.is_animated()`]: StickerFormat::is_animated
67    /// [animated]: https://telegram.org/blog/animated-stickers
68    #[must_use]
69    pub fn is_animated(&self) -> bool {
70        self.format.is_animated()
71    }
72
73    /// Returns `true` is this is a [video] sticker.
74    ///
75    /// Alias to [`self.format.is_video()`].
76    ///
77    /// [`self.format.is_video()`]: StickerFormat::is_video
78    /// [video]: https://telegram.org/blog/video-stickers-better-reactions
79    #[must_use]
80    pub fn is_video(&self) -> bool {
81        self.format.is_video()
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use crate::types::StickerSet;
88
89    #[test]
90    fn smoke_serde() {
91        // https://t.me/addstickers/teloxide_test
92        let json = r#"{
93            "name": "teloxide_test",
94            "title": "teloxide-test",
95            "is_animated": false,
96            "is_video": false,
97            "sticker_type": "regular",
98            "contains_masks": false,
99            "stickers": [
100                {
101                    "width": 512,
102                    "height": 512,
103                    "emoji": "⚙️",
104                    "set_name": "teloxide_test",
105                    "is_animated": false,
106                    "is_video": false,
107                    "type": "regular",
108                    "thumb": {
109                        "file_id": "AAMCAQADFQABYzB4ATH0sqXx351gZ5GpY1Z3Tl8AAlgCAAJ1t4hFbxNCoAg1-akBAAdtAAMpBA",
110                        "file_unique_id": "AQADWAIAAnW3iEVy",
111                        "file_size": 7698,
112                        "width": 320,
113                        "height": 320
114                    },
115                    "file_id": "CAACAgEAAxUAAWMweAEx9LKl8d-dYGeRqWNWd05fAAJYAgACdbeIRW8TQqAINfmpKQQ",
116                    "file_unique_id": "AgADWAIAAnW3iEU",
117                    "file_size": 12266
118                },
119                {
120                    "width": 512,
121                    "height": 512,
122                    "emoji": "⚙️",
123                    "set_name": "teloxide_test",
124                    "is_animated": false,
125                    "is_video": false,
126                    "type": "regular",
127                    "thumb": {
128                        "file_id": "AAMCAQADFQABYzB4AcABR8-MuvGagis9Pk6liSAAAs8DAAL2YYBFNbvduoN1p7oBAAdtAAMpBA",
129                        "file_unique_id": "AQADzwMAAvZhgEVy",
130                        "file_size": 7780,
131                        "width": 320,
132                        "height": 320
133                    },
134                    "file_id": "CAACAgEAAxUAAWMweAHAAUfPjLrxmoIrPT5OpYkgAALPAwAC9mGARTW73bqDdae6KQQ",
135                    "file_unique_id": "AgADzwMAAvZhgEU",
136                    "file_size": 12158
137                }
138            ]
139        }"#;
140
141        let set: StickerSet = serde_json::from_str(json).unwrap();
142
143        assert!(set.is_raster());
144        assert!(set.is_regular());
145        assert!(set.thumb.is_none());
146        assert_eq!(set.stickers.len(), 2);
147    }
148}