-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatcher.go
61 lines (49 loc) · 1.23 KB
/
matcher.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package pepper
type Matcher[T any] func(T) MatchResult
// Doesnt is a helper function to negate a matcher.
func Doesnt[T any](matcher Matcher[T]) Matcher[T] {
return negate(matcher)
}
// Not is a helper function to negate a matcher.
func Not[T any](matcher Matcher[T]) Matcher[T] {
return negate(matcher)
}
// Or combines matchers with a boolean OR.
func (m Matcher[T]) Or(matchers ...Matcher[T]) Matcher[T] {
return func(got T) MatchResult {
result := m(got)
for _, matcher := range matchers {
result.Description += " or " + matcher(got).Description
}
if result.Matches {
return result
}
for _, matcher := range matchers {
if r := matcher(got); r.Matches {
result.Matches = true
return result
}
}
return result
}
}
// And combines matchers with a boolean AND.
func (m Matcher[T]) And(matchers ...Matcher[T]) Matcher[T] {
return func(got T) MatchResult {
result := m(got)
for _, matcher := range matchers {
r := matcher(got)
result = result.Combine(r)
}
return result
}
}
func negate[T any](matcher Matcher[T]) Matcher[T] {
return func(got T) MatchResult {
result := matcher(got)
return MatchResult{
Description: "not " + result.Description,
Matches: !result.Matches,
}
}
}