joyful/internal/configparser/ruletype.go
Anna Rose Wiggins 8a903e0703 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>
2025-09-05 21:17:55 +00:00

53 lines
1.3 KiB
Go

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
}