-
Notifications
You must be signed in to change notification settings - Fork 74
/
fields.go
49 lines (41 loc) · 1.1 KB
/
fields.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
package main
import (
"fmt"
"regexp"
)
// FieldSet is a set of extension fields to include in a tag.
type FieldSet map[TagField]bool
// Includes tests whether the given field is included in the set.
func (f FieldSet) Includes(field TagField) bool {
b, ok := f[field]
return ok && b
}
// ErrInvalidFields is an error returned when attempting to parse invalid
// fields.
type ErrInvalidFields struct {
Fields string
}
func (e ErrInvalidFields) Error() string {
return fmt.Sprintf("invalid fields: %s", e.Fields)
}
// currently only "+l" is supported
var fieldsPattern = regexp.MustCompile(`^\+l$`)
func parseFields(fields string) (FieldSet, error) {
if fields == "" {
return FieldSet{}, nil
}
if fieldsPattern.MatchString(fields) {
return FieldSet{Language: true}, nil
}
return FieldSet{}, ErrInvalidFields{fields}
}
func parseExtraSymbols(symbols string) (FieldSet, error) {
symbolsPattern := regexp.MustCompile(`^\+q$`)
if symbols == "" {
return FieldSet{}, nil
}
if symbolsPattern.MatchString(symbols) {
return FieldSet{ExtraTags: true}, nil
}
return FieldSet{}, ErrInvalidFields{fields}
}