tg_flows/types/
animation.rs

1use mime::Mime;
2use serde::{Deserialize, Serialize};
3
4use crate::types::{FileMeta, PhotoSize};
5
6/// This object represents an animation file (GIF or H.264/MPEG-4 AVC video
7/// without sound).
8///
9/// [The official docs](https://core.telegram.org/bots/api#animation).
10#[serde_with_macros::skip_serializing_none]
11#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
12pub struct Animation {
13    /// Metadata of the animation file.
14    #[serde(flatten)]
15    pub file: FileMeta,
16
17    /// A video width as defined by a sender.
18    pub width: u32,
19
20    /// A video height as defined by a sender.
21    pub height: u32,
22
23    /// A duration of the video in seconds as defined by a sender.
24    pub duration: u32,
25
26    /// An animation thumbnail as defined by a sender.
27    pub thumb: Option<PhotoSize>,
28
29    /// An original animation filename as defined by a sender.
30    pub file_name: Option<String>,
31
32    /// A MIME type of the file as defined by a sender.
33    #[serde(with = "crate::types::non_telegram_types::mime::opt_deser")]
34    pub mime_type: Option<Mime>,
35}
36
37#[cfg(test)]
38mod tests {
39    use crate::types::FileMeta;
40
41    use super::*;
42
43    #[test]
44    fn deserialize() {
45        let json = r#"{
46        "file_id":"id",
47        "file_unique_id":"",
48        "width":320,
49        "height":320,
50        "duration":59,
51        "thumb":{
52            "file_id":"id",
53            "file_unique_id":"",
54            "width":320,
55            "height":320,
56            "file_size":3452
57        },
58        "file_name":"some",
59        "mime_type":"video/gif",
60        "file_size":6500}"#;
61        let expected = Animation {
62            file: FileMeta {
63                id: "id".to_string(),
64                unique_id: "".to_string(),
65                size: 6500,
66            },
67            width: 320,
68            height: 320,
69            duration: 59,
70            thumb: Some(PhotoSize {
71                file: FileMeta {
72                    id: "id".to_owned(),
73                    unique_id: "".to_owned(),
74                    size: 3452,
75                },
76                width: 320,
77                height: 320,
78            }),
79            file_name: Some("some".to_string()),
80            mime_type: Some("video/gif".parse().unwrap()),
81        };
82        let actual = serde_json::from_str::<Animation>(json).unwrap();
83        assert_eq!(actual, expected)
84    }
85}