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

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-06-12 14:48:11 +02:00 committed by pep
parent 03e955d24f
commit 4f7c51a529

View file

@ -92,6 +92,9 @@ pub enum Actor {
/// The nickname of this user. /// The nickname of this user.
Nick(String), Nick(String),
/// Both the full JID associated with this user and the nickname of the user.
JidAndNick(FullJid, String),
} }
impl TryFrom<Element> for Actor { impl TryFrom<Element> for Actor {
@ -105,9 +108,10 @@ impl TryFrom<Element> for Actor {
let nick = get_attr!(elem, "nick", Option); let nick = get_attr!(elem, "nick", Option);
match (jid, nick) { match (jid, nick) {
(Some(_), Some(_)) | (None, None) => Err(Error::ParseError( (None, None) => Err(Error::ParseError(
"Either 'jid' or 'nick' attribute is required.", "Either 'jid' or 'nick' attribute is required.",
)), )),
(Some(jid), Some(nick)) => Ok(Actor::JidAndNick(jid, nick)),
(Some(jid), _) => Ok(Actor::Jid(jid)), (Some(jid), _) => Ok(Actor::Jid(jid)),
(_, Some(nick)) => Ok(Actor::Nick(nick)), (_, Some(nick)) => Ok(Actor::Nick(nick)),
} }
@ -121,6 +125,7 @@ impl From<Actor> for Element {
(match actor { (match actor {
Actor::Jid(jid) => elem.attr("jid", jid), Actor::Jid(jid) => elem.attr("jid", jid),
Actor::Nick(nick) => elem.attr("nick", nick), Actor::Nick(nick) => elem.attr("nick", nick),
Actor::JidAndNick(jid, nick) => elem.attr("jid", jid).attr("nick", nick),
}) })
.build() .build()
} }
@ -456,21 +461,6 @@ mod tests {
assert_eq!(message, "Either 'jid' or 'nick' attribute is required."); 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] #[test]
fn test_actor_jid() { fn test_actor_jid() {
let elem: Element = "<actor xmlns='http://jabber.org/protocol/muc#user' let elem: Element = "<actor xmlns='http://jabber.org/protocol/muc#user'
@ -498,6 +488,22 @@ mod tests {
assert_eq!(nick, "baz".to_owned()); 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] #[test]
fn test_continue_simple() { fn test_continue_simple() {
let elem: Element = "<continue xmlns='http://jabber.org/protocol/muc#user'/>" let elem: Element = "<continue xmlns='http://jabber.org/protocol/muc#user'/>"