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/.
|
|
|
|
|
|
2017-05-06 19:48:41 +00:00
|
|
|
|
use std::convert::TryFrom;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
|
|
use minidom::Element;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-05-21 16:08:05 +00:00
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
pub enum FieldType {
|
|
|
|
|
Boolean,
|
|
|
|
|
Fixed,
|
|
|
|
|
Hidden,
|
|
|
|
|
JidMulti,
|
|
|
|
|
JidSingle,
|
|
|
|
|
ListMulti,
|
|
|
|
|
ListSingle,
|
|
|
|
|
TextMulti,
|
|
|
|
|
TextPrivate,
|
|
|
|
|
TextSingle,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for FieldType {
|
|
|
|
|
fn default() -> FieldType {
|
|
|
|
|
FieldType::TextSingle
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromStr for FieldType {
|
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<FieldType, Error> {
|
|
|
|
|
Ok(match s {
|
|
|
|
|
"boolean" => FieldType::Boolean,
|
|
|
|
|
"fixed" => FieldType::Fixed,
|
|
|
|
|
"hidden" => FieldType::Hidden,
|
|
|
|
|
"jid-multi" => FieldType::JidMulti,
|
|
|
|
|
"jid-single" => FieldType::JidSingle,
|
|
|
|
|
"list-multi" => FieldType::ListMulti,
|
|
|
|
|
"list-single" => FieldType::ListSingle,
|
|
|
|
|
"text-multi" => FieldType::TextMulti,
|
|
|
|
|
"text-private" => FieldType::TextPrivate,
|
|
|
|
|
"text-single" => FieldType::TextSingle,
|
|
|
|
|
|
|
|
|
|
_ => return Err(Error::ParseError("Invalid 'type' attribute in field element.")),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 15:41:29 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Option_ {
|
|
|
|
|
pub label: Option<String>,
|
|
|
|
|
pub value: String,
|
|
|
|
|
}
|
|
|
|
|
|
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-04-20 23:41:15 +00:00
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2017-04-18 19:44:36 +00:00
|
|
|
|
pub enum DataFormType {
|
|
|
|
|
Cancel,
|
|
|
|
|
Form,
|
|
|
|
|
Result_,
|
|
|
|
|
Submit,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromStr for DataFormType {
|
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<DataFormType, Error> {
|
2017-04-23 02:42:50 +00:00
|
|
|
|
Ok(match s {
|
|
|
|
|
"cancel" => DataFormType::Cancel,
|
|
|
|
|
"form" => DataFormType::Form,
|
|
|
|
|
"result" => DataFormType::Result_,
|
|
|
|
|
"submit" => DataFormType::Submit,
|
|
|
|
|
|
|
|
|
|
_ => return Err(Error::ParseError("Unknown data form type.")),
|
|
|
|
|
})
|
2017-04-18 19:44:36 +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-06 19:51:39 +00:00
|
|
|
|
impl<'a> TryFrom<&'a Element> for DataForm {
|
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
|
|
fn try_from(elem: &'a Element) -> Result<DataForm, Error> {
|
|
|
|
|
if !elem.is("x", ns::DATA_FORMS) {
|
|
|
|
|
return Err(Error::ParseError("This is not a data form element."));
|
|
|
|
|
}
|
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."));
|
|
|
|
|
}
|
|
|
|
|
for _ in child.children() {
|
|
|
|
|
return Err(Error::ParseError("Title element must not have any child."));
|
|
|
|
|
}
|
|
|
|
|
for _ in child.attrs() {
|
|
|
|
|
return Err(Error::ParseError("Title element must not have any attribute."));
|
|
|
|
|
}
|
|
|
|
|
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."));
|
|
|
|
|
}
|
|
|
|
|
for _ in child.children() {
|
|
|
|
|
return Err(Error::ParseError("instructions element must not have any child."));
|
|
|
|
|
}
|
|
|
|
|
for _ in child.attrs() {
|
|
|
|
|
return Err(Error::ParseError("instructions element must not have any attribute."));
|
|
|
|
|
}
|
|
|
|
|
form.instructions = Some(child.text());
|
|
|
|
|
} else if child.is("field", ns::DATA_FORMS) {
|
|
|
|
|
let var: String = get_attr!(child, "var", required);
|
2017-05-21 16:08:05 +00:00
|
|
|
|
let field_type = get_attr!(child, "type", default);
|
2017-05-21 15:41:29 +00:00
|
|
|
|
let label = get_attr!(child, "label", optional);
|
|
|
|
|
|
2017-05-21 16:08:05 +00:00
|
|
|
|
let is_form_type = var == "FORM_TYPE" && field_type == FieldType::Hidden;
|
|
|
|
|
let is_list = field_type == FieldType::ListSingle || field_type == FieldType::ListMulti;
|
2017-05-21 15:41:29 +00:00
|
|
|
|
let mut field = Field {
|
|
|
|
|
var: var,
|
|
|
|
|
type_: field_type,
|
|
|
|
|
label: label,
|
|
|
|
|
required: false,
|
|
|
|
|
options: vec!(),
|
|
|
|
|
values: vec!(),
|
|
|
|
|
media: vec!(),
|
|
|
|
|
};
|
|
|
|
|
for element in child.children() {
|
2017-05-06 19:51:39 +00:00
|
|
|
|
if element.is("value", ns::DATA_FORMS) {
|
2017-05-21 15:41:29 +00:00
|
|
|
|
for _ in element.children() {
|
|
|
|
|
return Err(Error::ParseError("Value element must not have any child."));
|
|
|
|
|
}
|
|
|
|
|
for _ in element.attrs() {
|
|
|
|
|
return Err(Error::ParseError("Value element must not have any attribute."));
|
|
|
|
|
}
|
|
|
|
|
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."));
|
|
|
|
|
}
|
|
|
|
|
for _ in element.children() {
|
|
|
|
|
return Err(Error::ParseError("Required element must not have any child."));
|
|
|
|
|
}
|
|
|
|
|
for _ in element.attrs() {
|
|
|
|
|
return Err(Error::ParseError("Required element must not have any attribute."));
|
|
|
|
|
}
|
|
|
|
|
field.required = true;
|
|
|
|
|
} else if element.is("option", ns::DATA_FORMS) {
|
|
|
|
|
if !is_list {
|
|
|
|
|
return Err(Error::ParseError("Option element found in non-list field."));
|
|
|
|
|
}
|
|
|
|
|
let label = get_attr!(element, "label", optional);
|
|
|
|
|
let mut value = None;
|
|
|
|
|
for child2 in element.children() {
|
|
|
|
|
if child2.is("value", ns::DATA_FORMS) {
|
|
|
|
|
if value.is_some() {
|
|
|
|
|
return Err(Error::ParseError("More than one value element in option element"));
|
|
|
|
|
}
|
|
|
|
|
value = Some(child2.text());
|
|
|
|
|
} else {
|
|
|
|
|
return Err(Error::ParseError("Non-value element in option element"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let value = value.ok_or(Error::ParseError("No value element in option element"))?;
|
|
|
|
|
field.options.push(Option_ {
|
|
|
|
|
label: label,
|
|
|
|
|
value: value,
|
|
|
|
|
});
|
2017-05-06 19:51:39 +00:00
|
|
|
|
} else if element.is("media", ns::MEDIA_ELEMENT) {
|
|
|
|
|
match MediaElement::try_from(element) {
|
2017-05-21 15:41:29 +00:00
|
|
|
|
Ok(media_element) => field.media.push(media_element),
|
2017-05-06 19:51:39 +00:00
|
|
|
|
Err(_) => (), // TODO: is it really nice to swallow this error?
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return Err(Error::ParseError("Field child isn’t a value or media element."));
|
2017-04-18 19:44:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-21 15:41:29 +00:00
|
|
|
|
if is_form_type {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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-06 19:51:39 +00:00
|
|
|
|
let form = DataForm::try_from(&elem).unwrap();
|
|
|
|
|
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-06 19:51:39 +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-06 19:51:39 +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!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown data form type.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_wrong_child() {
|
|
|
|
|
let elem: Element = "<x xmlns='jabber:x:data' type='cancel'><coucou/></x>".parse().unwrap();
|
2017-05-06 19:51:39 +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
|
|
|
|
}
|
|
|
|
|
}
|