-
Notifications
You must be signed in to change notification settings - Fork 672
/
type.go
59 lines (51 loc) · 1.04 KB
/
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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package compression
import (
"errors"
"strings"
)
var errUnknownCompressionType = errors.New("unknown compression type")
type Type byte
const (
TypeNone Type = iota + 1
TypeGzip
TypeZstd
)
func (t Type) String() string {
switch t {
case TypeNone:
return "none"
case TypeGzip:
return "gzip"
case TypeZstd:
return "zstd"
default:
return "unknown"
}
}
func TypeFromString(s string) (Type, error) {
switch s {
case TypeNone.String():
return TypeNone, nil
case TypeGzip.String():
return TypeGzip, nil
case TypeZstd.String():
return TypeZstd, nil
default:
return TypeNone, errUnknownCompressionType
}
}
func (t Type) MarshalJSON() ([]byte, error) {
var b strings.Builder
if _, err := b.WriteString(`"`); err != nil {
return nil, err
}
if _, err := b.WriteString(t.String()); err != nil {
return nil, err
}
if _, err := b.WriteString(`"`); err != nil {
return nil, err
}
return []byte(b.String()), nil
}