File: enum.go

package info (click to toggle)
rclone 1.69.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 45,716 kB
  • sloc: sh: 1,115; xml: 857; python: 754; javascript: 612; makefile: 299; ansic: 101; php: 74
file content (110 lines) | stat: -rw-r--r-- 2,342 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package fs

import (
	"encoding/json"
	"fmt"
	"strings"
)

// Enum is an option which can only be one of the Choices.
//
// Suggested implementation is something like this:
//
//	type choice = Enum[choices]
//
//	const (
//		choiceA choice = iota
//		choiceB
//		choiceC
//	)
//
//	type choices struct{}
//
//	func (choices) Choices() []string {
//		return []string{
//			choiceA: "A",
//			choiceB: "B",
//			choiceC: "C",
//		}
//	}
type Enum[C Choices] byte

// Choices returns the valid choices for this type.
//
// It must work on the zero value.
//
// Note that when using this in an Option the ExampleChoices will be
// filled in automatically.
type Choices interface {
	// Choices returns the valid choices for this type
	Choices() []string
}

// String renders the Enum as a string
func (e Enum[C]) String() string {
	choices := e.Choices()
	if int(e) >= len(choices) {
		return fmt.Sprintf("Unknown(%d)", e)
	}
	return choices[e]
}

// Choices returns the possible values of the Enum.
func (e Enum[C]) Choices() []string {
	var c C
	return c.Choices()
}

// Help returns a comma separated list of all possible states.
func (e Enum[C]) Help() string {
	return strings.Join(e.Choices(), ", ")
}

// Set the Enum entries
func (e *Enum[C]) Set(s string) error {
	for i, choice := range e.Choices() {
		if strings.EqualFold(s, choice) {
			*e = Enum[C](i)
			return nil
		}
	}
	return fmt.Errorf("invalid choice %q from: %s", s, e.Help())
}

// Type of the value.
//
// If C has a Type() string method then it will be used instead.
func (e Enum[C]) Type() string {
	var c C
	if do, ok := any(c).(typer); ok {
		return do.Type()
	}
	return strings.Join(e.Choices(), "|")
}

// Scan implements the fmt.Scanner interface
func (e *Enum[C]) Scan(s fmt.ScanState, ch rune) error {
	token, err := s.Token(true, nil)
	if err != nil {
		return err
	}
	return e.Set(string(token))
}

// UnmarshalJSON parses it as a string or an integer
func (e *Enum[C]) UnmarshalJSON(in []byte) error {
	choices := e.Choices()
	return UnmarshalJSONFlag(in, e, func(i int64) error {
		if i < 0 || i >= int64(len(choices)) {
			return fmt.Errorf("%d is out of range: must be 0..%d", i, len(choices))
		}
		*e = Enum[C](i)
		return nil
	})

}

// MarshalJSON encodes it as string
func (e *Enum[C]) MarshalJSON() ([]byte, error) {
	return json.Marshal(e.String())
}