use std::{future::Future, pin::Pin, rc::Rc, time::Duration};
use gpui::SharedString;
use super::path::FieldKey;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ValidationEvent {
Mount,
Change,
Blur,
Submit,
Server,
}
pub(crate) type LocalFuture<T> = Pin<Box<dyn Future<Output = T>>>;
type SyncCheck<T> = Rc<dyn Fn(&T) -> Option<SharedString>>;
type AsyncCheck<T> = Rc<dyn Fn(T) -> LocalFuture<Option<SharedString>>>;
pub struct Validators<T> {
pub(crate) sync: Vec<(ValidationEvent, SyncCheck<T>)>,
pub(crate) asynchronous: Vec<(ValidationEvent, Duration, AsyncCheck<T>)>,
pub(crate) listen_to: Vec<SharedString>,
}
impl<T: 'static> Validators<T> {
#[must_use]
pub fn new() -> Self {
Self {
sync: Vec::new(),
asynchronous: Vec::new(),
listen_to: Vec::new(),
}
}
fn sync<M: Into<SharedString>>(
mut self,
event: ValidationEvent,
check: impl Fn(&T) -> Option<M> + 'static,
) -> Self {
self.sync
.push((event, Rc::new(move |value| check(value).map(Into::into))));
self
}
fn asynchronous<M, Fut>(
mut self,
event: ValidationEvent,
debounce: Duration,
check: impl Fn(T) -> Fut + 'static,
) -> Self
where
M: Into<SharedString>,
Fut: Future<Output = Option<M>> + 'static,
{
self.asynchronous.push((
event,
debounce,
Rc::new(move |value| {
let future = check(value);
Box::pin(async move { future.await.map(Into::into) })
}),
));
self
}
#[must_use]
pub fn on_mount<M: Into<SharedString>>(
self,
check: impl Fn(&T) -> Option<M> + 'static,
) -> Self {
self.sync(ValidationEvent::Mount, check)
}
#[must_use]
pub fn on_change<M: Into<SharedString>>(
self,
check: impl Fn(&T) -> Option<M> + 'static,
) -> Self {
self.sync(ValidationEvent::Change, check)
}
#[must_use]
pub fn on_blur<M: Into<SharedString>>(self, check: impl Fn(&T) -> Option<M> + 'static) -> Self {
self.sync(ValidationEvent::Blur, check)
}
#[must_use]
pub fn on_submit<M: Into<SharedString>>(
self,
check: impl Fn(&T) -> Option<M> + 'static,
) -> Self {
self.sync(ValidationEvent::Submit, check)
}
#[must_use]
pub fn on_change_async_debounce<M, Fut>(
self,
debounce: Duration,
check: impl Fn(T) -> Fut + 'static,
) -> Self
where
M: Into<SharedString>,
Fut: Future<Output = Option<M>> + 'static,
{
self.asynchronous(ValidationEvent::Change, debounce, check)
}
#[must_use]
pub fn on_blur_async<M, Fut>(self, check: impl Fn(T) -> Fut + 'static) -> Self
where
M: Into<SharedString>,
Fut: Future<Output = Option<M>> + 'static,
{
self.asynchronous(ValidationEvent::Blur, Duration::ZERO, check)
}
#[must_use]
pub fn on_submit_async<M, Fut>(self, check: impl Fn(T) -> Fut + 'static) -> Self
where
M: Into<SharedString>,
Fut: Future<Output = Option<M>> + 'static,
{
self.asynchronous(ValidationEvent::Submit, Duration::ZERO, check)
}
#[must_use]
pub fn listen_to(mut self, other: &impl FieldKey) -> Self {
self.listen_to.push(other.field_key());
self
}
}
impl<T: 'static> Default for Validators<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> Clone for Validators<T> {
fn clone(&self) -> Self {
Self {
sync: self.sync.clone(),
asynchronous: self.asynchronous.clone(),
listen_to: self.listen_to.clone(),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FormError {
pub(crate) form: Vec<SharedString>,
pub(crate) fields: Vec<(SharedString, SharedString)>,
}
impl FormError {
#[must_use]
pub fn form(message: impl Into<SharedString>) -> Self {
Self {
form: vec![message.into()],
fields: Vec::new(),
}
}
#[must_use]
pub fn field(field: &impl FieldKey, message: impl Into<SharedString>) -> Self {
Self {
form: Vec::new(),
fields: vec![(field.field_key(), message.into())],
}
}
#[must_use]
pub fn at_key(key: impl Into<SharedString>, message: impl Into<SharedString>) -> Self {
Self {
form: Vec::new(),
fields: vec![(key.into(), message.into())],
}
}
#[must_use]
pub fn and_field(mut self, field: &impl FieldKey, message: impl Into<SharedString>) -> Self {
self.fields.push((field.field_key(), message.into()));
self
}
#[must_use]
pub fn and_form(mut self, message: impl Into<SharedString>) -> Self {
self.form.push(message.into());
self
}
#[must_use]
pub fn merge(mut self, other: FormError) -> Self {
self.form.extend(other.form);
self.fields.extend(other.fields);
self
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.form.is_empty() && self.fields.is_empty()
}
#[must_use]
pub fn form_errors(&self) -> &[SharedString] {
&self.form
}
#[must_use]
pub fn field_errors(&self) -> &[(SharedString, SharedString)] {
&self.fields
}
}
type FormCheck<V> = Rc<dyn Fn(&V) -> Option<FormError>>;
pub struct FormValidators<V> {
pub(crate) checks: Vec<(ValidationEvent, FormCheck<V>)>,
}
impl<V: 'static> FormValidators<V> {
#[must_use]
pub fn new() -> Self {
Self { checks: Vec::new() }
}
fn check(
mut self,
event: ValidationEvent,
check: impl Fn(&V) -> Option<FormError> + 'static,
) -> Self {
self.checks.push((event, Rc::new(check)));
self
}
#[must_use]
pub fn on_change(self, check: impl Fn(&V) -> Option<FormError> + 'static) -> Self {
self.check(ValidationEvent::Change, check)
}
#[must_use]
pub fn on_blur(self, check: impl Fn(&V) -> Option<FormError> + 'static) -> Self {
self.check(ValidationEvent::Blur, check)
}
#[must_use]
pub fn on_submit(self, check: impl Fn(&V) -> Option<FormError> + 'static) -> Self {
self.check(ValidationEvent::Submit, check)
}
#[must_use]
pub fn schema(self, schema: impl Schema<V> + 'static) -> Self {
let schema = Rc::new(schema);
let on_submit = schema.clone();
self.on_change(move |values| schema.validate(values))
.on_submit(move |values| on_submit.validate(values))
}
}
impl<V: 'static> Default for FormValidators<V> {
fn default() -> Self {
Self::new()
}
}
impl<V> Clone for FormValidators<V> {
fn clone(&self) -> Self {
Self {
checks: self.checks.clone(),
}
}
}
pub trait Schema<V> {
fn validate(&self, values: &V) -> Option<FormError>;
}
impl<V, F: Fn(&V) -> Option<FormError>> Schema<V> for F {
fn validate(&self, values: &V) -> Option<FormError> {
self(values)
}
}