-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp.go
104 lines (88 loc) · 1.68 KB
/
exp.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package filter
import (
"fmt"
"io/fs"
"strings"
)
type OrExp []FileExp
type AndExp []FileExp
type NotExp struct {
filter FileExp
}
type AlwasyExp bool
var (
_ FileExp = (OrExp)(nil)
_ FileExp = (AndExp)(nil)
_ FileExp = (*NotExp)(nil)
)
func NewNotExp(f FileExp) *NotExp {
return &NotExp{f}
}
func NewOrExp(f FileExp) OrExp {
return OrExp{f}
}
func (e OrExp) Match(path string, info fs.DirEntry) (bool, error) {
if len(e) == 0 {
return true, nil
}
for _, filters := range e {
match, err := filters.Match(path, info)
if err != nil {
return false, err
}
if match {
return match, nil
}
}
return false, nil
}
func (e AndExp) Match(path string, info fs.DirEntry) (bool, error) {
for _, filters := range e {
match, err := filters.Match(path, info)
if err != nil {
return false, err
}
if !match {
return false, nil
}
}
return true, nil
}
func (n *NotExp) Match(path string, info fs.DirEntry) (bool, error) {
match, err := n.filter.Match(path, info)
if err != nil {
return false, err
}
return !match, nil
}
func (e OrExp) String() string {
var buf strings.Builder
for i, f := range e {
if i == 0 {
fmt.Fprintf(&buf, "%s", f)
continue
}
fmt.Fprintf(&buf, " || %s", f)
}
return buf.String()
}
func (a AlwasyExp) Match(path string, info fs.DirEntry) (bool, error) {
return bool(a), nil
}
func (e AndExp) String() string {
var buf strings.Builder
for i, f := range e {
if i == 0 {
fmt.Fprintf(&buf, "%s", f)
continue
}
fmt.Fprintf(&buf, " && %s", f)
}
return buf.String()
}
func (e *NotExp) String() string {
return fmt.Sprintf("not %s", e.filter)
}
func (a AlwasyExp) String() string {
return fmt.Sprintf("%t", bool(a))
}