use dioxus::prelude::*;
use std::{fmt::Debug, hash::Hash, pin::Pin};
#[cfg(feature = "validify")]
use validify::Validate;
use crate::{field::FieldState, form::*};
#[cfg(not(feature = "validify"))]
pub trait GetFormState {
fn get_form_state(&self) -> Store<FormState>;
}
#[cfg(feature = "validify")]
pub trait GetFormState: Validate {
fn get_form_state(&self) -> Store<FormState>;
}
pub trait ValidateOnSubmit {
fn validate_on_submit(&mut self) -> Pin<Box<impl Future<Output = ()>>>;
}
pub trait InitForm {
fn use_form() -> Self;
}
pub trait FormSubmitInput {
type SubmitInput: PartialEq;
fn build_submit_input(&mut self) -> Option<Self::SubmitInput>;
}
pub trait GetFieldRegistry: Clone + 'static {
type FieldRegistry;
fn get_field_registry(&self) -> Self::FieldRegistry;
}
pub trait FieldStateProvider<TField: Clone + PartialEq + 'static>: Clone + 'static {
type FieldValue: FieldValue;
#[cfg(not(feature = "validify"))]
type FieldError: FieldError
+ From<<<Self as FieldStateProvider<TField>>::FieldValue as FieldValue>::ConversionError>;
#[cfg(feature = "validify")]
type FieldError: FieldError
+ From<<<Self as FieldStateProvider<TField>>::FieldValue as FieldValue>::ConversionError>
+ OptionFrom<validify::ValidationError>;
fn get_field_state(
&self,
field: &TField,
) -> Store<FieldState<Self::FieldValue, Self::FieldError>>;
fn name(&self, field: &TField) -> &'static str;
}
pub trait FieldValue:
Clone + Default + PartialEq + Debug + From<Self::PrimitiveValue> + StringValue + 'static
{
type PrimitiveValue: PrimitiveFieldValue;
type ConversionError: std::error::Error + Clone;
fn from_str(value: &str) -> Result<Self, Self::ConversionError>;
fn to_primitive_field_value(&self) -> Self::PrimitiveValue;
}
pub trait PrimitiveFieldValue: Debug + Clone + PartialEq + 'static {}
pub trait FieldError:
Clone + PartialEq + Eq + std::error::Error + PartialOrd + Ord + ToString + 'static
{
fn custom_message(&self) -> Option<String> {
None
}
fn error_value(&self) -> String {
self.custom_message().unwrap_or(self.to_string())
}
}
pub trait OptionFrom<T>: Sized {
fn option_from(value: T) -> Option<Self>;
}
pub trait CalculateCanSubmit {
fn calculate_can_submit(&mut self) -> bool;
}
pub trait StringValue {
fn to_string_value(&self) -> String {
String::new()
}
}