-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
83 lines (70 loc) · 1.65 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
package model
import (
"encoding/json"
"fmt"
)
// Status is an enumeration representing the supported statuses a channel can
// be in with respect to solving a puzzle. It can be marshalled to/from JSON as
// well as implements the fmt.Stringer interface for human readability.
type Status int
const (
// The channel exists, but a puzzle hasn't yet been selected.
StatusCreated Status = iota
// A puzzle has been selected, but not yet started.
StatusSelected
// The channel has a puzzle selected but is paused in its attempt to solve it.
StatusPaused
// The channel is actively trying to solve the puzzle.
StatusSolving
// The puzzle that was being solved is complete.
StatusComplete
)
func (s Status) String() string {
switch s {
case StatusCreated:
return "created"
case StatusSelected:
return "selected"
case StatusPaused:
return "paused"
case StatusSolving:
return "solving"
case StatusComplete:
return "complete"
default:
return "unknown"
}
}
func (s Status) MarshalJSON() ([]byte, error) {
switch s {
case StatusCreated:
case StatusSelected:
case StatusPaused:
case StatusSolving:
case StatusComplete:
default:
return nil, fmt.Errorf("unrecognized status: %v", s)
}
return json.Marshal(s.String())
}
func (s *Status) UnmarshalJSON(bs []byte) error {
var str string
if err := json.Unmarshal(bs, &str); err != nil {
return err
}
switch str {
case "created":
*s = StatusCreated
case "selected":
*s = StatusSelected
case "paused":
*s = StatusPaused
case "solving":
*s = StatusSolving
case "complete":
*s = StatusComplete
default:
return fmt.Errorf("unrecognized status string: %s", str)
}
return nil
}