mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
parsers: Allow both Jid and Nick in muc::user::Actor
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
03e955d24f
commit
4f7c51a529
1 changed files with 22 additions and 16 deletions
|
@ -92,6 +92,9 @@ pub enum Actor {
|
|||
|
||||
/// The nickname of this user.
|
||||
Nick(String),
|
||||
|
||||
/// Both the full JID associated with this user and the nickname of the user.
|
||||
JidAndNick(FullJid, String),
|
||||
}
|
||||
|
||||
impl TryFrom<Element> for Actor {
|
||||
|
@ -105,9 +108,10 @@ impl TryFrom<Element> for Actor {
|
|||
let nick = get_attr!(elem, "nick", Option);
|
||||
|
||||
match (jid, nick) {
|
||||
(Some(_), Some(_)) | (None, None) => Err(Error::ParseError(
|
||||
(None, None) => Err(Error::ParseError(
|
||||
"Either 'jid' or 'nick' attribute is required.",
|
||||
)),
|
||||
(Some(jid), Some(nick)) => Ok(Actor::JidAndNick(jid, nick)),
|
||||
(Some(jid), _) => Ok(Actor::Jid(jid)),
|
||||
(_, Some(nick)) => Ok(Actor::Nick(nick)),
|
||||
}
|
||||
|
@ -121,6 +125,7 @@ impl From<Actor> for Element {
|
|||
(match actor {
|
||||
Actor::Jid(jid) => elem.attr("jid", jid),
|
||||
Actor::Nick(nick) => elem.attr("nick", nick),
|
||||
Actor::JidAndNick(jid, nick) => elem.attr("jid", jid).attr("nick", nick),
|
||||
})
|
||||
.build()
|
||||
}
|
||||
|
@ -456,21 +461,6 @@ mod tests {
|
|||
assert_eq!(message, "Either 'jid' or 'nick' attribute is required.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_actor_required_attributes2() {
|
||||
let elem: Element = "<actor xmlns='http://jabber.org/protocol/muc#user'
|
||||
jid='foo@bar/baz'
|
||||
nick='baz'/>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let error = Actor::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(message, "Either 'jid' or 'nick' attribute is required.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_actor_jid() {
|
||||
let elem: Element = "<actor xmlns='http://jabber.org/protocol/muc#user'
|
||||
|
@ -498,6 +488,22 @@ mod tests {
|
|||
assert_eq!(nick, "baz".to_owned());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_actor_jid_nick() {
|
||||
let elem: Element = "<actor xmlns='http://jabber.org/protocol/muc#user'
|
||||
jid='foo@bar/baz'
|
||||
nick='baz'/>"
|
||||
.parse()
|
||||
.unwrap();
|
||||
let actor = Actor::try_from(elem).unwrap();
|
||||
let (jid, nick) = match actor {
|
||||
Actor::JidAndNick(jid, nick) => (jid, nick),
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(jid, "foo@bar/baz".parse::<FullJid>().unwrap());
|
||||
assert_eq!(nick, String::from("baz"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_continue_simple() {
|
||||
let elem: Element = "<continue xmlns='http://jabber.org/protocol/muc#user'/>"
|
||||
|
|
Loading…
Reference in a new issue