StrExt

Trait StrExt 

Source
pub trait StrExt<'a> {
    // Required methods
    fn validate<Rule: ValidateString>(
        self,
    ) -> Result<&'a VStr<Rule>, Rule::Error>;
    fn assume_valid<Rule: ValidateString>(self) -> &'a VStr<Rule>;
    fn validate_or_panic<Rule: ValidateString>(self) -> &'a VStr<Rule>
       where Rule::Error: Debug;
}
Expand description

Call .validate() on any str-slice to validate it.

§Example

use validus::vstr::{vstr, ValidateString, StrExt};

struct MyRule;

impl ValidateString for MyRule {
    type Error = &'static str;

    fn validate_str(s: &str) -> Result<(), Self::Error> {
        if s.len() > 5 {
            Ok(())
        } else {
            Err("string is too short")
        }
    }
}

// `StrExt` allows you to call `.validate` on any `str`-slice.
let vv: &vstr<MyRule> = "hello world".validate::<MyRule>().unwrap();
assert_eq!(vv, "hello world");

Required Methods§

Source

fn validate<Rule: ValidateString>(self) -> Result<&'a VStr<Rule>, Rule::Error>

Validate a string slice.

Source

fn assume_valid<Rule: ValidateString>(self) -> &'a VStr<Rule>

Assume a valid string slice.

Source

fn validate_or_panic<Rule: ValidateString>(self) -> &'a VStr<Rule>
where Rule::Error: Debug,

Validate the string slice or panic.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'a> StrExt<'a> for &'a str

Source§

fn validate<Rule: ValidateString>(self) -> Result<&'a VStr<Rule>, Rule::Error>

Source§

fn assume_valid<Rule: ValidateString>(self) -> &'a VStr<Rule>

Source§

fn validate_or_panic<Rule: ValidateString>(self) -> &'a VStr<Rule>
where Rule::Error: Debug,

Implementors§