2017-04-29 21:14:34 +00:00
|
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
|
//
|
|
|
|
|
// 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/.
|
|
|
|
|
|
2018-08-08 16:48:05 +00:00
|
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
|
2017-07-20 19:03:15 +00:00
|
|
|
|
use try_from::TryFrom;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
2018-05-14 14:12:56 +00:00
|
|
|
|
use minidom::Element;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
|
|
|
|
use error::Error;
|
2017-04-20 22:16:12 +00:00
|
|
|
|
use ns;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
2017-05-06 19:48:41 +00:00
|
|
|
|
use media_element::MediaElement;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
2018-05-28 15:05:04 +00:00
|
|
|
|
generate_element!(
|
|
|
|
|
Option_, "option", DATA_FORMS,
|
|
|
|
|
attributes: [
|
|
|
|
|
label: Option<String> = "label" => optional
|
|
|
|
|
],
|
|
|
|
|
children: [
|
|
|
|
|
value: Required<String> = ("value", DATA_FORMS) => String
|
|
|
|
|
]
|
|
|
|
|
);
|
2017-05-25 00:00:17 +00:00
|
|
|
|
|
2017-10-10 17:26:05 +00:00
|
|
|
|
generate_attribute!(FieldType, "type", {
|
|
|
|
|
Boolean => "boolean",
|
|
|
|
|
Fixed => "fixed",
|
|
|
|
|
Hidden => "hidden",
|
|
|
|
|
JidMulti => "jid-multi",
|
|
|
|
|
JidSingle => "jid-single",
|
|
|
|
|
ListMulti => "list-multi",
|
|
|
|
|
ListSingle => "list-single",
|
|
|
|
|
TextMulti => "text-multi",
|
|
|
|
|
TextPrivate => "text-private",
|
|
|
|
|
TextSingle => "text-single",
|
|
|
|
|
}, Default = TextSingle);
|
|
|
|
|
|
2017-04-20 23:41:15 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
2017-04-18 19:44:36 +00:00
|
|
|
|
pub struct Field {
|
|
|
|
|
pub var: String,
|
2017-05-21 16:08:05 +00:00
|
|
|
|
pub type_: FieldType,
|
2017-04-18 19:44:36 +00:00
|
|
|
|
pub label: Option<String>,
|
2017-05-21 15:41:29 +00:00
|
|
|
|
pub required: bool,
|
|
|
|
|
pub options: Vec<Option_>,
|
2017-04-18 19:44:36 +00:00
|
|
|
|
pub values: Vec<String>,
|
|
|
|
|
pub media: Vec<MediaElement>,
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-29 03:19:58 +00:00
|
|
|
|
impl Field {
|
|
|
|
|
fn is_list(&self) -> bool {
|
|
|
|
|
self.type_ == FieldType::ListSingle ||
|
|
|
|
|
self.type_ == FieldType::ListMulti
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<Element> for Field {
|
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Field, Error> {
|
2018-05-14 14:30:28 +00:00
|
|
|
|
check_self!(elem, "field", DATA_FORMS);
|
2017-10-10 17:26:05 +00:00
|
|
|
|
check_no_unknown_attributes!(elem, "field", ["label", "type", "var"]);
|
2017-07-29 03:19:58 +00:00
|
|
|
|
let mut field = Field {
|
|
|
|
|
var: get_attr!(elem, "var", required),
|
|
|
|
|
type_: get_attr!(elem, "type", default),
|
|
|
|
|
label: get_attr!(elem, "label", optional),
|
|
|
|
|
required: false,
|
|
|
|
|
options: vec!(),
|
|
|
|
|
values: vec!(),
|
|
|
|
|
media: vec!(),
|
|
|
|
|
};
|
|
|
|
|
for element in elem.children() {
|
|
|
|
|
if element.is("value", ns::DATA_FORMS) {
|
2017-10-10 17:26:05 +00:00
|
|
|
|
check_no_children!(element, "value");
|
2017-10-31 15:47:38 +00:00
|
|
|
|
check_no_attributes!(element, "value");
|
2017-07-29 03:19:58 +00:00
|
|
|
|
field.values.push(element.text());
|
|
|
|
|
} else if element.is("required", ns::DATA_FORMS) {
|
|
|
|
|
if field.required {
|
|
|
|
|
return Err(Error::ParseError("More than one required element."));
|
|
|
|
|
}
|
2017-10-10 17:26:05 +00:00
|
|
|
|
check_no_children!(element, "required");
|
2017-10-31 15:47:38 +00:00
|
|
|
|
check_no_attributes!(element, "required");
|
2017-07-29 03:19:58 +00:00
|
|
|
|
field.required = true;
|
|
|
|
|
} else if element.is("option", ns::DATA_FORMS) {
|
|
|
|
|
if !field.is_list() {
|
|
|
|
|
return Err(Error::ParseError("Option element found in non-list field."));
|
|
|
|
|
}
|
2017-10-10 17:26:05 +00:00
|
|
|
|
let option = Option_::try_from(element.clone())?;
|
|
|
|
|
field.options.push(option);
|
2017-07-29 03:19:58 +00:00
|
|
|
|
} else if element.is("media", ns::MEDIA_ELEMENT) {
|
|
|
|
|
let media_element = MediaElement::try_from(element.clone())?;
|
|
|
|
|
field.media.push(media_element);
|
|
|
|
|
} else {
|
2017-10-10 17:26:05 +00:00
|
|
|
|
return Err(Error::ParseError("Field child isn’t a value, option or media element."));
|
2017-07-29 03:19:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(field)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-25 00:00:17 +00:00
|
|
|
|
impl From<Field> for Element {
|
|
|
|
|
fn from(field: Field) -> Element {
|
|
|
|
|
Element::builder("field")
|
|
|
|
|
.ns(ns::DATA_FORMS)
|
|
|
|
|
.attr("var", field.var)
|
|
|
|
|
.attr("type", field.type_)
|
|
|
|
|
.attr("label", field.label)
|
|
|
|
|
.append(if field.required { Some(Element::builder("required").ns(ns::DATA_FORMS).build()) } else { None })
|
|
|
|
|
.append(field.options)
|
2017-07-20 22:09:22 +00:00
|
|
|
|
.append(field.values.into_iter().map(|value| {
|
2017-05-25 00:00:17 +00:00
|
|
|
|
Element::builder("value").ns(ns::DATA_FORMS).append(value).build()
|
|
|
|
|
}).collect::<Vec<_>>())
|
|
|
|
|
.append(field.media)
|
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-14 00:57:02 +00:00
|
|
|
|
generate_attribute!(DataFormType, "type", {
|
2017-06-13 23:50:57 +00:00
|
|
|
|
Cancel => "cancel",
|
|
|
|
|
Form => "form",
|
|
|
|
|
Result_ => "result",
|
|
|
|
|
Submit => "submit",
|
|
|
|
|
});
|
2017-05-25 00:00:17 +00:00
|
|
|
|
|
2017-04-20 23:41:15 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
2017-04-18 19:44:36 +00:00
|
|
|
|
pub struct DataForm {
|
|
|
|
|
pub type_: DataFormType,
|
|
|
|
|
pub form_type: Option<String>,
|
2017-05-21 15:41:29 +00:00
|
|
|
|
pub title: Option<String>,
|
|
|
|
|
pub instructions: Option<String>,
|
2017-04-18 19:44:36 +00:00
|
|
|
|
pub fields: Vec<Field>,
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
impl TryFrom<Element> for DataForm {
|
2017-07-20 19:03:15 +00:00
|
|
|
|
type Err = Error;
|
2017-05-06 19:51:39 +00:00
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
fn try_from(elem: Element) -> Result<DataForm, Error> {
|
2018-05-14 14:30:28 +00:00
|
|
|
|
check_self!(elem, "x", DATA_FORMS);
|
2017-10-10 17:26:05 +00:00
|
|
|
|
check_no_unknown_attributes!(elem, "x", ["type"]);
|
2017-05-21 15:41:29 +00:00
|
|
|
|
let type_ = get_attr!(elem, "type", required);
|
|
|
|
|
let mut form = DataForm {
|
|
|
|
|
type_: type_,
|
|
|
|
|
form_type: None,
|
|
|
|
|
title: None,
|
|
|
|
|
instructions: None,
|
|
|
|
|
fields: vec!(),
|
2017-05-06 19:51:39 +00:00
|
|
|
|
};
|
2017-05-21 15:41:29 +00:00
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if child.is("title", ns::DATA_FORMS) {
|
|
|
|
|
if form.title.is_some() {
|
|
|
|
|
return Err(Error::ParseError("More than one title in form element."));
|
|
|
|
|
}
|
2017-10-10 17:26:05 +00:00
|
|
|
|
check_no_children!(child, "title");
|
2017-10-31 15:47:38 +00:00
|
|
|
|
check_no_attributes!(child, "title");
|
2017-05-21 15:41:29 +00:00
|
|
|
|
form.title = Some(child.text());
|
|
|
|
|
} else if child.is("instructions", ns::DATA_FORMS) {
|
|
|
|
|
if form.instructions.is_some() {
|
|
|
|
|
return Err(Error::ParseError("More than one instructions in form element."));
|
|
|
|
|
}
|
2017-10-10 17:26:05 +00:00
|
|
|
|
check_no_children!(child, "instructions");
|
2017-10-31 15:47:38 +00:00
|
|
|
|
check_no_attributes!(child, "instructions");
|
2017-05-21 15:41:29 +00:00
|
|
|
|
form.instructions = Some(child.text());
|
|
|
|
|
} else if child.is("field", ns::DATA_FORMS) {
|
2017-07-29 03:19:58 +00:00
|
|
|
|
let field = Field::try_from(child.clone())?;
|
|
|
|
|
if field.var == "FORM_TYPE" && field.type_ == FieldType::Hidden {
|
2017-05-21 15:41:29 +00:00
|
|
|
|
if form.form_type.is_some() {
|
2017-05-06 19:51:39 +00:00
|
|
|
|
return Err(Error::ParseError("More than one FORM_TYPE in a data form."));
|
|
|
|
|
}
|
2017-05-21 15:41:29 +00:00
|
|
|
|
if field.values.len() != 1 {
|
2017-05-06 19:51:39 +00:00
|
|
|
|
return Err(Error::ParseError("Wrong number of values in FORM_TYPE."));
|
|
|
|
|
}
|
2017-05-21 15:41:29 +00:00
|
|
|
|
form.form_type = Some(field.values[0].clone());
|
2017-04-18 19:44:36 +00:00
|
|
|
|
}
|
2017-05-21 15:41:29 +00:00
|
|
|
|
form.fields.push(field);
|
2017-05-06 19:51:39 +00:00
|
|
|
|
} else {
|
2017-05-21 15:41:29 +00:00
|
|
|
|
return Err(Error::ParseError("Unknown child in data form element."));
|
2017-04-18 19:44:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-21 15:41:29 +00:00
|
|
|
|
Ok(form)
|
2017-04-18 19:44:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-25 00:00:17 +00:00
|
|
|
|
impl From<DataForm> for Element {
|
|
|
|
|
fn from(form: DataForm) -> Element {
|
|
|
|
|
Element::builder("x")
|
|
|
|
|
.ns(ns::DATA_FORMS)
|
|
|
|
|
.attr("type", form.type_)
|
2017-10-31 23:33:34 +00:00
|
|
|
|
.append(form.title.map(|title| Element::builder("title").ns(ns::DATA_FORMS).append(title)))
|
|
|
|
|
.append(form.instructions.map(|text| Element::builder("instructions").ns(ns::DATA_FORMS).append(text)))
|
2017-05-25 00:00:17 +00:00
|
|
|
|
.append(form.fields)
|
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-18 19:44:36 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2017-05-06 19:51:39 +00:00
|
|
|
|
use super::*;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_simple() {
|
|
|
|
|
let elem: Element = "<x xmlns='jabber:x:data' type='result'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let form = DataForm::try_from(elem).unwrap();
|
2017-05-06 19:51:39 +00:00
|
|
|
|
assert_eq!(form.type_, DataFormType::Result_);
|
2017-04-18 19:44:36 +00:00
|
|
|
|
assert!(form.form_type.is_none());
|
|
|
|
|
assert!(form.fields.is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid() {
|
|
|
|
|
let elem: Element = "<x xmlns='jabber:x:data'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = DataForm::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-05-21 15:41:29 +00:00
|
|
|
|
assert_eq!(message, "Required attribute 'type' missing.");
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = "<x xmlns='jabber:x:data' type='coucou'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = DataForm::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-06-14 00:57:02 +00:00
|
|
|
|
assert_eq!(message, "Unknown value for 'type' attribute.");
|
2017-04-18 19:44:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_wrong_child() {
|
|
|
|
|
let elem: Element = "<x xmlns='jabber:x:data' type='cancel'><coucou/></x>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = DataForm::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-05-21 15:41:29 +00:00
|
|
|
|
assert_eq!(message, "Unknown child in data form element.");
|
2017-04-18 19:44:36 +00:00
|
|
|
|
}
|
2018-05-28 15:05:04 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn option() {
|
|
|
|
|
let elem: Element = "<option xmlns='jabber:x:data' label='Coucou !'><value>coucou</value></option>".parse().unwrap();
|
|
|
|
|
let option = Option_::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(&option.label.unwrap(), "Coucou !");
|
|
|
|
|
assert_eq!(&option.value, "coucou");
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<option xmlns='jabber:x:data' label='Coucou !'/>".parse().unwrap();
|
|
|
|
|
let error = Option_::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Missing child value in option element.");
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<option xmlns='jabber:x:data' label='Coucou !'><value>coucou</value><value>error</value></option>".parse().unwrap();
|
|
|
|
|
let error = Option_::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Element option must not have more than one value child.");
|
|
|
|
|
}
|
2017-04-18 19:44:36 +00:00
|
|
|
|
}
|