// Code generated by go-enum DO NOT EDIT.
// Version:
// Revision:
// Build Date:
// Built By:

package search

import (
	"database/sql/driver"
	"encoding/json"
	"errors"
	"fmt"
	"strings"
)

const (
	TorrentFilesOrderByIndex     TorrentFilesOrderBy = "index"
	TorrentFilesOrderByPath      TorrentFilesOrderBy = "path"
	TorrentFilesOrderByExtension TorrentFilesOrderBy = "extension"
	TorrentFilesOrderBySize      TorrentFilesOrderBy = "size"
)

var ErrInvalidTorrentFilesOrderBy = fmt.Errorf("not a valid TorrentFilesOrderBy, try [%s]", strings.Join(_TorrentFilesOrderByNames, ", "))

var _TorrentFilesOrderByNames = []string{
	string(TorrentFilesOrderByIndex),
	string(TorrentFilesOrderByPath),
	string(TorrentFilesOrderByExtension),
	string(TorrentFilesOrderBySize),
}

// TorrentFilesOrderByNames returns a list of possible string values of TorrentFilesOrderBy.
func TorrentFilesOrderByNames() []string {
	tmp := make([]string, len(_TorrentFilesOrderByNames))
	copy(tmp, _TorrentFilesOrderByNames)
	return tmp
}

// TorrentFilesOrderByValues returns a list of the values for TorrentFilesOrderBy
func TorrentFilesOrderByValues() []TorrentFilesOrderBy {
	return []TorrentFilesOrderBy{
		TorrentFilesOrderByIndex,
		TorrentFilesOrderByPath,
		TorrentFilesOrderByExtension,
		TorrentFilesOrderBySize,
	}
}

// String implements the Stringer interface.
func (x TorrentFilesOrderBy) String() string {
	return string(x)
}

// IsValid provides a quick way to determine if the typed value is
// part of the allowed enumerated values
func (x TorrentFilesOrderBy) IsValid() bool {
	_, err := ParseTorrentFilesOrderBy(string(x))
	return err == nil
}

var _TorrentFilesOrderByValue = map[string]TorrentFilesOrderBy{
	"index":     TorrentFilesOrderByIndex,
	"path":      TorrentFilesOrderByPath,
	"extension": TorrentFilesOrderByExtension,
	"size":      TorrentFilesOrderBySize,
}

// ParseTorrentFilesOrderBy attempts to convert a string to a TorrentFilesOrderBy.
func ParseTorrentFilesOrderBy(name string) (TorrentFilesOrderBy, error) {
	if x, ok := _TorrentFilesOrderByValue[name]; ok {
		return x, nil
	}
	// Case insensitive parse, do a separate lookup to prevent unnecessary cost of lowercasing a string if we don't need to.
	if x, ok := _TorrentFilesOrderByValue[strings.ToLower(name)]; ok {
		return x, nil
	}
	return TorrentFilesOrderBy(""), fmt.Errorf("%s is %w", name, ErrInvalidTorrentFilesOrderBy)
}

// MarshalText implements the text marshaller method.
func (x TorrentFilesOrderBy) MarshalText() ([]byte, error) {
	return []byte(string(x)), nil
}

// UnmarshalText implements the text unmarshaller method.
func (x *TorrentFilesOrderBy) UnmarshalText(text []byte) error {
	tmp, err := ParseTorrentFilesOrderBy(string(text))
	if err != nil {
		return err
	}
	*x = tmp
	return nil
}

var errTorrentFilesOrderByNilPtr = errors.New("value pointer is nil") // one per type for package clashes

// Scan implements the Scanner interface.
func (x *TorrentFilesOrderBy) Scan(value interface{}) (err error) {
	if value == nil {
		*x = TorrentFilesOrderBy("")
		return
	}

	// A wider range of scannable types.
	// driver.Value values at the top of the list for expediency
	switch v := value.(type) {
	case string:
		*x, err = ParseTorrentFilesOrderBy(v)
	case []byte:
		*x, err = ParseTorrentFilesOrderBy(string(v))
	case TorrentFilesOrderBy:
		*x = v
	case *TorrentFilesOrderBy:
		if v == nil {
			return errTorrentFilesOrderByNilPtr
		}
		*x = *v
	case *string:
		if v == nil {
			return errTorrentFilesOrderByNilPtr
		}
		*x, err = ParseTorrentFilesOrderBy(*v)
	default:
		return errors.New("invalid type for TorrentFilesOrderBy")
	}

	return
}

// Value implements the driver Valuer interface.
func (x TorrentFilesOrderBy) Value() (driver.Value, error) {
	return x.String(), nil
}

type NullTorrentFilesOrderBy struct {
	TorrentFilesOrderBy TorrentFilesOrderBy
	Valid               bool
	Set                 bool
}

func NewNullTorrentFilesOrderBy(val interface{}) (x NullTorrentFilesOrderBy) {
	err := x.Scan(val) // yes, we ignore this error, it will just be an invalid value.
	_ = err            // make any errcheck linters happy
	return
}

// Scan implements the Scanner interface.
func (x *NullTorrentFilesOrderBy) Scan(value interface{}) (err error) {
	if value == nil {
		x.TorrentFilesOrderBy, x.Valid = TorrentFilesOrderBy(""), false
		return
	}

	err = x.TorrentFilesOrderBy.Scan(value)
	x.Valid = (err == nil)
	return
}

// Value implements the driver Valuer interface.
func (x NullTorrentFilesOrderBy) Value() (driver.Value, error) {
	if !x.Valid {
		return nil, nil
	}
	return x.TorrentFilesOrderBy.String(), nil
}

// MarshalJSON correctly serializes a NullTorrentFilesOrderBy to JSON.
func (n NullTorrentFilesOrderBy) MarshalJSON() ([]byte, error) {
	const nullStr = "null"
	if n.Valid {
		return json.Marshal(n.TorrentFilesOrderBy)
	}
	return []byte(nullStr), nil
}

// UnmarshalJSON correctly deserializes a NullTorrentFilesOrderBy from JSON.
func (n *NullTorrentFilesOrderBy) UnmarshalJSON(b []byte) error {
	n.Set = true
	var x interface{}
	err := json.Unmarshal(b, &x)
	if err != nil {
		return err
	}
	err = n.Scan(x)
	return err
}
