1// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
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
7use crate::util::error::Error;
8use crate::pubsub::PubSubPayload;
9use crate::ns;
10use minidom::Element;
11use std::convert::TryFrom;
12
13generate_elem_id!(
14 /// The artist or performer of the song or piece.
15 Artist, "artist", TUNE
16);
17
18generate_elem_id!(
19 /// The duration of the song or piece in seconds.
20 Length, "length", TUNE,
21 u16
22);
23
24generate_elem_id!(
25 /// The user's rating of the song or piece, from 1 (lowest) to 10 (highest).
26 Rating, "rating", TUNE,
27 u8
28);
29
30generate_elem_id!(
31 /// The collection (e.g., album) or other source (e.g., a band website that hosts streams or
32 /// audio files).
33 Source, "source", TUNE
34);
35
36generate_elem_id!(
37 /// The title of the song or piece.
38 Title, "title", TUNE
39);
40
41generate_elem_id!(
42 /// A unique identifier for the tune; e.g., the track number within a collection or the
43 /// specific URI for the object (e.g., a stream or audio file).
44 Track, "track", TUNE
45);
46
47generate_elem_id!(
48 /// A URI or URL pointing to information about the song, collection, or artist.
49 Uri, "uri", TUNE
50);
51
52/// Container for formatted text.
53#[derive(Debug, Clone)]
54pub struct Tune {
55 /// The artist or performer of the song or piece.
56 artist: Option<Artist>,
57
58 /// The duration of the song or piece in seconds.
59 length: Option<Length>,
60
61 /// The user's rating of the song or piece, from 1 (lowest) to 10 (highest).
62 rating: Option<Rating>,
63
64 /// The collection (e.g., album) or other source (e.g., a band website that hosts streams or
65 /// audio files).
66 source: Option<Source>,
67
68 /// The title of the song or piece.
69 title: Option<Title>,
70
71 /// A unique identifier for the tune; e.g., the track number within a collection or the
72 /// specific URI for the object (e.g., a stream or audio file).
73 track: Option<Track>,
74
75 /// A URI or URL pointing to information about the song, collection, or artist.
76 uri: Option<Uri>,
77}
78
79impl PubSubPayload for Tune {}
80
81impl Tune {
82 fn new() -> Tune {
83 Tune {
84 artist: None,
85 length: None,
86 rating: None,
87 source: None,
88 title: None,
89 track: None,
90 uri: None,
91 }
92 }
93}
94
95impl TryFrom<Element> for Tune {
96 type Error = Error;
97
98 fn try_from(elem: Element) -> Result<Tune, Error> {
99 check_self!(elem, "tune", TUNE);
100 check_no_attributes!(elem, "tune");
101
102 let mut tune = Tune::new();
103 for child in elem.children() {
104 if child.is("artist", ns::TUNE) {
105 if tune.artist.is_some() {
106 return Err(Error::ParseError("Tune can’t have more than one artist."));
107 }
108 tune.artist = Some(Artist::try_from(child.clone())?);
109 } else if child.is("length", ns::TUNE) {
110 if tune.length.is_some() {
111 return Err(Error::ParseError("Tune can’t have more than one length."));
112 }
113 tune.length = Some(Length::try_from(child.clone())?);
114 } else if child.is("rating", ns::TUNE) {
115 if tune.rating.is_some() {
116 return Err(Error::ParseError("Tune can’t have more than one rating."));
117 }
118 tune.rating = Some(Rating::try_from(child.clone())?);
119 } else if child.is("source", ns::TUNE) {
120 if tune.source.is_some() {
121 return Err(Error::ParseError("Tune can’t have more than one source."));
122 }
123 tune.source = Some(Source::try_from(child.clone())?);
124 } else if child.is("title", ns::TUNE) {
125 if tune.title.is_some() {
126 return Err(Error::ParseError("Tune can’t have more than one title."));
127 }
128 tune.title = Some(Title::try_from(child.clone())?);
129 } else if child.is("track", ns::TUNE) {
130 if tune.track.is_some() {
131 return Err(Error::ParseError("Tune can’t have more than one track."));
132 }
133 tune.track = Some(Track::try_from(child.clone())?);
134 } else if child.is("uri", ns::TUNE) {
135 if tune.uri.is_some() {
136 return Err(Error::ParseError("Tune can’t have more than one uri."));
137 }
138 tune.uri = Some(Uri::try_from(child.clone())?);
139 } else {
140 return Err(Error::ParseError("Unknown element in User Tune."));
141 }
142 }
143
144 Ok(tune)
145 }
146}
147
148impl From<Tune> for Element {
149 fn from(tune: Tune) -> Element {
150 Element::builder("tune")
151 .ns(ns::TUNE)
152 .append_all(tune.artist)
153 .append_all(tune.length)
154 .append_all(tune.rating)
155 .append_all(tune.source)
156 .append_all(tune.title)
157 .append_all(tune.track)
158 .append_all(tune.uri)
159 .build()
160 }
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166 use std::str::FromStr;
167
168 #[cfg(target_pointer_width = "32")]
169 #[test]
170 #[ignore]
171 fn test_size() {
172 assert_size!(Tune, 64);
173 assert_size!(Artist, 12);
174 assert_size!(Length, 2);
175 assert_size!(Rating, 1);
176 assert_size!(Source, 12);
177 assert_size!(Title, 12);
178 assert_size!(Track, 12);
179 assert_size!(Uri, 12);
180 }
181
182 #[cfg(target_pointer_width = "64")]
183 #[test]
184 fn test_size() {
185 assert_size!(Tune, 128);
186 assert_size!(Artist, 24);
187 assert_size!(Length, 2);
188 assert_size!(Rating, 1);
189 assert_size!(Source, 24);
190 assert_size!(Title, 24);
191 assert_size!(Track, 24);
192 assert_size!(Uri, 24);
193 }
194
195 #[test]
196 fn empty() {
197 let elem: Element = "<tune xmlns='http://jabber.org/protocol/tune'/>"
198 .parse()
199 .unwrap();
200 let elem2 = elem.clone();
201 let tune = Tune::try_from(elem).unwrap();
202 assert!(tune.artist.is_none());
203 assert!(tune.length.is_none());
204 assert!(tune.rating.is_none());
205 assert!(tune.source.is_none());
206 assert!(tune.title.is_none());
207 assert!(tune.track.is_none());
208 assert!(tune.uri.is_none());
209
210 let elem3 = tune.into();
211 assert_eq!(elem2, elem3);
212 }
213
214 #[test]
215 fn full() {
216 let elem: Element = "<tune xmlns='http://jabber.org/protocol/tune'><artist>Yes</artist><length>686</length><rating>8</rating><source>Yessongs</source><title>Heart of the Sunrise</title><track>3</track><uri>http://www.yesworld.com/lyrics/Fragile.html#9</uri></tune>"
217 .parse()
218 .unwrap();
219 let tune = Tune::try_from(elem).unwrap();
220 assert_eq!(tune.artist, Some(Artist::from_str("Yes").unwrap()));
221 assert_eq!(tune.length, Some(Length(686)));
222 assert_eq!(tune.rating, Some(Rating(8)));
223 assert_eq!(tune.source, Some(Source::from_str("Yessongs").unwrap()));
224 assert_eq!(tune.title, Some(Title::from_str("Heart of the Sunrise").unwrap()));
225 assert_eq!(tune.track, Some(Track::from_str("3").unwrap()));
226 assert_eq!(tune.uri, Some(Uri::from_str("http://www.yesworld.com/lyrics/Fragile.html#9").unwrap()));
227 }
228}