tune.rs

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