data_forms: Replace field_type String with a proper enum.
This commit is contained in:
parent
80695edb72
commit
7cdb46b90a
1 changed files with 45 additions and 5 deletions
|
@ -14,6 +14,47 @@ use ns;
|
||||||
|
|
||||||
use media_element::MediaElement;
|
use media_element::MediaElement;
|
||||||
|
|
||||||
|
#[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.")),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Option_ {
|
pub struct Option_ {
|
||||||
pub label: Option<String>,
|
pub label: Option<String>,
|
||||||
|
@ -23,7 +64,7 @@ pub struct Option_ {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Field {
|
pub struct Field {
|
||||||
pub var: String,
|
pub var: String,
|
||||||
pub type_: String, // TODO: use an enum here.
|
pub type_: FieldType,
|
||||||
pub label: Option<String>,
|
pub label: Option<String>,
|
||||||
pub required: bool,
|
pub required: bool,
|
||||||
pub options: Vec<Option_>,
|
pub options: Vec<Option_>,
|
||||||
|
@ -103,12 +144,11 @@ impl<'a> TryFrom<&'a Element> for DataForm {
|
||||||
form.instructions = Some(child.text());
|
form.instructions = Some(child.text());
|
||||||
} else if child.is("field", ns::DATA_FORMS) {
|
} else if child.is("field", ns::DATA_FORMS) {
|
||||||
let var: String = get_attr!(child, "var", required);
|
let var: String = get_attr!(child, "var", required);
|
||||||
// TODO: use Default instead.
|
let field_type = get_attr!(child, "type", default);
|
||||||
let field_type: String = get_attr!(child, "type", optional).unwrap_or(String::from("text-single"));
|
|
||||||
let label = get_attr!(child, "label", optional);
|
let label = get_attr!(child, "label", optional);
|
||||||
|
|
||||||
let is_form_type = var == "FORM_TYPE" && field_type == "hidden";
|
let is_form_type = var == "FORM_TYPE" && field_type == FieldType::Hidden;
|
||||||
let is_list = field_type == "list-single" || field_type == "list-multi";
|
let is_list = field_type == FieldType::ListSingle || field_type == FieldType::ListMulti;
|
||||||
let mut field = Field {
|
let mut field = Field {
|
||||||
var: var,
|
var: var,
|
||||||
type_: field_type,
|
type_: field_type,
|
||||||
|
|
Loading…
Reference in a new issue