jingle_ft: Finish implementation of received.

This commit is contained in:
Emmanuel Gil Peyrot 2017-10-31 16:09:28 +00:00
parent b7b3340c9f
commit 0200ced3e5

View file

@ -10,6 +10,7 @@ use std::collections::BTreeMap;
use std::str::FromStr; use std::str::FromStr;
use hashes::Hash; use hashes::Hash;
use jingle::Creator;
use minidom::{Element, IntoElements, IntoAttributeValue, ElementEmitter}; use minidom::{Element, IntoElements, IntoAttributeValue, ElementEmitter};
use chrono::{DateTime, FixedOffset}; use chrono::{DateTime, FixedOffset};
@ -58,12 +59,6 @@ pub struct Description {
pub file: File, pub file: File,
} }
#[derive(Debug, Clone)]
pub enum Creator {
Initiator,
Responder,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Checksum { pub struct Checksum {
pub name: String, pub name: String,
@ -77,17 +72,27 @@ pub struct Received {
pub creator: Creator, pub creator: Creator,
} }
impl IntoElements for Received { impl TryFrom<Element> for Received {
fn into_elements(self, emitter: &mut ElementEmitter) { type Err = Error;
let elem = Element::builder("received")
.ns(ns::JINGLE_FT) fn try_from(elem: Element) -> Result<Received, Error> {
.attr("name", self.name) check_self!(elem, "received", ns::JINGLE_FT);
.attr("creator", match self.creator { check_no_children!(elem, "received");
Creator::Initiator => "initiator", check_no_unknown_attributes!(elem, "received", ["name", "creator"]);
Creator::Responder => "responder", Ok(Received {
name: get_attr!(elem, "name", required),
creator: get_attr!(elem, "creator", required),
}) })
.build(); }
emitter.append_child(elem); }
impl From<Received> for Element {
fn from(received: Received) -> Element {
Element::builder("received")
.ns(ns::JINGLE_FT)
.attr("name", received.name)
.attr("creator", received.creator)
.build()
} }
} }
@ -327,4 +332,48 @@ mod tests {
}; };
assert_eq!(message, "Desc element present twice for the same xml:lang."); assert_eq!(message, "Desc element present twice for the same xml:lang.");
} }
#[test]
fn test_received() {
let elem: Element = "<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator'/>".parse().unwrap();
let received = Received::try_from(elem).unwrap();
assert_eq!(received.name, String::from("coucou"));
assert_eq!(received.creator, Creator::Initiator);
let elem2 = Element::from(received.clone());
let received2 = Received::try_from(elem2).unwrap();
assert_eq!(received2.name, String::from("coucou"));
assert_eq!(received2.creator, Creator::Initiator);
let elem: Element = "<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator'><coucou/></received>".parse().unwrap();
let error = Received::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in received element.");
let elem: Element = "<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator' coucou=''/>".parse().unwrap();
let error = Received::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown attribute in received element.");
let elem: Element = "<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' creator='initiator'/>".parse().unwrap();
let error = Received::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Required attribute 'name' missing.");
let elem: Element = "<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='coucou'/>".parse().unwrap();
let error = Received::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown value for 'creator' attribute.");
}
} }