Impl 'FromFormField' for 'Cow', 'Capped<Cow>'.

This commit is contained in:
Sergio Benitez 2021-04-07 19:48:01 -07:00
parent e157904524
commit 3535d08ff8
1 changed files with 17 additions and 1 deletions

View File

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr};
use std::num::{ use std::num::{
NonZeroIsize, NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128,
@ -312,14 +313,29 @@ impl<'v> FromFormField<'v> for bool {
fn from_value(field: ValueField<'v>) -> Result<'v, Self> { fn from_value(field: ValueField<'v>) -> Result<'v, Self> {
match field.value.as_uncased() { match field.value.as_uncased() {
v if v == "on" || v == "yes" || v == "true" => Ok(true),
v if v == "off" || v == "no" || v == "false" => Ok(false), v if v == "off" || v == "no" || v == "false" => Ok(false),
v if v.is_empty() || v == "on" || v == "yes" || v == "true" => Ok(true),
// force a `ParseBoolError` // force a `ParseBoolError`
_ => Ok("".parse()?), _ => Ok("".parse()?),
} }
} }
} }
#[crate::async_trait]
impl<'v> FromFormField<'v> for Capped<Cow<'v, str>> {
fn from_value(field: ValueField<'v>) -> Result<'v, Self> {
let capped = <Capped<&'v str>>::from_value(field)?;
Ok(capped.map(|s| s.into()))
}
async fn from_data(field: DataField<'v, '_>) -> Result<'v, Self> {
let capped = <Capped<&'v str>>::from_data(field).await?;
Ok(capped.map(|s| s.into()))
}
}
impl_strict_from_form_field_from_capped!(Cow<'v, str>);
macro_rules! impl_with_parse { macro_rules! impl_with_parse {
($($T:ident),+ $(,)?) => ($( ($($T:ident),+ $(,)?) => ($(
impl<'v> FromFormField<'v> for $T { impl<'v> FromFormField<'v> for $T {