use std::ops::{Deref, DerefMut};
use crate::form::prelude::*;
use crate::http::uri::fmt::{Query, FromUriParam};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Lenient<T>(T);
impl<T> Lenient<T> {
pub fn into_inner(self) -> T {
self.0
}
}
#[crate::async_trait]
impl<'v, T: FromForm<'v>> FromForm<'v> for Lenient<T> {
type Context = T::Context;
#[inline(always)]
fn init(opts: Options) -> Self::Context {
T::init(Options { strict: false, ..opts })
}
#[inline(always)]
fn push_value(ctxt: &mut Self::Context, field: ValueField<'v>) {
T::push_value(ctxt, field)
}
#[inline(always)]
async fn push_data(ctxt: &mut Self::Context, field: DataField<'v, '_>) {
T::push_data(ctxt, field).await
}
#[inline(always)]
fn finalize(this: Self::Context) -> Result<'v, Self> {
T::finalize(this).map(Self)
}
}
impl<T> Deref for Lenient<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for Lenient<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> From<T> for Lenient<T> {
#[inline]
fn from(val: T) -> Lenient<T> {
Lenient(val)
}
}
impl<'f, A, T: FromUriParam<Query, A> + FromForm<'f>> FromUriParam<Query, A> for Lenient<T> {
type Target = T::Target;
#[inline(always)]
fn from_uri_param(param: A) -> Self::Target {
T::from_uri_param(param)
}
}