mirror of
https://github.com/rclone/rclone.git
synced 2025-08-16 00:28:09 +02:00
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:
17
fs/enum.go
17
fs/enum.go
@ -91,14 +91,17 @@ func (e *Enum[C]) Scan(s fmt.ScanState, ch rune) error {
|
||||
return e.Set(string(token))
|
||||
}
|
||||
|
||||
// UnmarshalJSON parses it as a string
|
||||
// UnmarshalJSON parses it as a string or an integer
|
||||
func (e *Enum[C]) UnmarshalJSON(in []byte) error {
|
||||
var choice string
|
||||
err := json.Unmarshal(in, &choice)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return e.Set(choice)
|
||||
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
|
||||
|
Reference in New Issue
Block a user