modify parse options

This commit is contained in:
Mitchell Marino 2024-06-07 12:09:41 -05:00
parent 5d0b4a7449
commit fb47d553e7

View File

@ -237,7 +237,7 @@ impl<FD: FormToolData, FS: FormStyle, C: ControlData, FDT> ControlBuilder<FD, FS
/// 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 custom_parse(
pub fn parse_custom(
mut self,
parse_fn: impl ParseFn<C::ReturnType, FDT>,
unparse_fn: impl UnparseFn<C::ReturnType, FDT>,
@ -254,6 +254,76 @@ impl<FD: FormToolData, FS: FormStyle, C: ControlData, FDT> ControlBuilder<FD, FS
}
}
impl<FD, FS, C, FDT> ControlBuilder<FD, FS, C, FDT>
where
FD: FormToolData,
FS: FormStyle,
C: ControlData,
FDT: TryFrom<<C as ControlData>::ReturnType>,
<FDT as TryFrom<<C as ControlData>::ReturnType>>::Error: ToString,
<C as ControlData>::ReturnType: From<FDT>,
{
/// 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| {
<C as ControlData>::ReturnType::from(field)
}));
self
}
}
impl<FD, FS, C, FDT> ControlBuilder<FD, FS, C, FDT>
where
FD: FormToolData,
FS: FormStyle,
C: ControlData<ReturnType = String>,
FDT: FromStr + ToString,
<FDT as FromStr>::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::<FDT>()
.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::<FDT>()
.map_err(|e| e.to_string())
}));
self.unparse_fn = Some(Box::new(|field| field.to_string()));
self
}
}
impl<FD: FormToolData, FS: FormStyle, C: ValidatedControlData, FDT> ControlBuilder<FD, FS, C, FDT> {
/// Sets the validation function for this control
///
@ -273,54 +343,3 @@ impl<FD: FormToolData, FS: FormStyle, C: ValidatedControlData, FDT> ControlBuild
self
}
}
impl<FD, FS, C, FDT> ControlBuilder<FD, FS, C, FDT>
where
FD: FormToolData,
FS: FormStyle,
C: ControlData,
FDT: TryFrom<<C as ControlData>::ReturnType>,
<FDT as TryFrom<<C as ControlData>::ReturnType>>::Error: ToString,
<C as ControlData>::ReturnType: From<FDT>,
{
/// 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| {
<C as ControlData>::ReturnType::from(field)
}));
self
}
}
impl<FD, FS, C, FDT> ControlBuilder<FD, FS, C, FDT>
where
FD: FormToolData,
FS: FormStyle,
C: ControlData<ReturnType = String>,
FDT: FromStr + ToString,
<FDT as FromStr>::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::<FDT>()
.map_err(|e| e.to_string())
}));
self.unparse_fn = Some(Box::new(|field| field.to_string()));
self
}
}