From 1cc16e65dbf28432167ad2450f7886048f880c98 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Mon, 3 Jan 2022 11:50:58 +0100 Subject: [PATCH] dataforms: Add helper constructors for Field and DataForm. --- parsers/src/data_forms.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/parsers/src/data_forms.rs b/parsers/src/data_forms.rs index 596a317..e1eb473 100644 --- a/parsers/src/data_forms.rs +++ b/parsers/src/data_forms.rs @@ -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, } +impl DataForm { + /// Create a new DataForm. + pub fn new(type_: DataFormType, form_type: &str, fields: Vec) -> DataForm { + DataForm { + type_, + form_type: Some(String::from(form_type)), + title: None, + instructions: None, + fields, + } + } +} + impl TryFrom for DataForm { type Error = Error;