Struct validus::vstr::Later

source ·
#[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: Clone + ValidateString> Clone for Later<R>

source§

fn clone(&self) -> Later<R>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<R: Debug + ValidateString> Debug for Later<R>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<R: Default + ValidateString> Default for Later<R>

source§

fn default() -> Later<R>

Returns the “default value” for a type. Read more
source§

impl<R: ValidateString> From<ValidateAll> for Later<R>

source§

fn from(_: ValidateAll) -> Self

Converts to this type from the input type.
source§

impl<R: Hash + ValidateString> Hash for Later<R>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<R: PartialEq + ValidateString> PartialEq<Later<R>> for Later<R>

source§

fn eq(&self, other: &Later<R>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<R: ValidateString> ValidateString for Later<R>

§

type Error = Infallible

Explain why the string slice is invalid. Read more
source§

fn validate_str(_: &str) -> Result<(), Self::Error>

Validate a string slice.
source§

impl<R: Copy + ValidateString> Copy for Later<R>

source§

impl<R: Eq + ValidateString> Eq for Later<R>

source§

impl<R: ValidateString> StructuralEq for Later<R>

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.