1// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
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
7#![allow(missing_docs)]
8
9use jid::Jid;
10
11generate_element!(Stream, "stream", STREAM,
12attributes: [
13 from: Option<Jid> = "from" => optional,
14 to: Option<Jid> = "to" => optional,
15 id: Option<String> = "id" => optional,
16 version: Option<String> = "version" => optional,
17 xml_lang: Option<String> = "xml:lang" => optional,
18]);
19
20impl Stream {
21 pub fn new(to: Jid) -> Stream {
22 Stream {
23 from: None,
24 to: Some(to),
25 id: None,
26 version: Some(String::from("1.0")),
27 xml_lang: None,
28 }
29 }
30
31 pub fn with_from(mut self, from: Jid) -> Stream {
32 self.from = Some(from);
33 self
34 }
35
36 pub fn with_id(mut self, id: String) -> Stream {
37 self.id = Some(id);
38 self
39 }
40
41 pub fn with_lang(mut self, xml_lang: String) -> Stream {
42 self.xml_lang = Some(xml_lang);
43 self
44 }
45
46 pub fn is_version(&self, version: &str) -> bool {
47 match self.version {
48 None => false,
49 Some(ref self_version) => self_version == &String::from(version),
50 }
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57 use try_from::TryFrom;
58 use minidom::Element;
59
60 #[test]
61 fn test_simple() {
62 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();
63 let stream = Stream::try_from(elem).unwrap();
64 assert_eq!(stream.from, Some(Jid::domain("some-server.example")));
65 assert_eq!(stream.to, None);
66 assert_eq!(stream.id, Some(String::from("abc")));
67 assert_eq!(stream.version, Some(String::from("1.0")));
68 assert_eq!(stream.xml_lang, Some(String::from("en")));
69 }
70}