mirror of
https://github.com/onyx-and-iris/lottery-cli.git
synced 2026-08-17 00:34:25 +00:00
add AllKinds to the public API. Use it as a single source of truth.
This commit is contained in:
63
kinds.go
63
kinds.go
@@ -1,6 +1,10 @@
|
||||
package lottery
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Kind string
|
||||
|
||||
@@ -12,22 +16,43 @@ const (
|
||||
KindPowerball Kind = "powerball"
|
||||
)
|
||||
|
||||
func ParseKind(kind string) (Kind, error) {
|
||||
switch kind {
|
||||
case "lotto":
|
||||
return KindLotto, nil
|
||||
case "euromillions":
|
||||
return KindEuroMillions, nil
|
||||
case "setforlife":
|
||||
return KindSetForLife, nil
|
||||
case "thunderball":
|
||||
return KindThunderball, nil
|
||||
case "powerball":
|
||||
return KindPowerball, nil
|
||||
default:
|
||||
return "", fmt.Errorf(
|
||||
"invalid lottery kind: %s, must be one of: lotto, euromillions, setforlife, thunderball, powerball",
|
||||
kind,
|
||||
)
|
||||
}
|
||||
var allKinds = []Kind{
|
||||
KindLotto,
|
||||
KindEuroMillions,
|
||||
KindSetForLife,
|
||||
KindThunderball,
|
||||
KindPowerball,
|
||||
}
|
||||
|
||||
var allKindsText = []string{
|
||||
string(KindLotto),
|
||||
string(KindEuroMillions),
|
||||
string(KindSetForLife),
|
||||
string(KindThunderball),
|
||||
string(KindPowerball),
|
||||
}
|
||||
|
||||
var allKindsCSV = strings.Join(allKindsText, ", ")
|
||||
|
||||
// AllKinds returns the complete, ordered list of supported lottery kinds.
|
||||
//
|
||||
// The returned slice is a copy and can be modified safely by callers.
|
||||
func AllKinds() []Kind {
|
||||
kinds := make([]Kind, len(allKinds))
|
||||
copy(kinds, allKinds)
|
||||
return kinds
|
||||
}
|
||||
|
||||
// ParseKind parses a string into a Kind, returning an error if the string is not a valid kind.
|
||||
func ParseKind(kind string) (Kind, error) {
|
||||
parsed := Kind(kind)
|
||||
if slices.Contains(allKinds, parsed) {
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf(
|
||||
"invalid lottery kind: %s, must be one of: %s",
|
||||
kind,
|
||||
allKindsCSV,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user