#[repr(transparent)]pub struct Later<R: ValidateString> { /* private fields */ }
Expand description
Accept now, validate later.
This wrapper accepts all strings, but leaves behind a rule that should be applied later.
To lower a vstr<Later<Rule>>
to vstr<Rule>
, use
VStr::make_strict
.
Example
See also: fast_rule
.
use validus::prelude::*;
use validus::fast_rule;
struct EmailError;
struct Email;
fast_rule!(Email,
err = EmailError,
msg = "no @ symbol",
|s: &str| s.contains('@')
);
// Here, we start with an email with deferred (postponed) validation.
// Validation of `Later<_>` is infallible.
let v1: &vstr<Later<Email>> = "hi@example.com".validate().unwrap();
// Now, we truly validate it.
let v1: Result<&vstr<Email>, _> = v1.make_strict();
assert!(v1.is_ok());
// So, again, this is going to succeed.
let v2 = "notgood".validate::<Later<Email>>().unwrap();
// But, when we check it, it will fail, since it is not a good email address
// (according to the rule we defined).
let v2 = v2.make_strict();
assert!(v2.is_err());
// With the extension `StrExt`, we can also call `.assume_valid()`
// to skip validation, since we know that `Later<_>` doesn't validate.
let relaxed = "hi@example.com".assume_valid::<Later<Email>>();
assert!(relaxed.check().is_ok()); // This is infallible because `Later<_>` is infallible.
assert!(relaxed.make_strict().is_ok()); // Later<Email> -> Email.
let relaxed = "nonono".assume_valid::<Later<Email>>();
assert!(relaxed.check().is_ok()); // Yup, it is still infallible.
let strict = relaxed.make_strict(); // Now, we made it strict.
assert!(strict.is_err()); // It didn't make it (it was a bad email address.)
Trait Implementations§
source§impl<R: ValidateString> From<ValidateAll> for Later<R>
impl<R: ValidateString> From<ValidateAll> for Later<R>
source§fn from(_: ValidateAll) -> Self
fn from(_: ValidateAll) -> Self
Converts to this type from the input type.
source§impl<R: PartialEq + ValidateString> PartialEq<Later<R>> for Later<R>
impl<R: PartialEq + ValidateString> PartialEq<Later<R>> for Later<R>
source§impl<R: ValidateString> ValidateString for Later<R>
impl<R: ValidateString> ValidateString for Later<R>
impl<R: Copy + ValidateString> Copy for Later<R>
impl<R: Eq + ValidateString> Eq for Later<R>
impl<R: ValidateString> StructuralEq for Later<R>
impl<R: ValidateString> StructuralPartialEq for Later<R>
Auto Trait Implementations§
impl<R> RefUnwindSafe for Later<R>where R: RefUnwindSafe,
impl<R> Send for Later<R>
impl<R> Sync for Later<R>
impl<R> Unpin for Later<R>
impl<R> UnwindSafe for Later<R>where R: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more