halt_state.gno
1.38 Kb ยท 64 lines
1package halt
2
3type HaltStateManager map[OpType]bool
4
5func (h HaltStateManager) IsOperationHalted(op OpType) (bool, error) {
6 return h.getOperationHalted(op)
7}
8
9// ToConfig converts the `HaltStateManager` to a HaltConfig.
10func (h HaltStateManager) ToConfig() HaltConfig {
11 config := make(HaltConfig)
12 for op, halted := range h {
13 config[op] = halted
14 }
15 return config
16}
17
18// getOperationHalted retrieves halt state for the specified operation type.
19//
20// Errors:
21// - errOpTypeNotFound: the specified operation type does not exist in the manager
22func (h HaltStateManager) getOperationHalted(op OpType) (bool, error) {
23 halted, exists := h[op]
24 if !exists {
25 return false, makeErrorWithDetails(errOpTypeNotFound, op.String())
26 }
27
28 return halted, nil
29}
30
31func (h HaltStateManager) setOperationHalted(op OpType, halted bool) error {
32 if !op.IsValid() {
33 return makeErrorWithDetails(errInvalidOpType, op.String())
34 }
35
36 h[op] = halted
37
38 return nil
39}
40
41func (h HaltStateManager) updateOperationHaltsByConfig(config HaltConfig) error {
42 for op, halted := range config {
43 if err := h.setOperationHalted(op, halted); err != nil {
44 return err
45 }
46 }
47
48 return nil
49}
50
51func newHaltStateManagerByConfig(config HaltConfig) HaltStateManager {
52 haltState := make(HaltStateManager)
53
54 for _, op := range OpTypes() {
55 halted, exists := config[op]
56 if !exists {
57 halted = true
58 }
59
60 haltState[op] = halted
61 }
62
63 return haltState
64}