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