-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_breaker.go
80 lines (71 loc) · 2.25 KB
/
list_breaker.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
package commander
import (
"github.com/leep-frog/command/command"
"github.com/leep-frog/command/internal/operator"
)
// ListUntilSymbol returns an unbounded list node that ends when a specific symbol is parsed.
func ListUntilSymbol[T comparable](symbol T) *ListBreaker[[]T] {
return &ListBreaker[[]T]{
Validators: []*ValidatorOption[[]T]{
ListifyValidatorOption(NEQ[T](symbol)),
},
UsageFunc: func(d *command.Data, u *command.Usage) error {
arg := operator.GetOperator[T]().ToArgs(symbol)[0] // slices aren't comparable, so this will only ever be length 1
u.AddSymbol(arg, "List breaker")
return nil
},
}
}
// ListUntil returns a `ListBreaker` node that breaks when any of the provided `ValidatorOptions` are not satisfied.
func ListUntil[T any](validators ...*ValidatorOption[T]) *ListBreaker[[]T] {
var listValidators []*ValidatorOption[[]T]
for _, v := range validators {
listValidators = append(listValidators, ListifyValidatorOption(v))
}
return &ListBreaker[[]T]{
Validators: listValidators,
}
}
// ListBreaker is a type that implements `command.InputBreaker` as well as `ArgumentOtion[T]`.
type ListBreaker[T any] struct {
// Validators is the list of validators
Validators []*ValidatorOption[T]
// Discard is whether the culprit character should be removed
Discard bool
// UsageFunc modifies the usage doc
UsageFunc func(*command.Data, *command.Usage) error
}
func (lb *ListBreaker[T]) DiscardBreak(s string, d *command.Data) bool {
return lb.Discard
}
func (lb *ListBreaker[T]) Break(s string, d *command.Data) bool {
for _, v := range lb.Validators {
op := operator.GetOperator[T]()
args, err := operator.FromArgs(op, s)
if err != nil {
continue
}
if err := v.Validate(args, d); err != nil {
return true
}
}
return false
}
func (lb *ListBreaker[T]) Validate(t T, d *command.Data) error {
for _, v := range lb.Validators {
if err := v.Validate(t, d); err != nil {
return err
}
}
return nil
}
func (lb *ListBreaker[T]) modifyArgumentOption(ao *argumentOption[T]) {
ao.breakers = append(ao.breakers, lb)
}
// command.Usage updates the provided `command.Usage` object.
func (lb *ListBreaker[T]) Usage(d *command.Data, u *command.Usage) error {
if lb.UsageFunc != nil {
return lb.UsageFunc(d, u)
}
return nil
}