diff --git a/core/lib/src/form/validate.rs b/core/lib/src/form/validate.rs index 2a542fbd..0c8b7ef9 100644 --- a/core/lib/src/form/validate.rs +++ b/core/lib/src/form/validate.rs @@ -83,7 +83,7 @@ use std::convert::TryInto; use std::ops::{RangeBounds, Bound}; use std::fmt::Debug; -use crate::data::ByteUnit; +use crate::data::{ByteUnit, Capped}; use rocket_http::ContentType; use crate::{fs::TempFile, form::{Result, Error}}; @@ -334,6 +334,12 @@ impl> Len for Option { fn zero_len() -> L { T::zero_len() } } +impl> Len for Capped { + fn len(&self) -> L { self.value.len() } + fn len_into_u64(len: L) -> u64 { T::len_into_u64(len) } + fn zero_len() -> L { T::zero_len() } +} + impl> Len for Result<'_, T> { fn len(&self) -> L { self.as_ref().ok().len() } fn len_into_u64(len: L) -> u64 { T::len_into_u64(len) } @@ -801,6 +807,10 @@ pub fn ext<'v>(file: &TempFile<'_>, r#type: ContentType) -> Result<'v, ()> { /// #[derive(PartialEq, FromFormField)] /// enum Pet { Cat, Dog } /// +/// fn is_dog(p: &Pet) -> bool { +/// matches!(p, Pet::Dog) +/// } +/// /// #[derive(FromForm)] /// struct Foo { /// // These are equivalent. Prefer the former. @@ -809,10 +819,14 @@ pub fn ext<'v>(file: &TempFile<'_>, r#type: ContentType) -> Result<'v, ()> { /// pets: Vec, /// // These are equivalent. Prefer the former. /// #[field(validate = eq(Pet::Dog))] -/// #[field(validate = with(|p| *p == Pet::Dog, "expected a dog"))] +/// #[field(validate = with(|p| matches!(p, Pet::Dog), "expected a dog"))] +/// #[field(validate = with(|p| is_dog(p), "expected a dog"))] +/// # #[field(validate = with(|p| is_dog(&self.dog), "expected a dog"))] +/// #[field(validate = with(is_dog, "expected a dog"))] /// dog: Pet, /// // These are equivalent. Prefer the former. /// #[field(validate = contains(&self.dog))] +/// # #[field(validate = with(|p| is_dog(&self.dog), "expected a dog"))] /// #[field(validate = with(|pets| pets.iter().any(|p| p == &self.dog), "missing dog"))] /// one_dog_please: Vec, /// }