telers/types/
inline_query_result.rs

1use crate::types::{
2    InlineQueryResultArticle, InlineQueryResultAudio, InlineQueryResultCachedAudio,
3    InlineQueryResultCachedDocument, InlineQueryResultCachedGif, InlineQueryResultCachedMpeg4Gif,
4    InlineQueryResultCachedPhoto, InlineQueryResultCachedSticker, InlineQueryResultCachedVideo,
5    InlineQueryResultCachedVoice, InlineQueryResultContact, InlineQueryResultDocument,
6    InlineQueryResultGame, InlineQueryResultGif, InlineQueryResultLocation,
7    InlineQueryResultMpeg4Gif, InlineQueryResultPhoto, InlineQueryResultVenue,
8    InlineQueryResultVideo, InlineQueryResultVoice,
9};
10
11use serde::{Deserialize, Serialize};
12
13/// This object represents one result of an inline query. Telegram clients currently support results of the following 20 types:
14/// - [`InlineQueryResultCachedAudio`]
15/// - [`InlineQueryResultCachedDocument`]
16/// - [`InlineQueryResultCachedGif`]
17/// - [`InlineQueryResultCachedMpeg4Gif`]
18/// - [`InlineQueryResultCachedPhoto`]
19/// - [`InlineQueryResultCachedSticker`]
20/// - [`InlineQueryResultCachedVideo`]
21/// - [`InlineQueryResultCachedVoice`]
22/// - [`InlineQueryResultArticle`]
23/// - [`InlineQueryResultPhoto`]
24/// - [`InlineQueryResultGif`]
25/// - [`InlineQueryResultMpeg4Gif`]
26/// - [`InlineQueryResultVideo`]
27/// - [`InlineQueryResultAudio`]
28/// - [`InlineQueryResultVoice`]
29/// - [`InlineQueryResultDocument`]
30/// - [`InlineQueryResultLocation`]
31/// - [`InlineQueryResultVenue`]
32/// - [`InlineQueryResultContact`]
33/// - [`InlineQueryResultGame`]
34///
35/// All URLs passed in inline query results will be available to end users and therefore must be assumed to be **public**.
36/// # Documentation
37/// <https://core.telegram.org/bots/api#inlinequeryresult>
38#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
39#[serde(
40    tag = "type",
41    rename_all = "snake_case",
42    from = "raw::InlineQueryResult",
43    into = "raw::InlineQueryResult"
44)]
45pub enum InlineQueryResult {
46    #[serde(rename = "audio")]
47    CachedAudio(InlineQueryResultCachedAudio),
48    #[serde(rename = "document")]
49    CachedDocument(InlineQueryResultCachedDocument),
50    #[serde(rename = "gif")]
51    CachedGif(InlineQueryResultCachedGif),
52    #[serde(rename = "mpeg4_gif")]
53    CachedMpeg4Gif(InlineQueryResultCachedMpeg4Gif),
54    #[serde(rename = "photo")]
55    CachedPhoto(InlineQueryResultCachedPhoto),
56    #[serde(rename = "sticker")]
57    CachedSticker(InlineQueryResultCachedSticker),
58    #[serde(rename = "video")]
59    CachedVideo(InlineQueryResultCachedVideo),
60    #[serde(rename = "voice")]
61    CachedVoice(InlineQueryResultCachedVoice),
62    Article(InlineQueryResultArticle),
63    Audio(InlineQueryResultAudio),
64    Contact(InlineQueryResultContact),
65    Game(InlineQueryResultGame),
66    Document(InlineQueryResultDocument),
67    Gif(InlineQueryResultGif),
68    Venue(InlineQueryResultVenue),
69    Location(InlineQueryResultLocation),
70    #[serde(rename = "mpeg4_gif")]
71    Mpeg4Gif(InlineQueryResultMpeg4Gif),
72    Photo(InlineQueryResultPhoto),
73    Video(InlineQueryResultVideo),
74    Voice(InlineQueryResultVoice),
75}
76
77impl From<InlineQueryResultCachedAudio> for InlineQueryResult {
78    fn from(result: InlineQueryResultCachedAudio) -> Self {
79        InlineQueryResult::CachedAudio(result)
80    }
81}
82
83impl From<InlineQueryResultCachedDocument> for InlineQueryResult {
84    fn from(result: InlineQueryResultCachedDocument) -> Self {
85        InlineQueryResult::CachedDocument(result)
86    }
87}
88
89impl From<InlineQueryResultCachedGif> for InlineQueryResult {
90    fn from(result: InlineQueryResultCachedGif) -> Self {
91        InlineQueryResult::CachedGif(result)
92    }
93}
94
95impl From<InlineQueryResultCachedMpeg4Gif> for InlineQueryResult {
96    fn from(result: InlineQueryResultCachedMpeg4Gif) -> Self {
97        InlineQueryResult::CachedMpeg4Gif(result)
98    }
99}
100
101impl From<InlineQueryResultCachedPhoto> for InlineQueryResult {
102    fn from(result: InlineQueryResultCachedPhoto) -> Self {
103        InlineQueryResult::CachedPhoto(result)
104    }
105}
106
107impl From<InlineQueryResultCachedSticker> for InlineQueryResult {
108    fn from(result: InlineQueryResultCachedSticker) -> Self {
109        InlineQueryResult::CachedSticker(result)
110    }
111}
112
113impl From<InlineQueryResultCachedVideo> for InlineQueryResult {
114    fn from(result: InlineQueryResultCachedVideo) -> Self {
115        InlineQueryResult::CachedVideo(result)
116    }
117}
118
119impl From<InlineQueryResultCachedVoice> for InlineQueryResult {
120    fn from(result: InlineQueryResultCachedVoice) -> Self {
121        InlineQueryResult::CachedVoice(result)
122    }
123}
124
125impl From<InlineQueryResultArticle> for InlineQueryResult {
126    fn from(result: InlineQueryResultArticle) -> Self {
127        InlineQueryResult::Article(result)
128    }
129}
130
131impl From<InlineQueryResultAudio> for InlineQueryResult {
132    fn from(result: InlineQueryResultAudio) -> Self {
133        InlineQueryResult::Audio(result)
134    }
135}
136
137impl From<InlineQueryResultContact> for InlineQueryResult {
138    fn from(result: InlineQueryResultContact) -> Self {
139        InlineQueryResult::Contact(result)
140    }
141}
142
143impl From<InlineQueryResultGame> for InlineQueryResult {
144    fn from(result: InlineQueryResultGame) -> Self {
145        InlineQueryResult::Game(result)
146    }
147}
148
149impl From<InlineQueryResultDocument> for InlineQueryResult {
150    fn from(result: InlineQueryResultDocument) -> Self {
151        InlineQueryResult::Document(result)
152    }
153}
154
155impl From<InlineQueryResultGif> for InlineQueryResult {
156    fn from(result: InlineQueryResultGif) -> Self {
157        InlineQueryResult::Gif(result)
158    }
159}
160
161impl From<InlineQueryResultLocation> for InlineQueryResult {
162    fn from(result: InlineQueryResultLocation) -> Self {
163        InlineQueryResult::Location(result)
164    }
165}
166
167impl From<InlineQueryResultMpeg4Gif> for InlineQueryResult {
168    fn from(result: InlineQueryResultMpeg4Gif) -> Self {
169        InlineQueryResult::Mpeg4Gif(result)
170    }
171}
172
173impl From<InlineQueryResultPhoto> for InlineQueryResult {
174    fn from(result: InlineQueryResultPhoto) -> Self {
175        InlineQueryResult::Photo(result)
176    }
177}
178
179impl From<InlineQueryResultVenue> for InlineQueryResult {
180    fn from(result: InlineQueryResultVenue) -> Self {
181        InlineQueryResult::Venue(result)
182    }
183}
184
185impl From<InlineQueryResultVideo> for InlineQueryResult {
186    fn from(result: InlineQueryResultVideo) -> Self {
187        InlineQueryResult::Video(result)
188    }
189}
190
191impl From<InlineQueryResultVoice> for InlineQueryResult {
192    fn from(result: InlineQueryResultVoice) -> Self {
193        InlineQueryResult::Voice(result)
194    }
195}
196
197mod raw {
198    use super::{
199        Deserialize, InlineQueryResultArticle, InlineQueryResultAudio,
200        InlineQueryResultCachedAudio, InlineQueryResultCachedDocument, InlineQueryResultCachedGif,
201        InlineQueryResultCachedMpeg4Gif, InlineQueryResultCachedPhoto,
202        InlineQueryResultCachedSticker, InlineQueryResultCachedVideo, InlineQueryResultCachedVoice,
203        InlineQueryResultContact, InlineQueryResultDocument, InlineQueryResultGame,
204        InlineQueryResultGif, InlineQueryResultLocation, InlineQueryResultMpeg4Gif,
205        InlineQueryResultPhoto, InlineQueryResultVenue, InlineQueryResultVideo,
206        InlineQueryResultVoice, Serialize,
207    };
208
209    #[derive(Serialize, Deserialize)]
210    #[serde(untagged)]
211    pub(super) enum AudioKind {
212        Cached(InlineQueryResultCachedAudio),
213        NonCached(InlineQueryResultAudio),
214    }
215
216    #[derive(Serialize, Deserialize)]
217    #[serde(untagged)]
218    pub(super) enum DocumentKind {
219        Cached(InlineQueryResultCachedDocument),
220        NonCached(InlineQueryResultDocument),
221    }
222
223    #[derive(Serialize, Deserialize)]
224    #[serde(untagged)]
225    pub(super) enum GifKind {
226        Cached(InlineQueryResultCachedGif),
227        NonCached(InlineQueryResultGif),
228    }
229
230    #[derive(Serialize, Deserialize)]
231    #[serde(untagged)]
232    pub(super) enum Mpeg4GifKind {
233        Cached(InlineQueryResultCachedMpeg4Gif),
234        NonCached(InlineQueryResultMpeg4Gif),
235    }
236
237    #[derive(Serialize, Deserialize)]
238    #[serde(untagged)]
239    pub(super) enum PhotoKind {
240        Cached(InlineQueryResultCachedPhoto),
241        NonCached(InlineQueryResultPhoto),
242    }
243
244    #[derive(Serialize, Deserialize)]
245    #[serde(untagged)]
246    pub(super) enum VideoKind {
247        Cached(InlineQueryResultCachedVideo),
248        NonCached(InlineQueryResultVideo),
249    }
250
251    #[derive(Serialize, Deserialize)]
252    #[serde(untagged)]
253    pub(super) enum VoiceKind {
254        Cached(InlineQueryResultCachedVoice),
255        NonCached(InlineQueryResultVoice),
256    }
257
258    #[derive(Serialize, Deserialize)]
259    #[serde(tag = "type", rename_all = "snake_case")]
260    pub(super) enum InlineQueryResult {
261        // Types which have a cached and non-cached variant must be listed here
262        Audio(AudioKind),
263        Document(DocumentKind),
264        Gif(GifKind),
265        #[serde(rename = "mpeg4_gif")]
266        Mpeg4Gif(Mpeg4GifKind),
267        Photo(PhotoKind),
268        Video(VideoKind),
269        Voice(VoiceKind),
270
271        // Types which have only a cached variant must be listed here
272        #[serde(rename = "sticker")]
273        CachedSticker(InlineQueryResultCachedSticker),
274
275        // Types which have only a non-cached variant must be listed here
276        Article(InlineQueryResultArticle),
277        Contact(InlineQueryResultContact),
278        Game(InlineQueryResultGame),
279        Location(InlineQueryResultLocation),
280        Venue(InlineQueryResultVenue),
281    }
282
283    impl From<InlineQueryResult> for super::InlineQueryResult {
284        fn from(raw: InlineQueryResult) -> Self {
285            match raw {
286                InlineQueryResult::Audio(AudioKind::Cached(audio)) => {
287                    super::InlineQueryResult::CachedAudio(audio)
288                }
289                InlineQueryResult::Audio(AudioKind::NonCached(audio)) => {
290                    super::InlineQueryResult::Audio(audio)
291                }
292                InlineQueryResult::Document(DocumentKind::Cached(document)) => {
293                    super::InlineQueryResult::CachedDocument(document)
294                }
295                InlineQueryResult::Document(DocumentKind::NonCached(document)) => {
296                    super::InlineQueryResult::Document(document)
297                }
298                InlineQueryResult::Gif(GifKind::Cached(gif)) => {
299                    super::InlineQueryResult::CachedGif(gif)
300                }
301                InlineQueryResult::Gif(GifKind::NonCached(gif)) => {
302                    super::InlineQueryResult::Gif(gif)
303                }
304                InlineQueryResult::Mpeg4Gif(Mpeg4GifKind::Cached(gif)) => {
305                    super::InlineQueryResult::CachedMpeg4Gif(gif)
306                }
307                InlineQueryResult::Mpeg4Gif(Mpeg4GifKind::NonCached(gif)) => {
308                    super::InlineQueryResult::Mpeg4Gif(gif)
309                }
310                InlineQueryResult::Photo(PhotoKind::Cached(photo)) => {
311                    super::InlineQueryResult::CachedPhoto(photo)
312                }
313                InlineQueryResult::Photo(PhotoKind::NonCached(photo)) => {
314                    super::InlineQueryResult::Photo(photo)
315                }
316                InlineQueryResult::Video(VideoKind::Cached(video)) => {
317                    super::InlineQueryResult::CachedVideo(video)
318                }
319                InlineQueryResult::Video(VideoKind::NonCached(video)) => {
320                    super::InlineQueryResult::Video(video)
321                }
322                InlineQueryResult::Voice(VoiceKind::Cached(voice)) => {
323                    super::InlineQueryResult::CachedVoice(voice)
324                }
325                InlineQueryResult::Voice(VoiceKind::NonCached(voice)) => {
326                    super::InlineQueryResult::Voice(voice)
327                }
328
329                InlineQueryResult::CachedSticker(sticker) => {
330                    super::InlineQueryResult::CachedSticker(sticker)
331                }
332
333                InlineQueryResult::Article(article) => super::InlineQueryResult::Article(article),
334                InlineQueryResult::Contact(contact) => super::InlineQueryResult::Contact(contact),
335                InlineQueryResult::Game(game) => super::InlineQueryResult::Game(game),
336                InlineQueryResult::Location(location) => {
337                    super::InlineQueryResult::Location(location)
338                }
339                InlineQueryResult::Venue(venue) => super::InlineQueryResult::Venue(venue),
340            }
341        }
342    }
343
344    impl From<super::InlineQueryResult> for InlineQueryResult {
345        fn from(raw: super::InlineQueryResult) -> Self {
346            match raw {
347                super::InlineQueryResult::CachedAudio(audio) => {
348                    InlineQueryResult::Audio(AudioKind::Cached(audio))
349                }
350                super::InlineQueryResult::Audio(audio) => {
351                    InlineQueryResult::Audio(AudioKind::NonCached(audio))
352                }
353                super::InlineQueryResult::CachedDocument(document) => {
354                    InlineQueryResult::Document(DocumentKind::Cached(document))
355                }
356                super::InlineQueryResult::Document(document) => {
357                    InlineQueryResult::Document(DocumentKind::NonCached(document))
358                }
359                super::InlineQueryResult::CachedGif(gif) => {
360                    InlineQueryResult::Gif(GifKind::Cached(gif))
361                }
362                super::InlineQueryResult::Gif(gif) => {
363                    InlineQueryResult::Gif(GifKind::NonCached(gif))
364                }
365                super::InlineQueryResult::CachedMpeg4Gif(gif) => {
366                    InlineQueryResult::Mpeg4Gif(Mpeg4GifKind::Cached(gif))
367                }
368                super::InlineQueryResult::Mpeg4Gif(gif) => {
369                    InlineQueryResult::Mpeg4Gif(Mpeg4GifKind::NonCached(gif))
370                }
371                super::InlineQueryResult::CachedPhoto(photo) => {
372                    InlineQueryResult::Photo(PhotoKind::Cached(photo))
373                }
374                super::InlineQueryResult::Photo(photo) => {
375                    InlineQueryResult::Photo(PhotoKind::NonCached(photo))
376                }
377                super::InlineQueryResult::CachedVideo(video) => {
378                    InlineQueryResult::Video(VideoKind::Cached(video))
379                }
380                super::InlineQueryResult::Video(video) => {
381                    InlineQueryResult::Video(VideoKind::NonCached(video))
382                }
383                super::InlineQueryResult::CachedVoice(voice) => {
384                    InlineQueryResult::Voice(VoiceKind::Cached(voice))
385                }
386                super::InlineQueryResult::Voice(voice) => {
387                    InlineQueryResult::Voice(VoiceKind::NonCached(voice))
388                }
389
390                super::InlineQueryResult::CachedSticker(sticker) => {
391                    InlineQueryResult::CachedSticker(sticker)
392                }
393
394                super::InlineQueryResult::Article(article) => InlineQueryResult::Article(article),
395                super::InlineQueryResult::Contact(contact) => InlineQueryResult::Contact(contact),
396                super::InlineQueryResult::Game(game) => InlineQueryResult::Game(game),
397                super::InlineQueryResult::Location(location) => {
398                    InlineQueryResult::Location(location)
399                }
400                super::InlineQueryResult::Venue(venue) => InlineQueryResult::Venue(venue),
401            }
402        }
403    }
404}
405
406#[cfg(test)]
407mod tests {
408    use super::*;
409
410    #[test]
411    fn serialize_inline_query_result_cached_audio() {
412        let data =
413            InlineQueryResult::CachedAudio(InlineQueryResultCachedAudio::new("test", "test"));
414
415        let json = serde_json::to_string(&data).unwrap();
416
417        assert_eq!(
418            json,
419            r#"{"type":"audio","id":"test","audio_file_id":"test"}"#
420        );
421    }
422
423    #[test]
424    fn serialize_inline_query_result_audio() {
425        let data = InlineQueryResult::Audio(InlineQueryResultAudio::new("test", "test", "test"));
426
427        let json = serde_json::to_string(&data).unwrap();
428
429        assert_eq!(
430            json,
431            r#"{"type":"audio","id":"test","audio_url":"test","title":"test"}"#
432        );
433    }
434}