fs: re-implement CutoffMode, LogLevel, TerminalColorMode with Enum

This almost 100% backwards compatible. The only difference being that
in the rc options/get output CutoffMode, LogLevel, TerminalColorMode
will be output as strings instead of integers. This is a lot more
convenient for the user. They still accept integer inputs though so
the fallout from this should be minimal.
This commit is contained in:
Nick Craig-Wood
2023-09-27 15:31:47 +01:00
parent 60a6ef914c
commit 3092f82dcc
8 changed files with 53 additions and 145 deletions

View File

@ -1,58 +1,22 @@
package fs
import (
"fmt"
"strings"
)
type cutoffModeChoices struct{}
func (cutoffModeChoices) Choices() []string {
return []string{
CutoffModeHard: "HARD",
CutoffModeSoft: "SOFT",
CutoffModeCautious: "CAUTIOUS",
}
}
// CutoffMode describes the possible delete modes in the config
type CutoffMode byte
type CutoffMode = Enum[cutoffModeChoices]
// MaxTransferMode constants
// CutoffMode constants
const (
CutoffModeHard CutoffMode = iota
CutoffModeSoft
CutoffModeCautious
CutoffModeDefault = CutoffModeHard
)
var cutoffModeToString = []string{
CutoffModeHard: "HARD",
CutoffModeSoft: "SOFT",
CutoffModeCautious: "CAUTIOUS",
}
// String turns a LogLevel into a string
func (m CutoffMode) String() string {
if m >= CutoffMode(len(cutoffModeToString)) {
return fmt.Sprintf("CutoffMode(%d)", m)
}
return cutoffModeToString[m]
}
// Set a LogLevel
func (m *CutoffMode) Set(s string) error {
for n, name := range cutoffModeToString {
if s != "" && name == strings.ToUpper(s) {
*m = CutoffMode(n)
return nil
}
}
return fmt.Errorf("unknown cutoff mode %q", s)
}
// Type of the value
func (m CutoffMode) Type() string {
return "string"
}
// UnmarshalJSON makes sure the value can be parsed as a string or integer in JSON
func (m *CutoffMode) UnmarshalJSON(in []byte) error {
return UnmarshalJSONFlag(in, m, func(i int64) error {
if i < 0 || i >= int64(len(cutoffModeToString)) {
return fmt.Errorf("out of range cutoff mode %d", i)
}
*m = (CutoffMode)(i)
return nil
})
}