govote/voting/vote/choice.go

41 lines
645 B
Go
Raw Normal View History

2024-05-12 22:47:24 +00:00
package vote
import (
"fmt"
"strings"
)
type Choice int8
func (choice Choice) String() string {
switch choice {
case Yes:
return "YIP"
case No:
return "NOPE"
case Abstain:
return "DUNNO"
default:
panic("this code should never be reached")
}
}
const (
Abstain Choice = 0
Yes Choice = 1
No Choice = -1
)
func ValidChoices() []Choice {
return []Choice{Yes, No, Abstain}
}
func ChoiceFromString(s string) (Choice, error) {
for _, c := range ValidChoices() {
if strings.ToUpper(c.String()) == strings.ToUpper(s) {
return c, nil
}
}
return Abstain, fmt.Errorf("invalid choice: %s", s)
}