presence: Implement priority parsing.
This commit is contained in:
parent
4ec3898c2f
commit
21398447e7
1 changed files with 39 additions and 0 deletions
|
@ -40,11 +40,14 @@ impl IntoElements for Show {
|
||||||
|
|
||||||
pub type Status = String;
|
pub type Status = String;
|
||||||
|
|
||||||
|
pub type Priority = i8;
|
||||||
|
|
||||||
/// Lists every known payload of a `<presence/>`.
|
/// Lists every known payload of a `<presence/>`.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum PresencePayload {
|
pub enum PresencePayload {
|
||||||
Show(Show),
|
Show(Show),
|
||||||
Status(Status),
|
Status(Status),
|
||||||
|
Priority(Priority),
|
||||||
Delay(delay::Delay),
|
Delay(delay::Delay),
|
||||||
ECaps2(ecaps2::ECaps2),
|
ECaps2(ecaps2::ECaps2),
|
||||||
}
|
}
|
||||||
|
@ -153,6 +156,13 @@ pub fn parse_presence(root: &Element) -> Result<Presence, Error> {
|
||||||
}
|
}
|
||||||
let payload = PresencePayload::Status(elem.text());
|
let payload = PresencePayload::Status(elem.text());
|
||||||
payloads.push(PresencePayloadType::Parsed(payload));
|
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 {
|
} else {
|
||||||
let payload = if let Ok(delay) = delay::parse_delay(elem) {
|
let payload = if let Ok(delay) = delay::parse_delay(elem) {
|
||||||
Some(PresencePayload::Delay(delay))
|
Some(PresencePayload::Delay(delay))
|
||||||
|
@ -190,6 +200,12 @@ pub fn serialise_payload(payload: &PresencePayload) -> Element {
|
||||||
.append(status.to_owned())
|
.append(status.to_owned())
|
||||||
.build()
|
.build()
|
||||||
},
|
},
|
||||||
|
PresencePayload::Priority(ref priority) => {
|
||||||
|
Element::builder("status")
|
||||||
|
.ns(ns::JABBER_CLIENT)
|
||||||
|
.append(format!("{}", priority))
|
||||||
|
.build()
|
||||||
|
},
|
||||||
PresencePayload::Delay(ref delay) => delay::serialise(delay),
|
PresencePayload::Delay(ref delay) => delay::serialise(delay),
|
||||||
PresencePayload::ECaps2(ref ecaps2) => ecaps2::serialise(ecaps2),
|
PresencePayload::ECaps2(ref ecaps2) => ecaps2::serialise(ecaps2),
|
||||||
}
|
}
|
||||||
|
@ -295,6 +311,29 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_priority() {
|
||||||
|
let elem: Element = "<presence xmlns='jabber:client'><priority>-1</priority></presence>".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 = "<presence xmlns='jabber:client'><priority>128</priority></presence>".parse().unwrap();
|
||||||
|
let error = presence::parse_presence(&elem).unwrap_err();
|
||||||
|
match error {
|
||||||
|
Error::ParseIntError(_) => (),
|
||||||
|
_ => panic!(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unknown_child() {
|
fn test_unknown_child() {
|
||||||
let elem: Element = "<presence xmlns='jabber:client'><test xmlns='invalid'/></presence>".parse().unwrap();
|
let elem: Element = "<presence xmlns='jabber:client'><test xmlns='invalid'/></presence>".parse().unwrap();
|
||||||
|
|
Loading…
Reference in a new issue