// 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 (
	TorrentContentOrderByRelevance   TorrentContentOrderBy = "relevance"
	TorrentContentOrderByPublishedAt TorrentContentOrderBy = "published_at"
	TorrentContentOrderByUpdatedAt   TorrentContentOrderBy = "updated_at"
	TorrentContentOrderBySize        TorrentContentOrderBy = "size"
	TorrentContentOrderByFilesCount  TorrentContentOrderBy = "files_count"
	TorrentContentOrderBySeeders     TorrentContentOrderBy = "seeders"
	TorrentContentOrderByLeechers    TorrentContentOrderBy = "leechers"
	TorrentContentOrderByName        TorrentContentOrderBy = "name"
	TorrentContentOrderByInfoHash    TorrentContentOrderBy = "info_hash"
)

var ErrInvalidTorrentContentOrderBy = fmt.Errorf("not a valid TorrentContentOrderBy, try [%s]", strings.Join(_TorrentContentOrderByNames, ", "))

var _TorrentContentOrderByNames = []string{
	string(TorrentContentOrderByRelevance),
	string(TorrentContentOrderByPublishedAt),
	string(TorrentContentOrderByUpdatedAt),
	string(TorrentContentOrderBySize),
	string(TorrentContentOrderByFilesCount),
	string(TorrentContentOrderBySeeders),
	string(TorrentContentOrderByLeechers),
	string(TorrentContentOrderByName),
	string(TorrentContentOrderByInfoHash),
}

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

// TorrentContentOrderByValues returns a list of the values for TorrentContentOrderBy
func TorrentContentOrderByValues() []TorrentContentOrderBy {
	return []TorrentContentOrderBy{
		TorrentContentOrderByRelevance,
		TorrentContentOrderByPublishedAt,
		TorrentContentOrderByUpdatedAt,
		TorrentContentOrderBySize,
		TorrentContentOrderByFilesCount,
		TorrentContentOrderBySeeders,
		TorrentContentOrderByLeechers,
		TorrentContentOrderByName,
		TorrentContentOrderByInfoHash,
	}
}

// String implements the Stringer interface.
func (x TorrentContentOrderBy) 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 TorrentContentOrderBy) IsValid() bool {
	_, err := ParseTorrentContentOrderBy(string(x))
	return err == nil
}

var _TorrentContentOrderByValue = map[string]TorrentContentOrderBy{
	"relevance":    TorrentContentOrderByRelevance,
	"published_at": TorrentContentOrderByPublishedAt,
	"updated_at":   TorrentContentOrderByUpdatedAt,
	"size":         TorrentContentOrderBySize,
	"files_count":  TorrentContentOrderByFilesCount,
	"seeders":      TorrentContentOrderBySeeders,
	"leechers":     TorrentContentOrderByLeechers,
	"name":         TorrentContentOrderByName,
	"info_hash":    TorrentContentOrderByInfoHash,
}

// ParseTorrentContentOrderBy attempts to convert a string to a TorrentContentOrderBy.
func ParseTorrentContentOrderBy(name string) (TorrentContentOrderBy, error) {
	if x, ok := _TorrentContentOrderByValue[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 := _TorrentContentOrderByValue[strings.ToLower(name)]; ok {
		return x, nil
	}
	return TorrentContentOrderBy(""), fmt.Errorf("%s is %w", name, ErrInvalidTorrentContentOrderBy)
}

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

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

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

// Scan implements the Scanner interface.
func (x *TorrentContentOrderBy) Scan(value interface{}) (err error) {
	if value == nil {
		*x = TorrentContentOrderBy("")
		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 = ParseTorrentContentOrderBy(v)
	case []byte:
		*x, err = ParseTorrentContentOrderBy(string(v))
	case TorrentContentOrderBy:
		*x = v
	case *TorrentContentOrderBy:
		if v == nil {
			return errTorrentContentOrderByNilPtr
		}
		*x = *v
	case *string:
		if v == nil {
			return errTorrentContentOrderByNilPtr
		}
		*x, err = ParseTorrentContentOrderBy(*v)
	default:
		return errors.New("invalid type for TorrentContentOrderBy")
	}

	return
}

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

type NullTorrentContentOrderBy struct {
	TorrentContentOrderBy TorrentContentOrderBy
	Valid                 bool
	Set                   bool
}

func NewNullTorrentContentOrderBy(val interface{}) (x NullTorrentContentOrderBy) {
	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 *NullTorrentContentOrderBy) Scan(value interface{}) (err error) {
	if value == nil {
		x.TorrentContentOrderBy, x.Valid = TorrentContentOrderBy(""), false
		return
	}

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

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

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

// UnmarshalJSON correctly deserializes a NullTorrentContentOrderBy from JSON.
func (n *NullTorrentContentOrderBy) 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
}
