package judgement import "fmt" // Level is how much autonomy an action runs with, from the most (act // quietly) to the least (never). A higher value means less autonomy. type Level int // The five levels, in order of decreasing autonomy. const ( ActQuietly Level = iota + 1 ActAndTell ProposeAndWait Escalate Never ) var levelNames = [...]string{ ActQuietly: "act_quietly", ActAndTell: "act_and_tell", ProposeAndWait: "propose_and_wait", Escalate: "escalate", Never: "never", } func (l Level) String() string { if l < ActQuietly || l > Never { return fmt.Sprintf("level(%d)", int(l)) } return levelNames[l] } // MarshalText writes the level's name. func (l Level) MarshalText() ([]byte, error) { if l < ActQuietly || l > Never { return nil, fmt.Errorf("no level %d", int(l)) } return []byte(levelNames[l]), nil } // UnmarshalText reads a level's name. func (l *Level) UnmarshalText(b []byte) error { for i, name := range levelNames { if name != "" && name == string(b) { *l = Level(i) return nil } } return fmt.Errorf("no level named %q", b) } // Runs reports whether an action at this level runs without waiting for // anyone. func (l Level) Runs() bool { return l == ActQuietly || l == ActAndTell } // lowest is the level with the least autonomy among levels. func lowest(levels ...Level) Level { out := ActQuietly for _, l := range levels { out = max(out, l) } return out }