From a256e58672b33409f1e7f9102a047a50a10455cc Mon Sep 17 00:00:00 2001 From: Mitchell Marino Date: Tue, 26 Mar 2024 13:30:14 -0500 Subject: [PATCH] update --- src/controls/mod.rs | 57 +++++++++++++++++++++----------------- src/controls/select.rs | 8 ++++-- src/controls/text_area.rs | 8 ++++-- src/controls/text_input.rs | 8 ++++-- src/form.rs | 7 +++-- 5 files changed, 51 insertions(+), 37 deletions(-) diff --git a/src/controls/mod.rs b/src/controls/mod.rs index 5e1b3cc..f8bba51 100644 --- a/src/controls/mod.rs +++ b/src/controls/mod.rs @@ -7,26 +7,15 @@ pub mod submit; pub mod text_area; pub mod text_input; -pub trait ValidationFn: Fn(&FD) -> Result<(), String> + 'static {} -pub trait ParseFn: - Fn(C::ReturnType, &mut FD) -> Result<(), String> + 'static -{ -} +pub trait ValidationFn: Fn(&FDT) -> Result<(), String> + 'static {} +pub trait ParseFn: Fn(&CT) -> Result + 'static {} +pub trait FieldFn: Fn(&mut FD) -> FDT + 'static {} // implement the trait for all valid types -impl ValidationFn for T -where - FD: FormData, - T: Fn(&FD) -> Result<(), String> + 'static, -{ -} +impl ValidationFn for T where T: Fn(&FDT) -> Result<(), String> + 'static {} // implement the trait for all valid types -impl ParseFn for T -where - FD: FormData, - C: ControlData, - T: Fn(C::ReturnType, &mut FD) -> Result<(), String> + 'static, -{ -} +impl ParseFn for F where F: Fn(&CR) -> Result + 'static {} +// implement the trait for all valid types +impl FieldFn for F where F: Fn(&mut FD) -> FDT + 'static {} pub trait VanityControlData: 'static { fn build_control(fs: &FS, control: ControlRenderData) -> View; @@ -69,19 +58,23 @@ impl VanityControlBuilder { } } -pub struct ControlBuilder { - pub(crate) parse_fn: Box>, - pub(crate) validation_fn: Box>, +pub struct ControlBuilder { + pub(crate) field_fn: Option>>, + pub(crate) parse_fn: Option>>, + pub(crate) unparse_fn: Option>>, + pub(crate) validation_fn: Option>>, pub(crate) style_attributes: Vec, pub(crate) data: C, } -impl ControlBuilder { +impl ControlBuilder { pub(crate) fn new(data: C) -> Self { ControlBuilder { data, - parse_fn: Box::new(|_, _| Ok(())), - validation_fn: Box::new(|_| Ok(())), + field_fn: None, + parse_fn: None, + unparse_fn: None, + validation_fn: None, style_attributes: Vec::new(), } } @@ -103,6 +96,20 @@ impl ControlBuilder { ) } + // TODO: add method that automatically does the parse and unparse using + // TryInto and TryFrom + pub fn field_with( + mut self, + field_fn: impl FieldFn, + parse_fn: impl ParseFn, + unparse_fn: impl ParseFn, + ) -> Self { + self.field_fn = Box::new(field_fn); + self.parse_fn = Box::new(parse_fn); + self.unparse_fn = Box::new(unparse_fn); + self + } + pub fn parse_fn(mut self, parse_fn: impl ParseFn) -> Self { self.parse_fn = Box::new(parse_fn) as Box>; self @@ -112,7 +119,7 @@ impl ControlBuilder { mut self, validation_fn: impl Fn(&FD) -> Result<(), String> + 'static, ) -> Self { - self.validation_fn = Box::new(validation_fn) as Box>; + self.validation_fn = Some(Box::new(validation_fn)) as _; self } diff --git a/src/controls/select.rs b/src/controls/select.rs index 1bdfea5..eb05313 100644 --- a/src/controls/select.rs +++ b/src/controls/select.rs @@ -25,15 +25,17 @@ impl ControlData for SelectData { } impl FormBuilder { - pub fn select( + pub fn select( self, - builder: impl Fn(ControlBuilder) -> ControlBuilder, + builder: impl Fn( + ControlBuilder, + ) -> ControlBuilder, ) -> Self { self.new_control(builder) } } -impl ControlBuilder { +impl ControlBuilder { pub fn options(mut self, options: Vec) -> Self { self.data.options = options; self diff --git a/src/controls/text_area.rs b/src/controls/text_area.rs index d0683fb..fc71195 100644 --- a/src/controls/text_area.rs +++ b/src/controls/text_area.rs @@ -24,15 +24,17 @@ impl ControlData for TextAreaData { } impl FormBuilder { - pub fn text_area( + pub fn text_area( self, - builder: impl Fn(ControlBuilder) -> ControlBuilder, + builder: impl Fn( + ControlBuilder, + ) -> ControlBuilder, ) -> Self { self.new_control(builder) } } -impl ControlBuilder { +impl ControlBuilder { pub fn placeholder(mut self, placeholder: impl ToString) -> Self { self.data.placeholder = Some(placeholder.to_string()); self diff --git a/src/controls/text_input.rs b/src/controls/text_input.rs index ddc07d0..458f9e4 100644 --- a/src/controls/text_input.rs +++ b/src/controls/text_input.rs @@ -40,15 +40,17 @@ impl ControlData for TextInputData { } impl FormBuilder { - pub fn text_input( + pub fn text_input( self, - builder: impl Fn(ControlBuilder) -> ControlBuilder, + builder: impl Fn( + ControlBuilder, + ) -> ControlBuilder, ) -> Self { self.new_control(builder) } } -impl ControlBuilder { +impl ControlBuilder { pub fn placeholder(mut self, placeholder: impl ToString) -> Self { self.data.placeholder = Some(placeholder.to_string()); self diff --git a/src/form.rs b/src/form.rs index bc6ed5f..4c3d7a8 100644 --- a/src/form.rs +++ b/src/form.rs @@ -145,9 +145,9 @@ impl FormBuilder { self } - pub(crate) fn new_control( + pub(crate) fn new_control( mut self, - builder: impl Fn(ControlBuilder) -> ControlBuilder, + builder: impl Fn(ControlBuilder) -> ControlBuilder, ) -> Self { let control_builder = ControlBuilder::new(C::default()); let control = builder(control_builder); @@ -165,7 +165,7 @@ impl FormBuilder { full_builder.views.push(view); } - fn add_control(&mut self, control: ControlBuilder) { + fn add_control(&mut self, control: ControlBuilder) { let full_builder = match &mut self.inner { FormBuilderInner::ValidationBuilder { validations } => { validations.push(control.validation_fn); @@ -199,6 +199,7 @@ impl FormBuilder { FormBuilderInner::FullBuilder(full_builder) => Form { fd: full_builder.fd_get, validations: full_builder.validations, + // TODO: wrap in the style's form wrapper view: full_builder.views.into_view(), }, FormBuilderInner::ValidationBuilder { validations } => Form {