use std::{fmt::Debug, hash::Hash};
use topcoat::context::Cx;
use super::{Field, Schema};
use crate::values::{FieldError, FormValues};
pub trait FormInput: Sized + Send + Sync + 'static {
type Field: Copy + Eq + Hash + Debug + Send + Sync + 'static;
fn key(field: Self::Field) -> &'static str;
fn schema() -> Schema;
fn parse(cx: &Cx, values: &FormValues) -> Result<Self, Vec<FieldError>>;
fn write(&self, out: &mut FormValues);
}
impl FormInput for () {
type Field = std::convert::Infallible;
fn key(field: Self::Field) -> &'static str {
match field {}
}
fn schema() -> Schema {
Schema::empty()
}
fn parse(_cx: &Cx, _values: &FormValues) -> Result<Self, Vec<FieldError>> {
Ok(())
}
fn write(&self, _out: &mut FormValues) {}
}
#[doc(hidden)]
pub fn required_input(field: impl Into<Field>) -> Field {
let mut field = field.into();
field.set_required(true);
field
}