From 28186d77d2f9a58836ee431a7b4409c57a90309f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 26 Dec 2022 13:28:26 +0100 Subject: [PATCH] presence: PresenceFull newtype MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/main.rs | 1 + src/presence.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/presence.rs diff --git a/src/main.rs b/src/main.rs index ce35657..8f6cccb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ mod component; mod error; mod handlers; mod occupant; +mod presence; mod room; mod session; diff --git a/src/presence.rs b/src/presence.rs new file mode 100644 index 0000000..189eeed --- /dev/null +++ b/src/presence.rs @@ -0,0 +1,46 @@ +// Copyright (C) 2022-2099 The crate authors. +// +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU Affero General Public License as published by the +// Free Software Foundation, either version 3 of the License, or (at your +// option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License +// for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use crate::error::Error; +use std::convert::TryFrom; +use std::ops::Deref; +use xmpp_parsers::{presence::Presence, Jid}; + +/// Presence that has a FullJid as destination (to). +#[derive(Debug, Clone, PartialEq)] +pub struct PresenceFull(Presence); + +impl TryFrom for PresenceFull { + type Error = Error; + + fn try_from(presence: Presence) -> Result { + match (&presence.from, &presence.to) { + (Some(Jid::Full(_)), Some(Jid::Full(_))) => (), + (Some(Jid::Full(_)), _) => return Err(Error::InvalidDestinationJid), + (_, Some(Jid::Full(_))) => return Err(Error::InvalidOriginJid), + _ => return Err(Error::InvalidOriginJid), + } + + Ok(Self(presence)) + } +} + +impl Deref for PresenceFull { + type Target = Presence; + + fn deref(&self) -> &Self::Target { + &self.0 + } +}