mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
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 Priority = i8;
|
||||
|
||||
/// Lists every known payload of a `<presence/>`.
|
||||
#[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<Presence, Error> {
|
|||
}
|
||||
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 = "<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]
|
||||
fn test_unknown_child() {
|
||||
let elem: Element = "<presence xmlns='jabber:client'><test xmlns='invalid'/></presence>".parse().unwrap();
|
||||
|
|
Loading…
Reference in a new issue