Add a <stream:stream> parser.

This commit is contained in:
Emmanuel Gil Peyrot 2018-03-01 10:08:30 +01:00
parent 3de29e1e81
commit 395b64e644
3 changed files with 74 additions and 0 deletions

View file

@ -56,6 +56,8 @@ pub mod iq;
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
pub mod stanza_error; pub mod stanza_error;
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
pub mod stream;
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
pub mod sasl; pub mod sasl;
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
pub mod bind; pub mod bind;

View file

@ -10,6 +10,8 @@ pub const JABBER_CLIENT: &str = "jabber:client";
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
pub const XMPP_STANZAS: &str = "urn:ietf:params:xml:ns:xmpp-stanzas"; pub const XMPP_STANZAS: &str = "urn:ietf:params:xml:ns:xmpp-stanzas";
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
pub const STREAM: &str = "http://etherx.jabber.org/streams";
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
pub const SASL: &str = "urn:ietf:params:xml:ns:xmpp-sasl"; pub const SASL: &str = "urn:ietf:params:xml:ns:xmpp-sasl";
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core /// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
pub const BIND: &str = "urn:ietf:params:xml:ns:xmpp-bind"; pub const BIND: &str = "urn:ietf:params:xml:ns:xmpp-bind";

70
src/stream.rs Normal file
View file

@ -0,0 +1,70 @@
// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use try_from::TryFrom;
use minidom::Element;
use jid::Jid;
use error::Error;
use ns;
generate_element_with_only_attributes!(Stream, "stream", ns::STREAM, [
from: Option<Jid> = "from" => optional,
to: Option<Jid> = "to" => optional,
id: Option<String> = "id" => optional,
version: Option<String> = "version" => optional,
xml_lang: Option<String> = "xml:lang" => optional,
]);
impl Stream {
pub fn new(to: Jid) -> Stream {
Stream {
from: None,
to: Some(to),
id: None,
version: Some(String::from("1.0")),
xml_lang: None,
}
}
pub fn with_from(mut self, from: Jid) -> Stream {
self.from = Some(from);
self
}
pub fn with_id(mut self, id: String) -> Stream {
self.id = Some(id);
self
}
pub fn with_lang(mut self, xml_lang: String) -> Stream {
self.xml_lang = Some(xml_lang);
self
}
pub fn is_version(&self, version: &str) -> bool {
match self.version {
None => false,
Some(ref self_version) => self_version == &String::from(version),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple() {
let elem: Element = "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' xml:lang='en' version='1.0' id='abc' from='some-server.example'/>".parse().unwrap();
let stream = Stream::try_from(elem).unwrap();
assert_eq!(stream.from, Some(Jid::domain("some-server.example")));
assert_eq!(stream.to, None);
assert_eq!(stream.id, Some(String::from("abc")));
assert_eq!(stream.version, Some(String::from("1.0")));
assert_eq!(stream.xml_lang, Some(String::from("en")));
}
}