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