1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::error::Error;
pub fn apply<T: Suffix>(v: &T, (pat,): (&str,)) -> Result<(), Error> {
if !v.validate_suffix(pat) {
return Err(Error::new(format!("does not end with \"{pat}\"")));
}
Ok(())
}
#[cfg_attr(
feature = "nightly-error-messages",
rustc_on_unimplemented(
message = "`{Self}` does not support suffix validation",
label = "This type does not support suffix validation",
)
)]
pub trait Suffix {
fn validate_suffix(&self, pat: &str) -> bool;
}
impl<T: AsRef<str>> Suffix for T {
fn validate_suffix(&self, pat: &str) -> bool {
self.as_ref().ends_with(pat)
}
}