use core::{fmt, marker::PhantomData};
use refining_core::{predicate::Predicate, types::StaticStr};
use crate::types::{StaticRegex, TypeRegex};
pub struct Matches<R: TypeRegex + ?Sized> {
regex: PhantomData<R>,
}
pub const MATCHES: &str = "regex::matches";
impl<R: TypeRegex + ?Sized> Matches<R> {
#[must_use]
pub fn regex() -> StaticRegex {
R::get()
}
#[must_use]
pub fn pattern() -> StaticStr {
Self::regex().as_str()
}
}
impl<T: AsRef<str> + ?Sized, R: TypeRegex + ?Sized> Predicate<T> for Matches<R> {
fn check(value: &T) -> bool {
Self::regex().is_match(value.as_ref())
}
fn expect(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"string matching `{pattern}` pattern",
pattern = Self::pattern()
)
}
fn expect_code(formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(MATCHES)
}
}