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