1use xso::{
8 text::{Base64, StripWhitespace, TextCodec},
9 AsXml, FromXml,
10};
11
12use crate::hashes::Sha1HexAttribute;
13use crate::ns;
14use crate::pubsub::PubSubPayload;
15
16#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
18#[xml(namespace = ns::AVATAR_METADATA, name = "metadata")]
19pub struct Metadata {
20 #[xml(child(n = ..))]
22 pub infos: Vec<Info>,
23}
24
25impl PubSubPayload for Metadata {}
26
27#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
29#[xml(namespace = ns::AVATAR_METADATA, name = "info")]
30pub struct Info {
31 #[xml(attribute)]
33 pub bytes: u32,
34
35 #[xml(attribute(default))]
37 pub width: Option<u16>,
38
39 #[xml(attribute(default))]
41 pub height: Option<u16>,
42
43 #[xml(attribute)]
45 pub id: Sha1HexAttribute,
46
47 #[xml(attribute = "type")]
49 pub type_: String,
50
51 #[xml(attribute(default))]
53 pub url: Option<String>,
54}
55
56#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
58#[xml(namespace = ns::AVATAR_DATA, name = "data")]
59pub struct Data {
60 #[xml(text(codec = Base64.filtered(StripWhitespace)))]
62 pub data: Vec<u8>,
63}
64
65impl PubSubPayload for Data {}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70 use crate::hashes::Algo;
71 use minidom::Element;
72 #[cfg(not(feature = "disable-validation"))]
73 use xso::error::{Error, FromElementError};
74
75 #[cfg(target_pointer_width = "32")]
76 #[test]
77 fn test_size() {
78 assert_size!(Metadata, 12);
79 assert_size!(Info, 60);
80 assert_size!(Data, 12);
81 }
82
83 #[cfg(target_pointer_width = "64")]
84 #[test]
85 fn test_size() {
86 assert_size!(Metadata, 24);
87 assert_size!(Info, 112);
88 assert_size!(Data, 24);
89 }
90
91 #[test]
92 fn test_simple() {
93 let elem: Element = "<metadata xmlns='urn:xmpp:avatar:metadata'>
94 <info bytes='12345' width='64' height='64'
95 id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'
96 type='image/png'/>
97 </metadata>"
98 .parse()
99 .unwrap();
100 let metadata = Metadata::try_from(elem).unwrap();
101 assert_eq!(metadata.infos.len(), 1);
102 let info = &metadata.infos[0];
103 assert_eq!(info.bytes, 12345);
104 assert_eq!(info.width, Some(64));
105 assert_eq!(info.height, Some(64));
106 assert_eq!(info.id.algo, Algo::Sha_1);
107 assert_eq!(info.type_, "image/png");
108 assert_eq!(info.url, None);
109 assert_eq!(
110 info.id.hash,
111 [
112 17, 31, 75, 60, 80, 215, 176, 223, 114, 157, 41, 155, 198, 248, 233, 239, 144, 102,
113 151, 31
114 ]
115 );
116
117 let elem: Element = "<data xmlns='urn:xmpp:avatar:data'>AAAA</data>"
118 .parse()
119 .unwrap();
120 let data = Data::try_from(elem).unwrap();
121 assert_eq!(data.data, b"\0\0\0");
122 }
123
124 #[cfg(not(feature = "disable-validation"))]
125 #[test]
126 fn test_invalid() {
127 let elem: Element = "<data xmlns='urn:xmpp:avatar:data' id='coucou'/>"
128 .parse()
129 .unwrap();
130 let error = Data::try_from(elem).unwrap_err();
131 let message = match error {
132 FromElementError::Invalid(Error::Other(string)) => string,
133 _ => panic!(),
134 };
135 assert_eq!(message, "Unknown attribute in Data element.")
136 }
137}