Make enum values typed strings (#18)

This also moves validation into the parsing process and refactors a bunch of code related to the config.

Reviewed-on: #18
Co-authored-by: Anna Rose Wiggins <annabunches@gmail.com>
Co-committed-by: Anna Rose Wiggins <annabunches@gmail.com>
This commit is contained in:
Anna Rose Wiggins 2025-09-05 21:17:55 +00:00 committed by Anna Rose Wiggins
parent 8d2b15a7c8
commit 8a903e0703
10 changed files with 232 additions and 173 deletions

View file

@ -0,0 +1,53 @@
package configparser
import (
"fmt"
"strings"
)
// TODO: maybe these want to live somewhere other than configparser?
type RuleType string
const (
RuleTypeNone RuleType = ""
RuleTypeButton RuleType = "button"
RuleTypeButtonCombo RuleType = "button-combo"
RuleTypeButtonLatched RuleType = "button-latched"
RuleTypeAxis RuleType = "axis"
RuleTypeAxisCombined RuleType = "axis-combined"
RuleTypeAxisToButton RuleType = "axis-to-button"
RuleTypeAxisToRelaxis RuleType = "axis-to-relaxis"
RuleTypeModeSelect RuleType = "mode-select"
)
var (
ruleTypeMap = map[string]RuleType{
"button": RuleTypeButton,
"button-combo": RuleTypeButtonCombo,
"button-latched": RuleTypeButtonLatched,
"axis": RuleTypeAxis,
"axis-combined": RuleTypeAxisCombined,
"axis-to-button": RuleTypeAxisToButton,
"axis-to-relaxis": RuleTypeAxisToRelaxis,
"mode-select": RuleTypeModeSelect,
}
)
func ParseRuleType(in string) (RuleType, error) {
ruleType, ok := ruleTypeMap[strings.ToLower(in)]
if !ok {
return RuleTypeNone, fmt.Errorf("invalid rule type '%s'", in)
}
return ruleType, nil
}
func (rt *RuleType) UnmarshalYAML(unmarshal func(data interface{}) error) error {
var raw string
err := unmarshal(&raw)
if err != nil {
return err
}
*rt, err = ParseRuleType(raw)
return err
}