Refactor and cleanup config parsing. Again.

This commit is contained in:
Anna Rose Wiggins 2025-09-05 17:16:11 -04:00
parent 553966ac87
commit b77135eab8
7 changed files with 220 additions and 154 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
}