twilight_model/poll/
media.rs

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
use crate::id::{marker::EmojiMarker, Id};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct PollMedia {
    /// The emoji of the field.
    ///
    /// When creating a poll answer with an emoji, one only
    /// needs to send either the id (custom emoji) or name
    /// (default emoji) as the only field.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub emoji: Option<PartialPollMediaEmoji>,
    /// The text of the field.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct PartialPollMediaEmoji {
    #[serde(default)]
    pub animated: bool,
    pub id: Option<Id<EmojiMarker>>,
    pub name: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::{PartialPollMediaEmoji, PollMedia};
    use crate::id::Id;
    use serde_test::Token;

    #[test]
    fn poll_media() {
        let value = PollMedia {
            emoji: Some(PartialPollMediaEmoji {
                animated: true,
                id: Some(Id::new(1)),
                name: Some("a".to_owned()),
            }),
            text: Some("b".to_owned()),
        };

        serde_test::assert_tokens(
            &value,
            &[
                Token::Struct {
                    name: "PollMedia",
                    len: 2,
                },
                Token::Str("emoji"),
                Token::Some,
                Token::Struct {
                    name: "PartialPollMediaEmoji",
                    len: 3,
                },
                Token::Str("animated"),
                Token::Bool(true),
                Token::Str("id"),
                Token::Some,
                Token::NewtypeStruct { name: "Id" },
                Token::Str("1"),
                Token::Str("name"),
                Token::Some,
                Token::Str("a"),
                Token::StructEnd,
                Token::Str("text"),
                Token::Some,
                Token::Str("b"),
                Token::StructEnd,
            ],
        );
    }
}