New User Tune parser (XEP-0118).

This commit is contained in:
Emmanuel Gil Peyrot 2019-09-05 15:37:34 +02:00
parent c77221e437
commit 77920e5f46
5 changed files with 229 additions and 0 deletions

View file

@ -2,6 +2,7 @@ Version NEXT:
DATE Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
* New parsers/serialisers:
- XHTML-IM (XEP-0071)
- User Tune (XEP-0118)
- Bits of Binary (XEP-0231)
- Message Carbons (XEP-0280)
* Breaking changes:

View file

@ -183,6 +183,14 @@
<xmpp:since>0.4.0</xmpp:since>
</xmpp:SupportedXep>
</implements>
<implements>
<xmpp:SupportedXep>
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0118.html"/>
<xmpp:status>complete</xmpp:status>
<xmpp:version>1.2</xmpp:version>
<xmpp:since>NEXT</xmpp:since>
</xmpp:SupportedXep>
</implements>
<implements>
<xmpp:SupportedXep>
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0157.html"/>

View file

@ -102,6 +102,9 @@ pub mod component;
/// XEP-0115: Entity Capabilities
pub mod caps;
/// XEP-0118: User Tune
pub mod tune;
/// XEP-0157: Contact Addresses for XMPP Services
pub mod server_info;

View file

@ -84,6 +84,9 @@ pub const COMPONENT: &str = "jabber:component:accept";
/// XEP-0115: Entity Capabilities
pub const CAPS: &str = "http://jabber.org/protocol/caps";
/// XEP-0118: User Tune
pub const TUNE: &str = "http://jabber.org/protocol/tune";
/// XEP-0157: Contact Addresses for XMPP Services
pub const SERVER_INFO: &str = "http://jabber.org/network/serverinfo";

214
src/tune.rs Normal file
View file

@ -0,0 +1,214 @@
// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::util::error::Error;
use crate::pubsub::PubSubPayload;
use crate::ns;
use minidom::Element;
use std::convert::TryFrom;
generate_elem_id!(
/// The artist or performer of the song or piece.
Artist, "artist", TUNE
);
generate_elem_id!(
/// The duration of the song or piece in seconds.
Length, "length", TUNE,
u16
);
generate_elem_id!(
/// The user's rating of the song or piece, from 1 (lowest) to 10 (highest).
Rating, "rating", TUNE,
u8
);
generate_elem_id!(
/// The collection (e.g., album) or other source (e.g., a band website that hosts streams or
/// audio files).
Source, "source", TUNE
);
generate_elem_id!(
/// The title of the song or piece.
Title, "title", TUNE
);
generate_elem_id!(
/// A unique identifier for the tune; e.g., the track number within a collection or the
/// specific URI for the object (e.g., a stream or audio file).
Track, "track", TUNE
);
generate_elem_id!(
/// A URI or URL pointing to information about the song, collection, or artist.
Uri, "uri", TUNE
);
/// Container for formatted text.
#[derive(Debug, Clone)]
pub struct Tune {
/// The artist or performer of the song or piece.
artist: Option<Artist>,
/// The duration of the song or piece in seconds.
length: Option<Length>,
/// The user's rating of the song or piece, from 1 (lowest) to 10 (highest).
rating: Option<Rating>,
/// The collection (e.g., album) or other source (e.g., a band website that hosts streams or
/// audio files).
source: Option<Source>,
/// The title of the song or piece.
title: Option<Title>,
/// A unique identifier for the tune; e.g., the track number within a collection or the
/// specific URI for the object (e.g., a stream or audio file).
track: Option<Track>,
/// A URI or URL pointing to information about the song, collection, or artist.
uri: Option<Uri>,
}
impl PubSubPayload for Tune {}
impl Tune {
fn new() -> Tune {
Tune {
artist: None,
length: None,
rating: None,
source: None,
title: None,
track: None,
uri: None,
}
}
}
impl TryFrom<Element> for Tune {
type Error = Error;
fn try_from(elem: Element) -> Result<Tune, Error> {
check_self!(elem, "tune", TUNE);
check_no_attributes!(elem, "tune");
let mut tune = Tune::new();
for child in elem.children() {
if child.is("artist", ns::TUNE) {
if tune.artist.is_some() {
return Err(Error::ParseError("Tune cant have more than one artist."));
}
tune.artist = Some(Artist::try_from(child.clone())?);
} else if child.is("length", ns::TUNE) {
if tune.length.is_some() {
return Err(Error::ParseError("Tune cant have more than one length."));
}
tune.length = Some(Length::try_from(child.clone())?);
} else if child.is("rating", ns::TUNE) {
if tune.rating.is_some() {
return Err(Error::ParseError("Tune cant have more than one rating."));
}
tune.rating = Some(Rating::try_from(child.clone())?);
} else if child.is("source", ns::TUNE) {
if tune.source.is_some() {
return Err(Error::ParseError("Tune cant have more than one source."));
}
tune.source = Some(Source::try_from(child.clone())?);
} else if child.is("title", ns::TUNE) {
if tune.title.is_some() {
return Err(Error::ParseError("Tune cant have more than one title."));
}
tune.title = Some(Title::try_from(child.clone())?);
} else if child.is("track", ns::TUNE) {
if tune.track.is_some() {
return Err(Error::ParseError("Tune cant have more than one track."));
}
tune.track = Some(Track::try_from(child.clone())?);
} else if child.is("uri", ns::TUNE) {
if tune.uri.is_some() {
return Err(Error::ParseError("Tune cant have more than one uri."));
}
tune.uri = Some(Uri::try_from(child.clone())?);
} else {
return Err(Error::ParseError("Unknown element in User Tune."));
}
}
Ok(tune)
}
}
impl From<Tune> for Element {
fn from(tune: Tune) -> Element {
Element::builder("tune")
.ns(ns::TUNE)
.append(tune.artist)
.append(tune.length)
.append(tune.rating)
.append(tune.source)
.append(tune.title)
.append(tune.track)
.append(tune.uri)
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
#[cfg(target_pointer_width = "32")]
#[test]
#[ignore]
fn test_size() {
assert_size!(Tune, 0);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(Tune, 128);
}
#[test]
fn empty() {
let elem: Element = "<tune xmlns='http://jabber.org/protocol/tune'/>"
.parse()
.unwrap();
let elem2 = elem.clone();
let tune = Tune::try_from(elem).unwrap();
assert!(tune.artist.is_none());
assert!(tune.length.is_none());
assert!(tune.rating.is_none());
assert!(tune.source.is_none());
assert!(tune.title.is_none());
assert!(tune.track.is_none());
assert!(tune.uri.is_none());
let elem3 = tune.into();
assert_eq!(elem2, elem3);
}
#[test]
fn full() {
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>"
.parse()
.unwrap();
let tune = Tune::try_from(elem).unwrap();
assert_eq!(tune.artist, Some(Artist::from_str("Yes").unwrap()));
assert_eq!(tune.length, Some(Length(686)));
assert_eq!(tune.rating, Some(Rating(8)));
assert_eq!(tune.source, Some(Source::from_str("Yessongs").unwrap()));
assert_eq!(tune.title, Some(Title::from_str("Heart of the Sunrise").unwrap()));
assert_eq!(tune.track, Some(Track::from_str("3").unwrap()));
assert_eq!(tune.uri, Some(Uri::from_str("http://www.yesworld.com/lyrics/Fragile.html#9").unwrap()));
}
}