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