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_attribute!(
10 /// Whether a conference bookmark should be joined automatically.
11 Autojoin, "autojoin", bool
12);
13
14generate_element!(
15 /// A conference bookmark.
16 Conference, "conference", BOOKMARKS,
17 attributes: [
18 /// Whether a conference bookmark should be joined automatically.
19 autojoin: Autojoin = "autojoin" => default,
20
21 /// The JID of the conference.
22 jid: Jid = "jid" => required,
23
24 /// A user-defined name for this conference.
25 name: String = "name" => required
26 ],
27 children: [
28 /// The nick the user will use to join this conference.
29 nick: Option<String> = ("nick", BOOKMARKS) => String,
30
31 /// The password required to join this conference.
32 password: Option<String> = ("password", BOOKMARKS) => String
33 ]
34);
35
36generate_element!(
37 /// An URL bookmark.
38 Url, "url", BOOKMARKS,
39 attributes: [
40 /// A user-defined name for this URL.
41 name: String = "name" => required,
42
43 /// The URL of this bookmark.
44 url: String = "url" => required
45 ]
46);
47
48generate_element!(
49 /// Container element for multiple bookmarks.
50 Storage, "storage", BOOKMARKS,
51 children: [
52 /// Conferences the user has expressed an interest in.
53 conferences: Vec<Conference> = ("conference", BOOKMARKS) => Conference,
54
55 /// URLs the user is interested in.
56 urls: Vec<Url> = ("url", BOOKMARKS) => Url
57 ]
58);
59
60impl Storage {
61 /// Create an empty bookmarks storage.
62 pub fn new() -> Storage {
63 Storage {
64 conferences: Vec::new(),
65 urls: Vec::new(),
66 }
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73 use try_from::TryFrom;
74 use minidom::Element;
75 use compare_elements::NamespaceAwareCompare;
76
77 #[test]
78 fn empty() {
79 let elem: Element = "<storage xmlns='storage:bookmarks'/>".parse().unwrap();
80 let elem1 = elem.clone();
81 let storage = Storage::try_from(elem).unwrap();
82 assert_eq!(storage.conferences.len(), 0);
83 assert_eq!(storage.urls.len(), 0);
84
85 let elem2 = Element::from(Storage::new());
86 assert!(elem1.compare_to(&elem2));
87 }
88
89 #[test]
90 fn complete() {
91 let elem: Element = "<storage xmlns='storage:bookmarks'><url name='Example' url='https://example.org/'/><conference autojoin='true' jid='test-muc@muc.localhost' name='Test MUC'><nick>Coucou</nick><password>secret</password></conference></storage>".parse().unwrap();
92 let storage = Storage::try_from(elem).unwrap();
93 assert_eq!(storage.urls.len(), 1);
94 assert_eq!(storage.urls[0].name, "Example");
95 assert_eq!(storage.urls[0].url, "https://example.org/");
96 assert_eq!(storage.conferences.len(), 1);
97 assert_eq!(storage.conferences[0].autojoin, Autojoin::True);
98 assert_eq!(storage.conferences[0].jid, Jid::bare("test-muc", "muc.localhost"));
99 assert_eq!(storage.conferences[0].name, "Test MUC");
100 assert_eq!(storage.conferences[0].clone().nick.unwrap(), "Coucou");
101 assert_eq!(storage.conferences[0].clone().password.unwrap(), "secret");
102 }
103}