github.com/amazechain/amc@v0.1.3/internal/download/modes.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package download
    18  
    19  import "fmt"
    20  
    21  // SyncMode represents the synchronisation mode of the downloader.
    22  // It is a uint32 as it is used with atomic operations.
    23  type SyncMode uint32
    24  
    25  const (
    26  	FullSync SyncMode = iota
    27  	SnapSync
    28  	LightSync
    29  	HeaderSync
    30  )
    31  
    32  func (mode SyncMode) IsValid() bool {
    33  	return mode >= FullSync && mode <= HeaderSync
    34  }
    35  
    36  // String implements the stringer interface.
    37  func (mode SyncMode) String() string {
    38  	switch mode {
    39  	case FullSync:
    40  		return "full"
    41  	case SnapSync:
    42  		return "snap"
    43  	case LightSync:
    44  		return "light"
    45  	case HeaderSync:
    46  		return "header"
    47  	default:
    48  		return "unknown"
    49  	}
    50  }
    51  
    52  func (mode SyncMode) MarshalText() ([]byte, error) {
    53  	switch mode {
    54  	case FullSync:
    55  		return []byte("full"), nil
    56  	case SnapSync:
    57  		return []byte("snap"), nil
    58  	case LightSync:
    59  		return []byte("light"), nil
    60  	case HeaderSync:
    61  		return []byte("header"), nil
    62  	default:
    63  		return nil, fmt.Errorf("unknown sync mode %d", mode)
    64  	}
    65  }
    66  
    67  func (mode *SyncMode) UnmarshalText(text []byte) error {
    68  	switch string(text) {
    69  	case "full":
    70  		*mode = FullSync
    71  	case "snap":
    72  		*mode = SnapSync
    73  	case "light":
    74  		*mode = LightSync
    75  	case "header":
    76  		*mode = HeaderSync
    77  	default:
    78  		return fmt.Errorf(`unknown sync mode %q, want "full", "snap" or "light"`, text)
    79  	}
    80  	return nil
    81  }