Merge branch 'actor-jid-nick' into 'main'

Draft: parsers: Allow both Jid and Nick in muc::user::Actor

Closes #88

See merge request xmpp-rs/xmpp-rs!196
This commit is contained in:
pep 2024-04-21 05:34:26 +00:00
commit dbf3c3b779
1 changed files with 22 additions and 16 deletions

View File

@ -91,6 +91,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 {
@ -104,9 +107,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)),
}
@ -120,6 +124,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()
}
@ -461,21 +466,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'
@ -503,6 +493,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'/>"