Struct validus::vstr::VStr

source ·
#[repr(transparent)]
pub struct VStr<Rule: ValidateString> { /* private fields */ }
Expand description

A validated string slice with a given rule or label.

The validation is advisory; the type does not make any more guarantees than a str slice. In fact, the VStr::assume_valid method can be used to create a VStr from a str without validation.

That being said, in general, the VStr::try_validate method should be used to create a VStr from a str.

The Rule is any function-like ZST that can be called on any str-slice to validate it. See: ValidateString.

Example

// How to use `VStr`:
// 1. Create your rule (that implements `ValidateString`).
// 2. Mention your rule in the type signature of `VStr`.

use validus::prelude::{ValidateString, VStr};

fn my_validate(s: &str) -> bool { true }

// 1. Create your rule.
struct MyRule;
impl ValidateString for MyRule {
    type Error = ();

    fn validate_str(s: &str) -> Result<(), Self::Error> {
        my_validate(s).then(|| ()).ok_or(())
    }
}

// 2. Mention your rule in the type signature of `VStr`.
// Now, you have a `VStr<MyRule>`, a string slice validated according
// to `MyRule`.
let vstr: &VStr<MyRule> = VStr::try_validate("hello").unwrap();

Implementations§

source§

impl<Rule: ValidateString> VStr<Rule>

source

pub fn try_validate(s: &str) -> Result<&Self, Rule::Error>

Upgrade a str slice with validation.

source

pub fn assume_valid(s: &str) -> &Self

Upgrade a str slice without validation.

(This might be useful when validation is expensive and the underlying data can be assumed to be valid.)

source

pub fn check(&self) -> Result<&Self, Rule::Error>

Re-check itself.

  • If self was created with try_validate, then this should return Ok.
  • If self was created with assume_valid, then this should return Ok if and only if the underlying data is actually valid.
source

pub fn try_change_rules<Rule2: ValidateString>( &self ) -> Result<&VStr<Rule2>, Rule2::Error>

Try to change the rule, returning an error if the string slice does not satisfy the new rule.

Also see: VStr::change_rules, VStr::try_validate.

source

pub fn change_rules<Rule2: ValidateString>(&self) -> &VStr<Rule2>where Rule: Into<Rule2>,

Try to change the rule infallibly whenever Rule: Into<Rule2>.

Also see: VStr::try_change_rules, VStr::erase_rules, VStr::assume_valid.

source

pub fn erase_rules(&self) -> &VStr<ValidateAll>

Erase the rules.

erase_rules converts a &VStr<_> (of any rule) to &VStr<ValidateAll>. ValidateAll is a special, permissive rule that validates everything.

Also see: VStr::assume_valid.

source

pub fn as_str(&self) -> &str

Get the underlying string slice.

source§

impl<Rule: ValidateString> VStr<Later<Rule>>

source

pub fn make_strict(&self) -> Result<&VStr<Rule>, Rule::Error>

Try to validate it now.

See Later for more information and examples.

Trait Implementations§

source§

impl<Rule: ValidateString> AsRef<str> for VStr<Rule>

source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<Rule: ValidateString> Debug for VStr<Rule>

source§

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

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

impl<'de: 'a, 'a, Rule: ValidateString> Deserialize<'de> for &'a VStr<Rule>where Rule::Error: Display,

source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
source§

impl<'de, Rule: ValidateString> Deserialize<'de> for Box<VStr<Rule>>where Rule::Error: Display,

source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
source§

impl<Rule: ValidateString> Display for VStr<Rule>

source§

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

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

impl<'a, R: ValidateString> From<&'a VStr<R>> for VCow<'a, R>

source§

fn from(s: &'a vstr<R>) -> Self

Converts to this type from the input type.
source§

impl<'a, Rule: ValidateString> From<&'a VStr<Rule>> for &'a str

source§

fn from(vstr: &'a VStr<Rule>) -> Self

Converts to this type from the input type.
source§

impl<Rule: ValidateString> From<&VStr<Rule>> for Arc<VStr<Rule>>

source§

fn from(vstr: &VStr<Rule>) -> Self

Converts to this type from the input type.
source§

impl<Rule: ValidateString> From<&VStr<Rule>> for Arc<str>

source§

fn from(vstr: &VStr<Rule>) -> Self

Converts to this type from the input type.
source§

impl<Rule: ValidateString> From<&VStr<Rule>> for Box<VStr<Rule>>

source§

fn from(vstr: &VStr<Rule>) -> Self

Converts to this type from the input type.
source§

impl<Rule: ValidateString> From<&VStr<Rule>> for Box<str>

source§

fn from(vstr: &VStr<Rule>) -> Self

Converts to this type from the input type.
source§

impl<Rule: ValidateString> From<&VStr<Rule>> for Rc<VStr<Rule>>

source§

fn from(vstr: &VStr<Rule>) -> Self

Converts to this type from the input type.
source§

impl<Rule: ValidateString> From<&VStr<Rule>> for Rc<str>

source§

fn from(vstr: &VStr<Rule>) -> Self

Converts to this type from the input type.
source§

impl<Rule: ValidateString> From<&VStr<Rule>> for String

source§

fn from(vstr: &VStr<Rule>) -> Self

Converts to this type from the input type.
source§

impl<Rule: ValidateString> Hash for VStr<Rule>

source§

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

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

impl<Rule: ValidateString> Ord for VStr<Rule>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
source§

impl<Rule: ValidateString> PartialEq<VStr<Rule>> for str

source§

fn eq(&self, other: &VStr<Rule>) -> 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<Rule1: ValidateString, Rule2: ValidateString> PartialEq<VStr<Rule2>> for VStr<Rule1>

source§

fn eq(&self, other: &VStr<Rule2>) -> 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<Rule: ValidateString> PartialEq<str> for VStr<Rule>

source§

fn eq(&self, other: &str) -> 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<Rule: ValidateString> PartialOrd<VStr<Rule>> for str

source§

fn partial_cmp(&self, other: &VStr<Rule>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<Rule1: ValidateString, Rule2: ValidateString> PartialOrd<VStr<Rule2>> for VStr<Rule1>

source§

fn partial_cmp(&self, other: &VStr<Rule2>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<Rule: ValidateString> PartialOrd<str> for VStr<Rule>

source§

fn partial_cmp(&self, other: &str) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<Rule: ValidateString> Serialize for VStr<Rule>

source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
source§

impl<'a, Rule: ValidateString> TryFrom<&'a str> for &'a VStr<Rule>

§

type Error = <Rule as ValidateString>::Error

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

fn try_from(s: &'a str) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<Rule: ValidateString> TryFrom<String> for Box<VStr<Rule>>

§

type Error = <Rule as ValidateString>::Error

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

fn try_from(s: String) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<Rule: ValidateString> Eq for VStr<Rule>

Auto Trait Implementations§

§

impl<Rule> RefUnwindSafe for VStr<Rule>where Rule: RefUnwindSafe,

§

impl<Rule> Send for VStr<Rule>

§

impl<Rule> !Sized for VStr<Rule>

§

impl<Rule> Sync for VStr<Rule>

§

impl<Rule> Unpin for VStr<Rule>

§

impl<Rule> UnwindSafe for VStr<Rule>where Rule: 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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.