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// WARNING: do not add variants to this enum! Adding variants which refer
13// to anything but IQ, Message or Presence stanzas will cause the
14// stream management counters to be off.
15#[derive(FromXml, AsXml, Debug, PartialEq)]
16#[xml()]
17pub enum Stanza {
18    /// IQ stanza
19    #[xml(transparent)]
20    Iq(Iq),
21
22    /// Message stanza
23    #[xml(transparent)]
24    Message(Message),
25
26    /// Presence stanza
27    #[xml(transparent)]
28    Presence(Presence),
29}
30
31impl From<Iq> for Stanza {
32    fn from(other: Iq) -> Self {
33        Self::Iq(other)
34    }
35}
36
37impl From<Presence> for Stanza {
38    fn from(other: Presence) -> Self {
39        Self::Presence(other)
40    }
41}
42
43impl From<Message> for Stanza {
44    fn from(other: Message) -> Self {
45        Self::Message(other)
46    }
47}
48
49impl TryFrom<Stanza> for Message {
50    type Error = Stanza;
51
52    fn try_from(other: Stanza) -> Result<Self, Stanza> {
53        match other {
54            Stanza::Message(st) => Ok(st),
55            other => Err(other),
56        }
57    }
58}
59
60impl TryFrom<Stanza> for Presence {
61    type Error = Stanza;
62
63    fn try_from(other: Stanza) -> Result<Self, Stanza> {
64        match other {
65            Stanza::Presence(st) => Ok(st),
66            other => Err(other),
67        }
68    }
69}
70
71impl TryFrom<Stanza> for Iq {
72    type Error = Stanza;
73
74    fn try_from(other: Stanza) -> Result<Self, Stanza> {
75        match other {
76            Stanza::Iq(st) => Ok(st),
77            other => Err(other),
78        }
79    }
80}