1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
use iq::Iq;
use message::Message;
use peanuts::{
element::{Content, ContentBuilder, FromContent, FromElement, IntoContent},
DeserializeError,
};
use presence::Presence;
use super::stream::{self, Error as StreamError};
pub mod error;
pub mod iq;
pub mod message;
pub mod presence;
pub const XMLNS: &str = "jabber:client";
/// TODO: End tag
#[derive(Debug, Clone)]
pub enum Stanza {
Message(Message),
Presence(Presence),
Iq(Iq),
Error(StreamError),
OtherContent(Content),
}
impl FromContent for Stanza {
fn from_content(content: Content) -> peanuts::element::DeserializeResult<Self> {
match content {
Content::Element(element) => Ok(Stanza::from_element(element)?),
Content::Text(_) => Ok(Stanza::OtherContent(content)),
Content::PI => Ok(Stanza::OtherContent(content)),
Content::Comment(_) => Ok(Stanza::OtherContent(content)),
}
}
}
impl FromElement for Stanza {
fn from_element(element: peanuts::Element) -> peanuts::element::DeserializeResult<Self> {
match element.identify() {
(Some(XMLNS), "message") => Ok(Stanza::Message(Message::from_element(element)?)),
(Some(XMLNS), "presence") => Ok(Stanza::Presence(Presence::from_element(element)?)),
(Some(XMLNS), "iq") => Ok(Stanza::Iq(Iq::from_element(element)?)),
(Some(stream::XMLNS), "error") => {
Ok(Stanza::Error(StreamError::from_element(element)?))
}
_ => Err(DeserializeError::UnexpectedElement(element)),
}
}
}
impl IntoContent for Stanza {
fn builder(&self) -> peanuts::element::ContentBuilder {
match self {
Stanza::Message(message) => <Message as IntoContent>::builder(message),
Stanza::Presence(presence) => <Presence as IntoContent>::builder(presence),
Stanza::Iq(iq) => <Iq as IntoContent>::builder(iq),
Stanza::Error(error) => <StreamError as IntoContent>::builder(error),
Stanza::OtherContent(_content) => ContentBuilder::Comment("other-content".to_string()),
}
}
}
|