Handle extensions element in bookmarks2
This commit is contained in:
parent
76db2ae117
commit
20a94117d3
1 changed files with 97 additions and 21 deletions
|
|
@ -3,6 +3,10 @@
|
||||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
// 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
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
use crate::ns;
|
||||||
|
use crate::util::error::Error;
|
||||||
|
use crate::Element;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
generate_attribute!(
|
generate_attribute!(
|
||||||
/// Whether a conference bookmark should be joined automatically.
|
/// Whether a conference bookmark should be joined automatically.
|
||||||
|
|
@ -11,24 +15,24 @@ generate_attribute!(
|
||||||
bool
|
bool
|
||||||
);
|
);
|
||||||
|
|
||||||
generate_element!(
|
|
||||||
/// A conference bookmark.
|
/// A conference bookmark.
|
||||||
Conference, "conference", BOOKMARKS2,
|
#[derive(Debug, Clone)]
|
||||||
attributes: [
|
pub struct Conference {
|
||||||
/// Whether a conference bookmark should be joined automatically.
|
/// Whether a conference bookmark should be joined automatically.
|
||||||
autojoin: Default<Autojoin> = "autojoin",
|
pub autojoin: Autojoin,
|
||||||
|
|
||||||
/// A user-defined name for this conference.
|
/// A user-defined name for this conference.
|
||||||
name: Option<String> = "name",
|
pub name: Option<String>,
|
||||||
],
|
|
||||||
children: [
|
|
||||||
/// The nick the user will use to join this conference.
|
/// The nick the user will use to join this conference.
|
||||||
nick: Option<String> = ("nick", BOOKMARKS2) => String,
|
pub nick: Option<String>,
|
||||||
|
|
||||||
/// The password required to join this conference.
|
/// The password required to join this conference.
|
||||||
password: Option<String> = ("password", BOOKMARKS2) => String
|
pub password: Option<String>,
|
||||||
]
|
|
||||||
);
|
/// Extensions elements.
|
||||||
|
pub extensions: Vec<Element>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Conference {
|
impl Conference {
|
||||||
/// Create a new conference.
|
/// Create a new conference.
|
||||||
|
|
@ -38,10 +42,77 @@ impl Conference {
|
||||||
name: None,
|
name: None,
|
||||||
nick: None,
|
nick: None,
|
||||||
password: None,
|
password: None,
|
||||||
|
extensions: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFrom<Element> for Conference {
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn try_from(root: Element) -> Result<Conference, Error> {
|
||||||
|
check_self!(root, "conference", BOOKMARKS2, "Conference");
|
||||||
|
check_no_unknown_attributes!(root, "Conference", ["autojoin", "name"]);
|
||||||
|
|
||||||
|
let mut conference = Conference {
|
||||||
|
autojoin: get_attr!(root, "autojoin", Default),
|
||||||
|
name: get_attr!(root, "name", Option),
|
||||||
|
nick: None,
|
||||||
|
password: None,
|
||||||
|
extensions: vec![],
|
||||||
|
};
|
||||||
|
|
||||||
|
for child in root.children().cloned() {
|
||||||
|
if child.is("extensions", ns::BOOKMARKS2) {
|
||||||
|
if conference.extensions.len() > 0 {
|
||||||
|
return Err(Error::ParseError(
|
||||||
|
"Conference must not have more than one extensions element.",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
conference.extensions.extend(child.children().cloned());
|
||||||
|
} else if child.is("nick", ns::BOOKMARKS2) {
|
||||||
|
if conference.nick.is_some() {
|
||||||
|
return Err(Error::ParseError(
|
||||||
|
"Conference must not have more than one nick.",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
check_no_children!(child, "nick");
|
||||||
|
conference.nick = Some(child.text());
|
||||||
|
} else if child.is("password", ns::BOOKMARKS2) {
|
||||||
|
if conference.password.is_some() {
|
||||||
|
return Err(Error::ParseError(
|
||||||
|
"Conference must not have more than one password.",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
check_no_children!(child, "password");
|
||||||
|
conference.password = Some(child.text());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(conference)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Conference> for Element {
|
||||||
|
fn from(conference: Conference) -> Element {
|
||||||
|
Element::builder("conference", ns::BOOKMARKS2)
|
||||||
|
.attr("autojoin", conference.autojoin)
|
||||||
|
.attr("name", conference.name)
|
||||||
|
.append_all(
|
||||||
|
conference
|
||||||
|
.nick
|
||||||
|
.map(|nick| Element::builder("nick", ns::BOOKMARKS2).append(nick)),
|
||||||
|
)
|
||||||
|
.append_all(
|
||||||
|
conference
|
||||||
|
.password
|
||||||
|
.map(|password| Element::builder("password", ns::BOOKMARKS2).append(password)),
|
||||||
|
)
|
||||||
|
.append_all(conference.extensions)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
@ -52,13 +123,13 @@ mod tests {
|
||||||
#[cfg(target_pointer_width = "32")]
|
#[cfg(target_pointer_width = "32")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_size() {
|
fn test_size() {
|
||||||
assert_size!(Conference, 40);
|
assert_size!(Conference, 52);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_pointer_width = "64")]
|
#[cfg(target_pointer_width = "64")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_size() {
|
fn test_size() {
|
||||||
assert_size!(Conference, 80);
|
assert_size!(Conference, 104);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -79,12 +150,17 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn complete() {
|
fn complete() {
|
||||||
let elem: Element = "<conference xmlns='urn:xmpp:bookmarks:1' autojoin='true' name='Test MUC'><nick>Coucou</nick><password>secret</password></conference>".parse().unwrap();
|
let elem: Element = "<conference xmlns='urn:xmpp:bookmarks:1' autojoin='true' name='Test MUC'><nick>Coucou</nick><password>secret</password><extensions><test xmlns='urn:xmpp:unknown' /></extensions></conference>".parse().unwrap();
|
||||||
let conference = Conference::try_from(elem).unwrap();
|
let conference = Conference::try_from(elem).unwrap();
|
||||||
assert_eq!(conference.autojoin, Autojoin::True);
|
assert_eq!(conference.autojoin, Autojoin::True);
|
||||||
assert_eq!(conference.name, Some(String::from("Test MUC")));
|
assert_eq!(conference.name, Some(String::from("Test MUC")));
|
||||||
assert_eq!(conference.clone().nick.unwrap(), "Coucou");
|
assert_eq!(conference.clone().nick.unwrap(), "Coucou");
|
||||||
assert_eq!(conference.clone().password.unwrap(), "secret");
|
assert_eq!(conference.clone().password.unwrap(), "secret");
|
||||||
|
assert_eq!(conference.clone().extensions.len(), 1);
|
||||||
|
assert_eq!(
|
||||||
|
conference.clone().extensions[0],
|
||||||
|
Element::builder("test", "urn:xmpp:unknown").build()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue