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::AttachmentId;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Attachment {
    /// Attachment's [media type].
    ///
    /// [media type]: https://en.wikipedia.org/wiki/Media_type
    pub content_type: Option<String>,
    pub filename: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub height: Option<u64>,
    pub id: AttachmentId,
    pub proxy_url: String,
    pub size: u64,
    pub url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub width: Option<u64>,
}

#[cfg(test)]
mod tests {
    use super::Attachment;
    use crate::id::AttachmentId;
    use serde_test::Token;

    #[test]
    fn test_attachment() {
        let value = Attachment {
            content_type: Some("image/png".to_owned()),
            filename: "a.png".to_owned(),
            height: Some(184),
            id: AttachmentId(700_000_000_000_000_000),
            proxy_url: "https://cdn.example.com/1.png".to_owned(),
            size: 13_593,
            url: "https://example.com/1.png".to_owned(),
            width: Some(184),
        };

        serde_test::assert_tokens(
            &value,
            &[
                Token::Struct {
                    name: "Attachment",
                    len: 8,
                },
                Token::Str("content_type"),
                Token::Some,
                Token::Str("image/png"),
                Token::Str("filename"),
                Token::Str("a.png"),
                Token::Str("height"),
                Token::Some,
                Token::U64(184),
                Token::Str("id"),
                Token::NewtypeStruct {
                    name: "AttachmentId",
                },
                Token::Str("700000000000000000"),
                Token::Str("proxy_url"),
                Token::Str("https://cdn.example.com/1.png"),
                Token::Str("size"),
                Token::U64(13_593),
                Token::Str("url"),
                Token::Str("https://example.com/1.png"),
                Token::Str("width"),
                Token::Some,
                Token::U64(184),
                Token::StructEnd,
            ],
        );
    }
}