-
Notifications
You must be signed in to change notification settings - Fork 943
/
Copy pathcase.go
51 lines (43 loc) · 1.16 KB
/
case.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
package cm
// CaseUnmarshaler returns an function that can unmarshal text into
// [variant] or [enum] case T.
//
// [enum]: https://component-model.bytecodealliance.org/design/wit.html#enums
// [variant]: https://component-model.bytecodealliance.org/design/wit.html#variants
func CaseUnmarshaler[T ~uint8 | ~uint16 | ~uint32](cases []string) func(v *T, text []byte) error {
if len(cases) <= linearScanThreshold {
return func(v *T, text []byte) error {
if len(text) == 0 {
return &emptyTextError{}
}
s := string(text)
for i := 0; i < len(cases); i++ {
if cases[i] == s {
*v = T(i)
return nil
}
}
return &noMatchingCaseError{}
}
}
m := make(map[string]T, len(cases))
for i, v := range cases {
m[v] = T(i)
}
return func(v *T, text []byte) error {
if len(text) == 0 {
return &emptyTextError{}
}
c, ok := m[string(text)]
if !ok {
return &noMatchingCaseError{}
}
*v = c
return nil
}
}
const linearScanThreshold = 16
type emptyTextError struct{}
func (*emptyTextError) Error() string { return "empty text" }
type noMatchingCaseError struct{}
func (*noMatchingCaseError) Error() string { return "no matching case" }