presence: PresenceFull newtype

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-12-26 13:28:26 +01:00
parent 38838991c0
commit 28186d77d2
2 changed files with 47 additions and 0 deletions

View file

@ -22,6 +22,7 @@ mod component;
mod error;
mod handlers;
mod occupant;
mod presence;
mod room;
mod session;

46
src/presence.rs Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
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<Presence> for PresenceFull {
type Error = Error;
fn try_from(presence: Presence) -> Result<Self, Self::Error> {
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
}
}