From 21398447e7fe96c287aeed3bc448857fa80b24af Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 30 Apr 2017 21:29:55 +0100 Subject: [PATCH] presence: Implement priority parsing. --- src/presence.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/presence.rs b/src/presence.rs index ad6301e..3f29833 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -40,11 +40,14 @@ impl IntoElements for Show { pub type Status = String; +pub type Priority = i8; + /// Lists every known payload of a ``. #[derive(Debug, Clone)] pub enum PresencePayload { Show(Show), Status(Status), + Priority(Priority), Delay(delay::Delay), ECaps2(ecaps2::ECaps2), } @@ -153,6 +156,13 @@ pub fn parse_presence(root: &Element) -> Result { } let payload = PresencePayload::Status(elem.text()); payloads.push(PresencePayloadType::Parsed(payload)); + } else if elem.is("priority", ns::JABBER_CLIENT) { + for _ in elem.children() { + return Err(Error::ParseError("Unknown child in priority element.")); + } + let priority = Priority::from_str(elem.text().as_ref())?; + let payload = PresencePayload::Priority(priority); + payloads.push(PresencePayloadType::Parsed(payload)); } else { let payload = if let Ok(delay) = delay::parse_delay(elem) { Some(PresencePayload::Delay(delay)) @@ -190,6 +200,12 @@ pub fn serialise_payload(payload: &PresencePayload) -> Element { .append(status.to_owned()) .build() }, + PresencePayload::Priority(ref priority) => { + Element::builder("status") + .ns(ns::JABBER_CLIENT) + .append(format!("{}", priority)) + .build() + }, PresencePayload::Delay(ref delay) => delay::serialise(delay), PresencePayload::ECaps2(ref ecaps2) => ecaps2::serialise(ecaps2), } @@ -295,6 +311,29 @@ mod tests { } } + #[test] + fn test_priority() { + let elem: Element = "-1".parse().unwrap(); + let presence = presence::parse_presence(&elem).unwrap(); + assert_eq!(presence.payloads.len(), 1); + match presence.payloads[0] { + presence::PresencePayloadType::Parsed(presence::PresencePayload::Priority(ref priority)) => { + assert_eq!(*priority, presence::Priority::from(-1i8)); + }, + _ => panic!("Failed to parse priority."), + } + } + + #[test] + fn test_invalid_priority() { + let elem: Element = "128".parse().unwrap(); + let error = presence::parse_presence(&elem).unwrap_err(); + match error { + Error::ParseIntError(_) => (), + _ => panic!(), + }; + } + #[test] fn test_unknown_child() { let elem: Element = "".parse().unwrap();