testing_library_dom/types/
matches.rs

1use std::{fmt::Display, rc::Rc};
2
3use aria_query::AriaRole;
4use regex::Regex;
5use web_sys::Element;
6
7pub type MatcherFunction = dyn Fn(String, Option<&Element>) -> bool;
8
9#[derive(Clone)]
10pub enum Matcher {
11    Function(Rc<MatcherFunction>),
12    Regex(Regex),
13    Number(f64),
14    String(String),
15}
16
17impl Display for Matcher {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        write!(
20            f,
21            "{}",
22            match self {
23                Self::Function(_) => "MatcherFn".to_string(),
24                Self::Regex(regex) => regex.to_string(),
25                Self::Number(n) => n.to_string(),
26                Self::String(s) => s.clone(),
27            }
28        )
29    }
30}
31
32impl From<Rc<MatcherFunction>> for Matcher {
33    fn from(value: Rc<MatcherFunction>) -> Self {
34        Self::Function(value)
35    }
36}
37
38impl From<Regex> for Matcher {
39    fn from(value: Regex) -> Self {
40        Self::Regex(value)
41    }
42}
43
44impl From<f64> for Matcher {
45    fn from(value: f64) -> Self {
46        Self::Number(value)
47    }
48}
49
50impl From<&str> for Matcher {
51    fn from(value: &str) -> Self {
52        Self::String(value.to_string())
53    }
54}
55
56impl From<String> for Matcher {
57    fn from(value: String) -> Self {
58        Self::String(value)
59    }
60}
61
62pub type ByRoleMatcher = AriaRole;
63
64pub type NormalizerFn = dyn Fn(String) -> String;
65
66#[derive(Default)]
67pub struct NormalizerOptions {
68    pub trim: Option<bool>,
69    pub collapse_whitespace: Option<bool>,
70    pub normalizer: Option<Rc<NormalizerFn>>,
71}
72
73#[derive(Clone, Default)]
74pub struct MatcherOptions {
75    pub exact: Option<bool>,
76    pub trim: Option<bool>,
77    pub collapse_whitespace: Option<bool>,
78    pub normalizer: Option<Rc<NormalizerFn>>,
79    pub suggest: Option<bool>,
80}
81
82impl MatcherOptions {
83    pub fn exact(mut self, value: bool) -> Self {
84        self.exact = Some(value);
85        self
86    }
87
88    pub fn trim(mut self, value: bool) -> Self {
89        self.trim = Some(value);
90        self
91    }
92
93    pub fn collapse_whitespace(mut self, value: bool) -> Self {
94        self.collapse_whitespace = Some(value);
95        self
96    }
97
98    pub fn normalizer(mut self, value: Rc<NormalizerFn>) -> Self {
99        self.normalizer = Some(value);
100        self
101    }
102
103    pub fn suggest(mut self, value: bool) -> Self {
104        self.suggest = Some(value);
105        self
106    }
107}
108
109#[derive(Clone, Debug, PartialEq)]
110pub enum Ignore {
111    False,
112    String(String),
113}
114
115impl From<String> for Ignore {
116    fn from(value: String) -> Self {
117        Ignore::String(value)
118    }
119}
120
121#[derive(Clone, Default)]
122pub struct SelectorMatcherOptions {
123    pub exact: Option<bool>,
124    pub trim: Option<bool>,
125    pub collapse_whitespace: Option<bool>,
126    pub normalizer: Option<Rc<NormalizerFn>>,
127    pub suggest: Option<bool>,
128    pub selector: Option<String>,
129    pub ignore: Option<Ignore>,
130}
131
132impl SelectorMatcherOptions {
133    pub fn exact(mut self, value: bool) -> Self {
134        self.exact = Some(value);
135        self
136    }
137
138    pub fn trim(mut self, value: bool) -> Self {
139        self.trim = Some(value);
140        self
141    }
142
143    pub fn collapse_whitespace(mut self, value: bool) -> Self {
144        self.collapse_whitespace = Some(value);
145        self
146    }
147
148    pub fn normalizer(mut self, value: Rc<NormalizerFn>) -> Self {
149        self.normalizer = Some(value);
150        self
151    }
152
153    pub fn suggest(mut self, value: bool) -> Self {
154        self.suggest = Some(value);
155        self
156    }
157
158    pub fn selector(mut self, value: String) -> Self {
159        self.selector = Some(value);
160        self
161    }
162
163    pub fn ignore(mut self, value: Ignore) -> Self {
164        self.ignore = Some(value);
165        self
166    }
167}
168
169#[derive(Default)]
170pub struct DefaultNormalizerOptions {
171    pub trim: Option<bool>,
172    pub collapse_whitespace: Option<bool>,
173}