From 33ac338e3ea7a359169f74e44208e31ec470f180 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 29 May 2018 15:04:16 +0200 Subject: [PATCH] bookmarks: Add an implementation of XEP-0048. --- src/bookmarks.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 ++ src/ns.rs | 3 ++ 3 files changed, 97 insertions(+) create mode 100644 src/bookmarks.rs diff --git a/src/bookmarks.rs b/src/bookmarks.rs new file mode 100644 index 00000000..4c449faf --- /dev/null +++ b/src/bookmarks.rs @@ -0,0 +1,91 @@ +// Copyright (c) 2018 Emmanuel Gil Peyrot +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#![deny(missing_docs)] + +use jid::Jid; + +generate_attribute!( + /// Whether a conference bookmark should be joined automatically. + Autojoin, "autojoin", bool +); + +generate_element!( + /// A conference bookmark. + Conference, "conference", BOOKMARKS, + attributes: [ + /// Whether a conference bookmark should be joined automatically. + autojoin: Autojoin = "autojoin" => default, + + /// The JID of the conference. + jid: Jid = "jid" => required, + + /// A user-defined name for this conference. + name: String = "name" => required + ], + children: [ + /// The nick the user will use to join this conference. + nick: Option = ("nick", BOOKMARKS) => String, + + /// The password required to join this conference. + password: Option = ("password", BOOKMARKS) => String + ] +); + +generate_element!( + /// An URL bookmark. + Url, "url", BOOKMARKS, + attributes: [ + /// A user-defined name for this URL. + name: String = "name" => required, + + /// The URL of this bookmark. + url: String = "url" => required + ] +); + +generate_element!( + /// Container element for multiple bookmarks. + Storage, "storage", BOOKMARKS, + children: [ + /// Conferences the user has expressed an interest in. + conferences: Vec = ("conference", BOOKMARKS) => Conference, + + /// URLs the user is interested in. + urls: Vec = ("url", BOOKMARKS) => Url + ] +); + +impl Storage { + /// Create an empty bookmarks storage. + pub fn new() -> Storage { + Storage { + conferences: Vec::new(), + urls: Vec::new(), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use try_from::TryFrom; + use minidom::Element; + use error::Error; + use compare_elements::NamespaceAwareCompare; + + #[test] + fn empty() { + let elem: Element = "".parse().unwrap(); + let elem1 = elem.clone(); + let storage = Storage::try_from(elem).unwrap(); + assert_eq!(storage.conferences.len(), 0); + assert_eq!(storage.urls.len(), 0); + + let elem2 = Element::from(Storage::new()); + assert!(elem1.compare_to(&elem2)); + } +} diff --git a/src/lib.rs b/src/lib.rs index 5fe91b18..82094267 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,6 +83,9 @@ pub mod muc; /// XEP-0047: In-Band Bytestreams pub mod ibb; +/// XEP-0048: Bookmarks +pub mod bookmarks; + /// XEP-0059: Result Set Management pub mod rsm; diff --git a/src/ns.rs b/src/ns.rs index e5f79fb5..b561e56f 100644 --- a/src/ns.rs +++ b/src/ns.rs @@ -38,6 +38,9 @@ pub const MUC_USER: &str = "http://jabber.org/protocol/muc#user"; /// XEP-0047: In-Band Bytestreams pub const IBB: &str = "http://jabber.org/protocol/ibb"; +/// XEP-0048: Bookmarks +pub const BOOKMARKS: &str = "storage:bookmarks"; + /// XEP-0059: Result Set Management pub const RSM: &str = "http://jabber.org/protocol/rsm";