Skip to main content

Rule

Trait Rule 

Source
pub trait Rule: Send + Sync {
    // Required methods
    fn name(&self) -> &'static str;
    fn check(&self, input: &str) -> RuleResult;
}
Expand description

A sanitization rule that inspects an input string and reports violations.

Implement this trait to create custom rules. Rules are composable via Sanitizer.

§Example

use shell_sanitize::{Rule, RuleResult, RuleViolation};

struct NoSpacesRule;

impl Rule for NoSpacesRule {
    fn name(&self) -> &'static str { "no_spaces" }

    fn check(&self, input: &str) -> RuleResult {
        let violations: Vec<_> = input
            .char_indices()
            .filter(|(_, c)| *c == ' ')
            .map(|(i, _)| RuleViolation::new(self.name(), "space character found")
                .at(i)
                .with_fragment(" "))
            .collect();
        if violations.is_empty() { Ok(()) } else { Err(violations) }
    }
}

Required Methods§

Source

fn name(&self) -> &'static str

A unique, human-readable identifier for this rule.

Source

fn check(&self, input: &str) -> RuleResult

Check the input and return violations, if any.

Trait Implementations§

Source§

impl Rule for Box<dyn Rule>

Source§

fn name(&self) -> &'static str

A unique, human-readable identifier for this rule.
Source§

fn check(&self, input: &str) -> RuleResult

Check the input and return violations, if any.

Implementations on Foreign Types§

Source§

impl Rule for Box<dyn Rule>

Source§

fn name(&self) -> &'static str

Source§

fn check(&self, input: &str) -> RuleResult

Implementors§