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