lib.rs

  1//! A crate parsing common XMPP elements into Rust structures.
  2//!
  3//! Each module implements the [`TryFrom<Element>`] trait, which takes a
  4//! minidom [`Element`] and returns a `Result` whose value is `Ok` if the
  5//! element parsed correctly, `Err(error::Error)` otherwise.
  6//!
  7//! The returned structure can be manipuled as any Rust structure, with each
  8//! field being public.  You can also create the same structure manually, with
  9//! some having `new()` and `with_*()` helper methods to create them.
 10//!
 11//! Once you are happy with your structure, you can serialise it back to an
 12//! [`Element`], using either `From` or `Into<Element>`, which give you what
 13//! you want to be sending on the wire.
 14//!
 15//! [`TryFrom<Element>`]: ../try_from/trait.TryFrom.html
 16//! [`Element`]: ../minidom/element/struct.Element.html
 17
 18// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 19// Copyright (c) 2017 Maxime “pep” Buquet <pep+code@bouah.net>
 20//
 21// This Source Code Form is subject to the terms of the Mozilla Public
 22// License, v. 2.0. If a copy of the MPL was not distributed with this
 23// file, You can obtain one at http://mozilla.org/MPL/2.0/.
 24
 25extern crate minidom;
 26extern crate jid;
 27extern crate base64;
 28extern crate digest;
 29extern crate sha_1;
 30extern crate sha2;
 31extern crate sha3;
 32extern crate blake2;
 33extern crate chrono;
 34extern crate try_from;
 35
 36macro_rules! get_attr {
 37    ($elem:ident, $attr:tt, $type:tt) => (
 38        get_attr!($elem, $attr, $type, value, value.parse()?)
 39    );
 40    ($elem:ident, $attr:tt, optional, $value:ident, $func:expr) => (
 41        match $elem.attr($attr) {
 42            Some($value) => Some($func),
 43            None => None,
 44        }
 45    );
 46    ($elem:ident, $attr:tt, required, $value:ident, $func:expr) => (
 47        match $elem.attr($attr) {
 48            Some($value) => $func,
 49            None => return Err(Error::ParseError(concat!("Required attribute '", $attr, "' missing."))),
 50        }
 51    );
 52    ($elem:ident, $attr:tt, default, $value:ident, $func:expr) => (
 53        match $elem.attr($attr) {
 54            Some($value) => $func,
 55            None => Default::default(),
 56        }
 57    );
 58}
 59
 60macro_rules! generate_attribute {
 61    ($elem:ident, $name:tt, {$($a:ident => $b:tt),+,}) => (
 62        generate_attribute!($elem, $name, {$($a => $b),+});
 63    );
 64    ($elem:ident, $name:tt, {$($a:ident => $b:tt),+,}, Default = $default:ident) => (
 65        generate_attribute!($elem, $name, {$($a => $b),+}, Default = $default);
 66    );
 67    ($elem:ident, $name:tt, {$($a:ident => $b:tt),+}) => (
 68        #[derive(Debug, Clone, PartialEq)]
 69        pub enum $elem {
 70            $(
 71                #[doc=$b]
 72                #[doc="value for this attribute."]
 73                $a
 74            ),+
 75        }
 76        impl FromStr for $elem {
 77            type Err = Error;
 78            fn from_str(s: &str) -> Result<$elem, Error> {
 79                Ok(match s {
 80                    $($b => $elem::$a),+,
 81                    _ => return Err(Error::ParseError(concat!("Unknown value for '", $name, "' attribute."))),
 82                })
 83            }
 84        }
 85        impl IntoAttributeValue for $elem {
 86            fn into_attribute_value(self) -> Option<String> {
 87                Some(String::from(match self {
 88                    $($elem::$a => $b),+
 89                }))
 90            }
 91        }
 92    );
 93    ($elem:ident, $name:tt, {$($a:ident => $b:tt),+}, Default = $default:ident) => (
 94        #[derive(Debug, Clone, PartialEq)]
 95        pub enum $elem {
 96            $(
 97                #[doc=$b]
 98                #[doc="value for this attribute."]
 99                $a
100            ),+
101        }
102        impl FromStr for $elem {
103            type Err = Error;
104            fn from_str(s: &str) -> Result<$elem, Error> {
105                Ok(match s {
106                    $($b => $elem::$a),+,
107                    _ => return Err(Error::ParseError(concat!("Unknown value for '", $name, "' attribute."))),
108                })
109            }
110        }
111        impl IntoAttributeValue for $elem {
112            #[allow(unreachable_patterns)]
113            fn into_attribute_value(self) -> Option<String> {
114                Some(String::from(match self {
115                    $elem::$default => return None,
116                    $($elem::$a => $b),+
117                }))
118            }
119        }
120        impl Default for $elem {
121            fn default() -> $elem {
122                $elem::$default
123            }
124        }
125    );
126}
127
128macro_rules! generate_id {
129    ($elem:ident) => (
130        #[derive(Debug, Clone, PartialEq, Eq, Hash)]
131        pub struct $elem(pub String);
132        impl FromStr for $elem {
133            type Err = Error;
134            fn from_str(s: &str) -> Result<$elem, Error> {
135                // TODO: add a way to parse that differently when needed.
136                Ok($elem(String::from(s)))
137            }
138        }
139        impl IntoAttributeValue for $elem {
140            fn into_attribute_value(self) -> Option<String> {
141                Some(self.0)
142            }
143        }
144    );
145}
146
147macro_rules! generate_elem_id {
148    ($elem:ident, $name:tt, $ns:expr) => (
149        #[derive(Debug, Clone, PartialEq, Eq, Hash)]
150        pub struct $elem(pub String);
151        impl FromStr for $elem {
152            type Err = Error;
153            fn from_str(s: &str) -> Result<$elem, Error> {
154                // TODO: add a way to parse that differently when needed.
155                Ok($elem(String::from(s)))
156            }
157        }
158        impl From<$elem> for Element {
159            fn from(elem: $elem) -> Element {
160                Element::builder($name)
161                        .ns($ns)
162                        .append(elem.0)
163                        .build()
164            }
165        }
166    );
167}
168
169/// Error type returned by every parser on failure.
170pub mod error;
171/// XML namespace definitions used through XMPP.
172pub mod ns;
173
174#[cfg(test)]
175/// Namespace-aware comparison for tests
176mod compare_elements;
177
178/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
179pub mod message;
180/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
181pub mod presence;
182/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
183pub mod iq;
184/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
185pub mod stanza_error;
186
187/// RFC 6121: Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence
188pub mod roster;
189
190/// XEP-0004: Data Forms
191pub mod data_forms;
192
193/// XEP-0030: Service Discovery
194pub mod disco;
195
196/// XEP-0045: Multi-User Chat
197pub mod muc;
198
199/// XEP-0047: In-Band Bytestreams
200pub mod ibb;
201
202/// XEP-0059: Result Set Management
203pub mod rsm;
204
205/// XEP-0060: Publish-Subscribe
206pub mod pubsub;
207
208/// XEP-0077: In-Band Registration
209pub mod ibr;
210
211/// XEP-0085: Chat State Notifications
212pub mod chatstates;
213
214/// XEP-0092: Software Version
215pub mod version;
216
217/// XEP-0115: Entity Capabilities
218pub mod caps;
219
220/// XEP-0166: Jingle
221pub mod jingle;
222
223/// XEP-0184: Message Delivery Receipts
224pub mod receipts;
225
226/// XEP-0199: XMPP Ping
227pub mod ping;
228
229/// XEP-0203: Delayed Delivery
230pub mod delay;
231
232/// XEP-0221: Data Forms Media Element
233pub mod media_element;
234
235/// XEP-0224: Attention
236pub mod attention;
237
238/// XEP-0234: Jingle File Transfer
239pub mod jingle_ft;
240
241/// XEP-0260: Jingle SOCKS5 Bytestreams Transport Method
242pub mod jingle_s5b;
243
244/// XEP-0261: Jingle In-Band Bytestreams Transport Method
245pub mod jingle_ibb;
246
247/// XEP-0297: Stanza Forwarding
248pub mod forwarding;
249
250/// XEP-0300: Use of Cryptographic Hash Functions in XMPP
251pub mod hashes;
252
253/// XEP-0308: Last Message Correction
254pub mod message_correct;
255
256/// XEP-0313: Message Archive Management
257pub mod mam;
258
259/// XEP-0319: Last User Interaction in Presence
260pub mod idle;
261
262/// XEP-0353: Jingle Message Initiation
263pub mod jingle_message;
264
265/// XEP-0359: Unique and Stable Stanza IDs
266pub mod stanza_id;
267
268/// XEP-0380: Explicit Message Encryption
269pub mod eme;
270
271/// XEP-0390: Entity Capabilities 2.0
272pub mod ecaps2;