Skip to main content

xmpp_parsers/
stanza.rs

1// Copyright (c) 2024 Jonas Schäfer <jonas@zombofant.net>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use xso::{AsXml, FromXml};
8
9use crate::{iq::Iq, message::Message, presence::Presence};
10
11/// A stanza sent/received over the stream.
12///
13/// WARNING: do not add variants to this enum! Adding variants which refer
14/// to anything but [`Iq`], [`Message`] or [`Presence`] stanzas will cause the
15/// stream management counters to be off.
16#[derive(FromXml, AsXml, Debug, PartialEq)]
17#[xml()]
18pub enum Stanza {
19    /// IQ stanza
20    #[xml(transparent)]
21    Iq(Iq),
22
23    /// Message stanza
24    #[xml(transparent)]
25    Message(Message),
26
27    /// Presence stanza
28    #[xml(transparent)]
29    Presence(Presence),
30}
31
32impl From<Iq> for Stanza {
33    fn from(other: Iq) -> Self {
34        Self::Iq(other)
35    }
36}
37
38impl From<Presence> for Stanza {
39    fn from(other: Presence) -> Self {
40        Self::Presence(other)
41    }
42}
43
44impl From<Message> for Stanza {
45    fn from(other: Message) -> Self {
46        Self::Message(other)
47    }
48}
49
50impl TryFrom<Stanza> for Message {
51    type Error = Stanza;
52
53    fn try_from(other: Stanza) -> Result<Self, Stanza> {
54        match other {
55            Stanza::Message(st) => Ok(st),
56            other => Err(other),
57        }
58    }
59}
60
61impl TryFrom<Stanza> for Presence {
62    type Error = Stanza;
63
64    fn try_from(other: Stanza) -> Result<Self, Stanza> {
65        match other {
66            Stanza::Presence(st) => Ok(st),
67            other => Err(other),
68        }
69    }
70}
71
72impl TryFrom<Stanza> for Iq {
73    type Error = Stanza;
74
75    fn try_from(other: Stanza) -> Result<Self, Stanza> {
76        match other {
77            Stanza::Iq(st) => Ok(st),
78            other => Err(other),
79        }
80    }
81}