From 4f7c51a5291db7e110012370b4494a9c93c44d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 12 Jun 2023 14:48:11 +0200 Subject: [PATCH] parsers: Allow both Jid and Nick in muc::user::Actor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- parsers/src/muc/user.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/parsers/src/muc/user.rs b/parsers/src/muc/user.rs index a60cc36..b94f5d6 100644 --- a/parsers/src/muc/user.rs +++ b/parsers/src/muc/user.rs @@ -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 for Actor { @@ -105,9 +108,10 @@ impl TryFrom 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 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 = "" - .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 = " (jid, nick), + _ => panic!(), + }; + assert_eq!(jid, "foo@bar/baz".parse::().unwrap()); + assert_eq!(nick, String::from("baz")); + } + #[test] fn test_continue_simple() { let elem: Element = ""