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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
use std::ops::Deref;
use peanuts::{
element::{FromElement, IntoElement},
DeserializeError, Element,
};
use tracing::debug;
pub const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-sasl";
#[derive(Debug, Clone)]
pub struct Mechanisms {
pub mechanisms: Vec<String>,
}
impl FromElement for Mechanisms {
fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> {
element.check_name("mechanisms")?;
element.check_namespace(XMLNS)?;
debug!("getting mechanisms");
let mechanisms: Vec<Mechanism> = element.pop_children()?;
debug!("gottting mechanisms");
let mechanisms = mechanisms
.into_iter()
.map(|Mechanism(mechanism)| mechanism)
.collect();
debug!("gottting mechanisms");
Ok(Mechanisms { mechanisms })
}
}
impl IntoElement for Mechanisms {
fn builder(&self) -> peanuts::element::ElementBuilder {
Element::builder("mechanisms", Some(XMLNS)).push_children(
self.mechanisms
.iter()
.map(|mechanism| Mechanism(mechanism.to_string()))
.collect(),
)
}
}
pub struct Mechanism(String);
impl FromElement for Mechanism {
fn from_element(mut element: peanuts::Element) -> peanuts::element::DeserializeResult<Self> {
element.check_name("mechanism")?;
element.check_namespace(XMLNS)?;
let mechanism = element.pop_value()?;
Ok(Mechanism(mechanism))
}
}
impl IntoElement for Mechanism {
fn builder(&self) -> peanuts::element::ElementBuilder {
Element::builder("mechanism", Some(XMLNS)).push_text(self.0.clone())
}
}
impl Deref for Mechanism {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug)]
pub struct Auth {
pub mechanism: String,
pub sasl_data: String,
}
impl IntoElement for Auth {
fn builder(&self) -> peanuts::element::ElementBuilder {
Element::builder("auth", Some(XMLNS))
.push_attribute("mechanism", self.mechanism.clone())
.push_text(self.sasl_data.clone())
}
}
#[derive(Debug)]
pub struct Challenge(String);
impl Deref for Challenge {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl FromElement for Challenge {
fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> {
element.check_name("challenge")?;
element.check_namespace(XMLNS)?;
let sasl_data = element.value()?;
Ok(Challenge(sasl_data))
}
}
#[derive(Debug)]
pub struct Success(String);
impl Deref for Success {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl FromElement for Success {
fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> {
element.check_name("success")?;
element.check_namespace(XMLNS)?;
let sasl_data = element.value()?;
Ok(Success(sasl_data))
}
}
#[derive(Debug)]
pub enum ServerResponse {
Challenge(Challenge),
Success(Success),
}
impl FromElement for ServerResponse {
fn from_element(element: Element) -> peanuts::element::DeserializeResult<Self> {
match element.identify() {
(Some(XMLNS), "challenge") => {
Ok(ServerResponse::Challenge(Challenge::from_element(element)?))
}
(Some(XMLNS), "success") => {
Ok(ServerResponse::Success(Success::from_element(element)?))
}
_ => Err(DeserializeError::UnexpectedElement(element)),
}
}
}
#[derive(Debug)]
pub struct Response(String);
impl Response {
pub fn new(response: String) -> Self {
Self(response)
}
}
impl Deref for Response {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl IntoElement for Response {
fn builder(&self) -> peanuts::element::ElementBuilder {
Element::builder("reponse", Some(XMLNS)).push_text(self.0.clone())
}
}
|