garde/rules/
contains.rs

1//! Substring validation.
2//!
3//! ```rust
4//! const STR: &str = "test";
5//!
6//! #[derive(garde::Validate)]
7//! struct Test {
8//!     #[garde(contains("test"))]
9//!     v: String,
10//!     #[garde(contains(STR))]
11//!     w: String,
12//! }
13//! ```
14//!
15//! The entrypoint is the [`Contains`] trait. Implementing this trait for a type allows that type to be used with the `#[garde(contains)]` rule.
16//!
17//! This trait has a blanket implementation for all `T: garde::rules::AsStr`.
18
19use super::AsStr;
20use crate::error::Error;
21
22pub fn apply<T: Contains>(v: &T, (pat,): (&str,)) -> Result<(), Error> {
23    if !v.validate_contains(pat) {
24        return Err(Error::new(format!("does not contain \"{pat}\"")));
25    }
26    Ok(())
27}
28
29pub trait Contains {
30    fn validate_contains(&self, pat: &str) -> bool;
31}
32
33impl<T: AsStr> Contains for T {
34    fn validate_contains(&self, pat: &str) -> bool {
35        self.as_str().contains(pat)
36    }
37}
38
39impl<T: Contains> Contains for Option<T> {
40    fn validate_contains(&self, pat: &str) -> bool {
41        match self {
42            Some(value) => value.validate_contains(pat),
43            None => true,
44        }
45    }
46}