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::BareJid;
8
9generate_attribute!(
10 /// Whether a conference bookmark should be joined automatically.
11 Autojoin,
12 "autojoin",
13 bool
14);
15
16generate_element!(
17 /// A conference bookmark.
18 Conference, "conference", BOOKMARKS,
19 attributes: [
20 /// Whether a conference bookmark should be joined automatically.
21 autojoin: Default<Autojoin> = "autojoin",
22
23 /// The JID of the conference.
24 jid: Required<BareJid> = "jid",
25
26 /// A user-defined name for this conference.
27 name: Required<String> = "name",
28 ],
29 children: [
30 /// The nick the user will use to join this conference.
31 nick: Option<String> = ("nick", BOOKMARKS) => String,
32
33 /// The password required to join this conference.
34 password: Option<String> = ("password", BOOKMARKS) => String
35 ]
36);
37
38generate_element!(
39 /// An URL bookmark.
40 Url, "url", BOOKMARKS,
41 attributes: [
42 /// A user-defined name for this URL.
43 name: Required<String> = "name",
44
45 /// The URL of this bookmark.
46 url: Required<String> = "url",
47 ]
48);
49
50generate_element!(
51 /// Container element for multiple bookmarks.
52 #[derive(Default)]
53 Storage, "storage", BOOKMARKS,
54 children: [
55 /// Conferences the user has expressed an interest in.
56 conferences: Vec<Conference> = ("conference", BOOKMARKS) => Conference,
57
58 /// URLs the user is interested in.
59 urls: Vec<Url> = ("url", BOOKMARKS) => Url
60 ]
61);
62
63impl Storage {
64 /// Create an empty bookmarks storage.
65 pub fn new() -> Storage {
66 Storage::default()
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73 use crate::util::compare_elements::NamespaceAwareCompare;
74 use minidom::Element;
75 use std::convert::TryFrom;
76
77 #[cfg(target_pointer_width = "32")]
78 #[test]
79 fn test_size() {
80 assert_size!(Conference, 64);
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, 128);
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!(
115 storage.conferences[0].jid,
116 BareJid::new("test-muc", "muc.localhost")
117 );
118 assert_eq!(storage.conferences[0].name, "Test MUC");
119 assert_eq!(storage.conferences[0].clone().nick.unwrap(), "Coucou");
120 assert_eq!(storage.conferences[0].clone().password.unwrap(), "secret");
121 }
122}