github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/neatptc/downloader/modes.go (about)

     1  package downloader
     2  
     3  import "fmt"
     4  
     5  type SyncMode int
     6  
     7  const (
     8  	FullSync SyncMode = iota
     9  	FastSync
    10  )
    11  
    12  func (mode SyncMode) IsValid() bool {
    13  	return mode >= FullSync
    14  }
    15  
    16  func (mode SyncMode) String() string {
    17  	switch mode {
    18  	case FullSync:
    19  		return "full"
    20  	case FastSync:
    21  		return "fast"
    22  	default:
    23  		return "unknown"
    24  	}
    25  }
    26  
    27  func (mode SyncMode) MarshalText() ([]byte, error) {
    28  	switch mode {
    29  	case FullSync:
    30  		return []byte("full"), nil
    31  	case FastSync:
    32  		return []byte("fast"), nil
    33  	default:
    34  		return nil, fmt.Errorf("unknown sync mode %d", mode)
    35  	}
    36  }
    37  
    38  func (mode *SyncMode) UnmarshalText(text []byte) error {
    39  	switch string(text) {
    40  	case "full":
    41  		*mode = FullSync
    42  	case "fast":
    43  		*mode = FastSync
    44  	default:
    45  		return fmt.Errorf(`unknown sync mode %q, want "full", "fast" or "light"`, text)
    46  	}
    47  	return nil
    48  }