41 lines
626 B
Go
41 lines
626 B
Go
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)
|
|
}
|