dataforms: Add helper constructors for Field and DataForm.
This commit is contained in:
parent
56363714fb
commit
1cc16e65db
1 changed files with 37 additions and 0 deletions
|
@ -94,6 +94,30 @@ pub struct Field {
|
|||
}
|
||||
|
||||
impl Field {
|
||||
/// Create a new Field, of the given var and type.
|
||||
pub fn new(var: &str, type_: FieldType) -> Field {
|
||||
Field {
|
||||
var: String::from(var),
|
||||
type_,
|
||||
label: None,
|
||||
required: false,
|
||||
options: Vec::new(),
|
||||
media: Vec::new(),
|
||||
values: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Set only one value in this Field.
|
||||
pub fn with_value(mut self, value: &str) -> Field {
|
||||
self.values.push(String::from(value));
|
||||
self
|
||||
}
|
||||
|
||||
/// Create a text-single Field with the given var and unique value.
|
||||
pub fn text_single(var: &str, value: &str) -> Field {
|
||||
Field::new(var, FieldType::TextSingle).with_value(value)
|
||||
}
|
||||
|
||||
fn is_list(&self) -> bool {
|
||||
self.type_ == FieldType::ListSingle || self.type_ == FieldType::ListMulti
|
||||
}
|
||||
|
@ -207,6 +231,19 @@ pub struct DataForm {
|
|||
pub fields: Vec<Field>,
|
||||
}
|
||||
|
||||
impl DataForm {
|
||||
/// Create a new DataForm.
|
||||
pub fn new(type_: DataFormType, form_type: &str, fields: Vec<Field>) -> DataForm {
|
||||
DataForm {
|
||||
type_,
|
||||
form_type: Some(String::from(form_type)),
|
||||
title: None,
|
||||
instructions: None,
|
||||
fields,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Element> for DataForm {
|
||||
type Error = Error;
|
||||
|
||||
|
|
Loading…
Reference in a new issue