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