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/.
67//! Jingle thumbnails (XEP-0264)
89use xso::{AsXml, FromXml};
1011use crate::ns;
12use core::num::NonZeroU16;
1314/// 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)]
20pub uri: String,
2122/// The media type of the thumbnail.
23#[xml(attribute(default, name = "media-type"))]
24pub media_type: Option<String>,
2526/// The width of the thumbnail.
27#[xml(attribute(default))]
28pub width: Option<NonZeroU16>,
2930/// The height of the thumbnail.
31#[xml(attribute(default))]
32pub height: Option<NonZeroU16>,
33}
3435#[cfg(test)]
36mod tests {
37use crate::jingle_thumbnails::Thumbnail;
38use core::num::NonZeroU16;
39use minidom::Element;
4041#[cfg(target_pointer_width = "32")]
42 #[test]
43fn test_size() {
44assert_size!(Thumbnail, 28);
45 }
4647#[cfg(target_pointer_width = "64")]
48 #[test]
49fn test_size() {
50assert_size!(Thumbnail, 56);
51 }
5253#[test]
54fn test_simple_parse() {
55// Extracted from https://xmpp.org/extensions/xep-0264.html#example-1
56let 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'/>";
6162let elem: Element = test_xml.parse().unwrap();
6364let thumbnail = Thumbnail::try_from(elem).unwrap();
6566assert_eq!(
67 thumbnail.uri,
68"cid:sha1+ffd7c8d28e9c5e82afea41f97108c6b4@bob.xmpp.org"
69);
70assert_eq!(thumbnail.media_type.unwrap(), "image/png");
71assert_eq!(thumbnail.width, NonZeroU16::new(128));
72assert_eq!(thumbnail.height, NonZeroU16::new(96));
73 }
74}