From f2c8e74ef448950685f1a7922bfe8bbc9cf19bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 17 Sep 2022 16:53:33 +0200 Subject: [PATCH] Return SessionAlreadyExists error in Occupant::add_session MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/error.rs | 2 ++ src/room.rs | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/error.rs b/src/error.rs index 8923f4d..9486a3b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -24,6 +24,7 @@ pub enum Error { MismatchJids(Jid, Jid), NickAlreadyAssigned(String), NonexistantSession(FullJid), + SessionAlreadyExists(FullJid), XMPPError(TokioXMPPError), } @@ -35,6 +36,7 @@ impl fmt::Display for Error { Error::MismatchJids(jid1, jid2) => write!(f, "Mismatch Jids: {}, {}", jid1, jid2), Error::NickAlreadyAssigned(err) => write!(f, "Nickname already assigned: {}", err), Error::NonexistantSession(err) => write!(f, "Session doesn't exist: {}", err), + Error::SessionAlreadyExists(err) => write!(f, "Session already exist: {}", err), Error::XMPPError(err) => write!(f, "XMPP error: {}", err), } } diff --git a/src/room.rs b/src/room.rs index 84a7a46..f8cdb15 100644 --- a/src/room.rs +++ b/src/room.rs @@ -221,15 +221,28 @@ impl Occupant { pub fn add_session(&mut self, real: FullJid) -> Result<(), Error> { if BareJid::from(real.clone()) != self.real { - return Err(Error::MismatchJids(Jid::from(self.real.clone()), Jid::from(real.clone()))); + return Err(Error::MismatchJids( + Jid::from(self.real.clone()), + Jid::from(real.clone()), + )); } + for session in &self.sessions { + if &real == session { + return Err(Error::SessionAlreadyExists(real)); + } + } + + self.sessions.push(real); Ok(()) } pub fn remove_session(&mut self, real: FullJid) -> Result<(), Error> { if BareJid::from(real.clone()) != self.real { - return Err(Error::MismatchJids(Jid::from(self.real.clone()), Jid::from(real.clone()))); + return Err(Error::MismatchJids( + Jid::from(self.real.clone()), + Jid::from(real.clone()), + )); } let len = self.sessions.len();