-
Notifications
You must be signed in to change notification settings - Fork 669
/
status.go
105 lines (92 loc) · 1.98 KB
/
status.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package choices
import (
"errors"
"github.com/ava-labs/avalanchego/utils/wrappers"
)
var errUnknownStatus = errors.New("unknown status")
type Status uint32
// List of possible status values
// [Unknown] Zero value, means the operation is not known
// [Processing] means the operation is known, but hasn't been decided yet
// [Rejected] means the operation will never be accepted
// [Accepted] means the operation was accepted
const (
Unknown Status = iota
Processing
Rejected
Accepted
)
func (s Status) MarshalJSON() ([]byte, error) {
if err := s.Valid(); err != nil {
return nil, err
}
return []byte("\"" + s.String() + "\""), nil
}
func (s *Status) UnmarshalJSON(b []byte) error {
str := string(b)
if str == "null" {
return nil
}
switch str {
case "\"Unknown\"":
*s = Unknown
case "\"Processing\"":
*s = Processing
case "\"Rejected\"":
*s = Rejected
case "\"Accepted\"":
*s = Accepted
default:
return errUnknownStatus
}
return nil
}
// Fetched returns true if the status has been set.
func (s Status) Fetched() bool {
switch s {
case Processing:
return true
default:
return s.Decided()
}
}
// Decided returns true if the status is Rejected or Accepted.
func (s Status) Decided() bool {
switch s {
case Rejected, Accepted:
return true
default:
return false
}
}
// Valid returns nil if the status is a valid status.
func (s Status) Valid() error {
switch s {
case Unknown, Processing, Rejected, Accepted:
return nil
default:
return errUnknownStatus
}
}
func (s Status) String() string {
switch s {
case Unknown:
return "Unknown"
case Processing:
return "Processing"
case Rejected:
return "Rejected"
case Accepted:
return "Accepted"
default:
return "Invalid status"
}
}
// Bytes returns the byte repr. of this status
func (s Status) Bytes() []byte {
p := wrappers.Packer{Bytes: make([]byte, 4)}
p.PackInt(uint32(s))
return p.Bytes
}