garde/rules/
suffix.rs

1//! Suffix validation.
2//!
3//! ```rust
4//! const SFX: &str = "_test";
5//!
6//! #[derive(garde::Validate)]
7//! struct Test {
8//!     #[garde(suffix("_test"))]
9//!     v: String,
10//!     #[garde(suffix(SFX))]
11//!     w: String,
12//! }
13//! ```
14//!
15//! The entrypoint is the [`Suffix`] trait. Implementing this trait for a type allows that type to be used with the `#[garde(suffix)]` 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: Suffix>(v: &T, (pat,): (&str,)) -> Result<(), Error> {
23    if !v.validate_suffix(pat) {
24        return Err(Error::new(format!("does not end with \"{pat}\"")));
25    }
26    Ok(())
27}
28
29pub trait Suffix {
30    fn validate_suffix(&self, pat: &str) -> bool;
31}
32
33impl<T: AsStr> Suffix for T {
34    fn validate_suffix(&self, pat: &str) -> bool {
35        self.as_str().ends_with(pat)
36    }
37}
38
39impl<T: Suffix> Suffix for Option<T> {
40    fn validate_suffix(&self, pat: &str) -> bool {
41        match self {
42            Some(value) => value.validate_suffix(pat),
43            None => true,
44        }
45    }
46}