xpct/matchers/strings/
casefold.rs

1use std::borrow::Cow;
2
3use unicase::UniCase;
4
5use crate::core::Match;
6use crate::matchers::Mismatch;
7
8/// The matcher for [`eq_casefold`].
9///
10/// [`eq_casefold`]: crate::eq_casefold
11#[derive(Debug)]
12pub struct EqCasefoldMatcher<'a> {
13    expected: Cow<'a, str>,
14}
15
16impl<'a> EqCasefoldMatcher<'a> {
17    /// Create a new [`EqCasefoldMatcher`] from the expected string.
18    pub fn new(expected: impl Into<Cow<'a, str>>) -> Self {
19        Self {
20            expected: expected.into(),
21        }
22    }
23}
24
25impl<'a, Actual> Match<Actual> for EqCasefoldMatcher<'a>
26where
27    Actual: AsRef<str>,
28{
29    type Fail = Mismatch<Cow<'a, str>, Actual>;
30
31    fn matches(&mut self, actual: &Actual) -> crate::Result<bool> {
32        Ok(UniCase::new(actual.as_ref()) == UniCase::new(self.expected.as_ref()))
33    }
34
35    fn fail(self, actual: Actual) -> Self::Fail {
36        Mismatch {
37            expected: self.expected,
38            actual,
39        }
40    }
41}