#[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>
impl<Rule: ValidateString> VStr<Rule>
sourcepub fn try_validate(s: &str) -> Result<&Self, Rule::Error>
pub fn try_validate(s: &str) -> Result<&Self, Rule::Error>
Upgrade a str slice with validation.
sourcepub fn assume_valid(s: &str) -> &Self
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.)
sourcepub fn check(&self) -> Result<&Self, Rule::Error>
pub fn check(&self) -> Result<&Self, Rule::Error>
Re-check itself.
- If
selfwas created withtry_validate, then this should returnOk. - If
selfwas created withassume_valid, then this should returnOkif and only if the underlying data is actually valid.
sourcepub fn try_change_rules<Rule2: ValidateString>(
&self
) -> Result<&VStr<Rule2>, Rule2::Error>
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.
sourcepub fn change_rules<Rule2: ValidateString>(&self) -> &VStr<Rule2>where
Rule: Into<Rule2>,
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.
sourcepub fn erase_rules(&self) -> &VStr<ValidateAll>
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.
Trait Implementations§
source§impl<Rule: ValidateString> Debug for VStr<Rule>
impl<Rule: ValidateString> Debug for VStr<Rule>
source§impl<'de: 'a, 'a, Rule: ValidateString> Deserialize<'de> for &'a VStr<Rule>where
Rule::Error: Display,
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>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
source§impl<'de, Rule: ValidateString> Deserialize<'de> for Box<VStr<Rule>>where
Rule::Error: Display,
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>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
source§impl<Rule: ValidateString> Display for VStr<Rule>
impl<Rule: ValidateString> Display for VStr<Rule>
source§impl<Rule: ValidateString> Hash for VStr<Rule>
impl<Rule: ValidateString> Hash for VStr<Rule>
source§impl<Rule: ValidateString> Ord for VStr<Rule>
impl<Rule: ValidateString> Ord for VStr<Rule>
source§impl<Rule: ValidateString> PartialEq<VStr<Rule>> for str
impl<Rule: ValidateString> PartialEq<VStr<Rule>> for str
source§impl<Rule1: ValidateString, Rule2: ValidateString> PartialEq<VStr<Rule2>> for VStr<Rule1>
impl<Rule1: ValidateString, Rule2: ValidateString> PartialEq<VStr<Rule2>> for VStr<Rule1>
source§impl<Rule: ValidateString> PartialOrd<VStr<Rule>> for str
impl<Rule: ValidateString> PartialOrd<VStr<Rule>> for str
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl<Rule1: ValidateString, Rule2: ValidateString> PartialOrd<VStr<Rule2>> for VStr<Rule1>
impl<Rule1: ValidateString, Rule2: ValidateString> PartialOrd<VStr<Rule2>> for VStr<Rule1>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl<Rule: ValidateString> PartialOrd<str> for VStr<Rule>
impl<Rule: ValidateString> PartialOrd<str> for VStr<Rule>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more