diff --git a/src/controls/mod.rs b/src/controls/mod.rs index c18825f..cacf492 100644 --- a/src/controls/mod.rs +++ b/src/controls/mod.rs @@ -237,7 +237,7 @@ impl ControlBuilder, unparse_fn: impl UnparseFn, @@ -254,6 +254,76 @@ impl ControlBuilder ControlBuilder +where + FD: FormToolData, + FS: FormStyle, + C: ControlData, + FDT: TryFrom<::ReturnType>, + ::ReturnType>>::Error: ToString, + ::ReturnType: From, +{ + /// Sets the parse functions to use the [`TryFrom`] and [`From`] traits + /// for parsing and unparsing respectively. + /// + /// The parse and unparse functions define how to turn what the user + /// types in the form into what is stored in the form data struct and + /// vice versa. + pub fn parse_from(mut self) -> Self { + self.parse_fn = Some(Box::new(|control_return_value| { + FDT::try_from(control_return_value).map_err(|e| e.to_string()) + })); + self.unparse_fn = Some(Box::new(|field| { + ::ReturnType::from(field) + })); + self + } +} + +impl ControlBuilder +where + FD: FormToolData, + FS: FormStyle, + C: ControlData, + FDT: FromStr + ToString, + ::Err: ToString, +{ + /// Sets the parse functions to use the [`FromStr`] [`ToString`] and traits + /// for parsing and unparsing respectively. To trim the string before parsing, + /// see [`parse_trimed_string`]. + /// + /// The parse and unparse functions define how to turn what the user + /// types in the form into what is stored in the form data struct and + /// vice versa. + pub fn parse_string(mut self) -> Self { + self.parse_fn = Some(Box::new(|control_return_value| { + control_return_value + .parse::() + .map_err(|e| e.to_string()) + })); + self.unparse_fn = Some(Box::new(|field| field.to_string())); + self + } + + /// Sets the parse functions to use the [`FromStr`] [`ToString`] and traits + /// for parsing and unparsing respectively, similar to [`parse_string`]. + /// However, this method trims the string before parsing. + /// + /// The parse and unparse functions define how to turn what the user + /// types in the form into what is stored in the form data struct and + /// vice versa. + pub fn parse_trimmed_string(mut self) -> Self { + self.parse_fn = Some(Box::new(|control_return_value| { + control_return_value + .trim() + .parse::() + .map_err(|e| e.to_string()) + })); + self.unparse_fn = Some(Box::new(|field| field.to_string())); + self + } +} + impl ControlBuilder { /// Sets the validation function for this control /// @@ -273,54 +343,3 @@ impl ControlBuild self } } - -impl ControlBuilder -where - FD: FormToolData, - FS: FormStyle, - C: ControlData, - FDT: TryFrom<::ReturnType>, - ::ReturnType>>::Error: ToString, - ::ReturnType: From, -{ - /// Sets the parse functions to use the [`TryFrom`] and [`From`] traits - /// for parsing and unparsing respectively. - /// - /// The parse and unparse functions define how to turn what the user - /// types in the form into what is stored in the form data struct and - /// vice versa. - pub fn default_parse(mut self) -> Self { - self.parse_fn = Some(Box::new(|control_return_value| { - FDT::try_from(control_return_value).map_err(|e| e.to_string()) - })); - self.unparse_fn = Some(Box::new(|field| { - ::ReturnType::from(field) - })); - self - } -} - -impl ControlBuilder -where - FD: FormToolData, - FS: FormStyle, - C: ControlData, - FDT: FromStr + ToString, - ::Err: ToString, -{ - /// Sets the parse functions to use the [`FromStr`] [`ToString`] and traits - /// for parsing and unparsing respectively. - /// - /// The parse and unparse functions define how to turn what the user - /// types in the form into what is stored in the form data struct and - /// vice versa. - pub fn string_parse(mut self) -> Self { - self.parse_fn = Some(Box::new(|control_return_value| { - control_return_value - .parse::() - .map_err(|e| e.to_string()) - })); - self.unparse_fn = Some(Box::new(|field| field.to_string())); - self - } -}