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 crate::compare_elements::NamespaceAwareCompare;
76
77 #[cfg(target_pointer_width = "32")]
78 #[test]
79 fn test_size() {
80 assert_size!(Conference, 76);
81 assert_size!(Url, 24);
82 assert_size!(Storage, 24);
83 }
84
85 #[cfg(target_pointer_width = "64")]
86 #[test]
87 fn test_size() {
88 assert_size!(Conference, 152);
89 assert_size!(Url, 48);
90 assert_size!(Storage, 48);
91 }
92
93 #[test]
94 fn empty() {
95 let elem: Element = "<storage xmlns='storage:bookmarks'/>".parse().unwrap();
96 let elem1 = elem.clone();
97 let storage = Storage::try_from(elem).unwrap();
98 assert_eq!(storage.conferences.len(), 0);
99 assert_eq!(storage.urls.len(), 0);
100
101 let elem2 = Element::from(Storage::new());
102 assert!(elem1.compare_to(&elem2));
103 }
104
105 #[test]
106 fn complete() {
107 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();
108 let storage = Storage::try_from(elem).unwrap();
109 assert_eq!(storage.urls.len(), 1);
110 assert_eq!(storage.urls[0].name, "Example");
111 assert_eq!(storage.urls[0].url, "https://example.org/");
112 assert_eq!(storage.conferences.len(), 1);
113 assert_eq!(storage.conferences[0].autojoin, Autojoin::True);
114 assert_eq!(storage.conferences[0].jid, Jid::bare("test-muc", "muc.localhost"));
115 assert_eq!(storage.conferences[0].name, "Test MUC");
116 assert_eq!(storage.conferences[0].clone().nick.unwrap(), "Coucou");
117 assert_eq!(storage.conferences[0].clone().password.unwrap(), "secret");
118 }
119}