This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
trailer_type.go
76 lines (63 loc) · 1.55 KB
/
trailer_type.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
package nontransparent
import (
"fmt"
"strings"
)
// TrailerType is the king of supported trailers for non-transparent frames.
type TrailerType int
const (
// LF is the line feed - ie., byte 10. Also the default one.
LF TrailerType = iota
// NUL is the nul byte - ie., byte 0.
NUL
)
var names = [...]string{"LF", "NUL"}
var bytes = []int{10, 0}
func (t TrailerType) String() string {
if t < LF || t > NUL {
return ""
}
return names[t]
}
// Value returns the byte corresponding to the receiving TrailerType.
func (t TrailerType) Value() (int, error) {
if t < LF || t > NUL {
return -1, fmt.Errorf("unknown TrailerType")
}
return bytes[t], nil
}
// TrailerTypeFromString returns a TrailerType given a string.
func TrailerTypeFromString(s string) (TrailerType, error) {
switch strings.ToUpper(s) {
case `"LF"`:
fallthrough
case `'LF'`:
fallthrough
case `LF`:
return LF, nil
case `"NUL"`:
fallthrough
case `'NUL'`:
fallthrough
case `NUL`:
return NUL, nil
}
return -1, fmt.Errorf("unknown TrailerType")
}
// UnmarshalTOML decodes trailer type from TOML data.
func (t *TrailerType) UnmarshalTOML(data []byte) (err error) {
return t.UnmarshalText(data)
}
// UnmarshalText implements encoding.TextUnmarshaler
func (t *TrailerType) UnmarshalText(data []byte) (err error) {
*t, err = TrailerTypeFromString(string(data))
return err
}
// MarshalText implements encoding.TextMarshaler
func (t TrailerType) MarshalText() ([]byte, error) {
s := t.String()
if s != "" {
return []byte(s), nil
}
return nil, fmt.Errorf("unknown TrailerType")
}