1// Copyright (c) 2023 XMPP-RS contributors.
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, you can obtain one at http://mozilla.org/MPL/2.0/.
6
7//! Jingle thumbnails (XEP-0264)
8
9use xso::{AsXml, FromXml};
10
11use crate::ns;
12use core::num::NonZeroU16;
13
14/// A Jingle thumbnail.
15#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
16#[xml(namespace = ns::JINGLE_THUMBNAILS, name = "thumbnail")]
17pub struct Thumbnail {
18 /// The URI of the thumbnail.
19 #[xml(attribute)]
20 pub uri: String,
21
22 /// The media type of the thumbnail.
23 #[xml(attribute(default, name = "media-type"))]
24 pub media_type: Option<String>,
25
26 /// The width of the thumbnail.
27 #[xml(attribute(default))]
28 pub width: Option<NonZeroU16>,
29
30 /// The height of the thumbnail.
31 #[xml(attribute(default))]
32 pub height: Option<NonZeroU16>,
33}
34
35#[cfg(test)]
36mod tests {
37 use crate::jingle_thumbnails::Thumbnail;
38 use core::num::NonZeroU16;
39 use minidom::Element;
40
41 #[cfg(target_pointer_width = "32")]
42 #[test]
43 fn test_size() {
44 assert_size!(Thumbnail, 28);
45 }
46
47 #[cfg(target_pointer_width = "64")]
48 #[test]
49 fn test_size() {
50 assert_size!(Thumbnail, 56);
51 }
52
53 #[test]
54 fn test_simple_parse() {
55 // Extracted from https://xmpp.org/extensions/xep-0264.html#example-1
56 let test_xml = "<thumbnail xmlns='urn:xmpp:thumbs:1'
57 uri='cid:sha1+ffd7c8d28e9c5e82afea41f97108c6b4@bob.xmpp.org'
58 media-type='image/png'
59 width='128'
60 height='96'/>";
61
62 let elem: Element = test_xml.parse().unwrap();
63
64 let thumbnail = Thumbnail::try_from(elem).unwrap();
65
66 assert_eq!(
67 thumbnail.uri,
68 "cid:sha1+ffd7c8d28e9c5e82afea41f97108c6b4@bob.xmpp.org"
69 );
70 assert_eq!(thumbnail.media_type.unwrap(), "image/png");
71 assert_eq!(thumbnail.width, NonZeroU16::new(128));
72 assert_eq!(thumbnail.height, NonZeroU16::new(96));
73 }
74}