-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
83 lines (72 loc) · 2.02 KB
/
state.go
File metadata and controls
83 lines (72 loc) · 2.02 KB
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
package flux
import (
"encoding"
"encoding/json"
"fmt"
)
// State represents the learning stage of a card.
type State int
const (
Learning State = iota + 1 // New card, in initial learning.
Review // Entered long-term review cycle.
Relearning // Forgotten, relearning.
)
var (
stateNames = [...]string{Learning: "Learning", Review: "Review", Relearning: "Relearning"}
stateByName = map[string]State{
"Learning": Learning,
"Review": Review,
"Relearning": Relearning,
}
)
// Compile-time interface checks.
var (
_ fmt.Stringer = State(0)
_ json.Marshaler = State(0)
_ json.Unmarshaler = (*State)(nil)
_ encoding.TextMarshaler = State(0)
_ encoding.TextUnmarshaler = (*State)(nil)
)
func (s State) isValid() bool {
return s >= Learning && s <= Relearning
}
// String returns the name of the state ("Learning", "Review", "Relearning").
// For invalid values it returns "State(n)".
func (s State) String() string {
if s.isValid() {
return stateNames[s]
}
return fmt.Sprintf("State(%d)", int(s))
}
// MarshalText implements encoding.TextMarshaler.
func (s State) MarshalText() ([]byte, error) {
if !s.isValid() {
return nil, fmt.Errorf("flux: invalid state: %d", int(s))
}
return []byte(stateNames[s]), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (s *State) UnmarshalText(text []byte) error {
v, ok := stateByName[string(text)]
if !ok {
return fmt.Errorf("flux: invalid state: %q", text)
}
*s = v
return nil
}
// MarshalJSON implements json.Marshaler. State serializes as a JSON string.
func (s State) MarshalJSON() ([]byte, error) {
text, err := s.MarshalText()
if err != nil {
return nil, err
}
return json.Marshal(string(text))
}
// UnmarshalJSON implements json.Unmarshaler. Expects a JSON string.
func (s *State) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return fmt.Errorf("flux: invalid state: %s", data)
}
return s.UnmarshalText([]byte(str))
}