bookmarks.rs

 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#![deny(missing_docs)]
 8
 9use jid::Jid;
10
11generate_attribute!(
12    /// Whether a conference bookmark should be joined automatically.
13    Autojoin, "autojoin", 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: Autojoin = "autojoin" => default,
22
23        /// The JID of the conference.
24        jid: Jid = "jid" => required,
25
26        /// A user-defined name for this conference.
27        name: String = "name" => required
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: String = "name" => required,
44
45        /// The URL of this bookmark.
46        url: String = "url" => required
47    ]
48);
49
50generate_element!(
51    /// Container element for multiple bookmarks.
52    Storage, "storage", BOOKMARKS,
53    children: [
54        /// Conferences the user has expressed an interest in.
55        conferences: Vec<Conference> = ("conference", BOOKMARKS) => Conference,
56
57        /// URLs the user is interested in.
58        urls: Vec<Url> = ("url", BOOKMARKS) => Url
59    ]
60);
61
62impl Storage {
63    /// Create an empty bookmarks storage.
64    pub fn new() -> Storage {
65        Storage {
66            conferences: Vec::new(),
67            urls: Vec::new(),
68        }
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75    use try_from::TryFrom;
76    use minidom::Element;
77    use error::Error;
78    use compare_elements::NamespaceAwareCompare;
79
80    #[test]
81    fn empty() {
82        let elem: Element = "<storage xmlns='storage:bookmarks'/>".parse().unwrap();
83        let elem1 = elem.clone();
84        let storage = Storage::try_from(elem).unwrap();
85        assert_eq!(storage.conferences.len(), 0);
86        assert_eq!(storage.urls.len(), 0);
87
88        let elem2 = Element::from(Storage::new());
89        assert!(elem1.compare_to(&elem2));
90    }
91}